HDU - 1541(线段树单点更新.)、POJ - 1287(最小生成树.)、HDU - 1560(迭代深搜.)

Time limit1000 ms
Memory limit32768 kB
OSWindows

题目:
Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it’s formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.

You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0

思路:这道题还是比较巧妙的,我们可以发现我们只需要统计线段树内比x小的叶子节点的个数即可。因为题目说了按y的递增顺序给出,所以我们在一个个插入的时候们只要比较比x小的区间有多少个点即可。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
const int maxn = 32000+10;
int tree[maxn<<2],t,x,y,ans[maxn<<2];
 
void insert(int l,int r,int i)
{
    if(l == r)
        t+=tree[i];
    else
    {
        int mid = (l+r)>>1;
        if(x<=mid)
            insert(l,mid,2*i);
        else
        {
            t+=tree[2*i];//走右子树,则把左子树的所有星星数目加起来
            insert(mid+1,r,2*i+1);
        }
    }
    tree[i]++;
}
 
int main()
{
    int n,i;
    while(~scanf("%d",&n))
    {
        memset(tree,0,sizeof(tree));
        memset(ans,0,sizeof(ans));
        for(i = 0; i<n; i++)
        {
            scanf("%d%d",&x,&y);
            t = 0;
            insert(0,maxn,1);
            ans[t]++;//等级个数
        }
        for(i = 0; i<n; i++)
            printf("%d\n",ans[i]);
    }
 
    return 0;
}

Time limit 1000 ms
Memory limit 10000 kB
OS Linux

题目:
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.
Sample Input
1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0
Sample Output
0
17
16
26

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[110];
int par[110]; 
int n,m;
int i ,j;
struct node{
	int u,v,w;
}arr[5500];
void init()
{
	for(i=1;i<=n;i++)
	par[i]=i;
}
bool cmp(node a,node b)
{
	return a.w<b.w;
}
int find(int x)
{
	if(x==par[x])
	return x;
	else
	return par[x]=find(par[x]);
}
int unite(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		par[fy]=fx;
		return 1;
	}
	return 0;
}
int main()
{
	while(scanf("%d",&n),n)
	{
		int ans=0,ant=0;
		int sum=0;
		if(n==0)
		break;
		scanf("%d",&m);
		//m=n*(n-1)/2;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d",&arr[i].u,&arr[i].v,&arr[i].w);
		}
		sort(arr+1,arr+m+1,cmp);
		init();
		for(i=1;i<=m;i++)
		{
		 if(unite(arr[i].u,arr[i].v))
		 {
		 	ant++;
		 	sum+=arr[i].w;
		 }	
		 if(ant==n-1)
		 break;
		}
		printf("%d\n",sum);
	}
}

Time limit5000 ms
Memory limit32768 kB
OSWindows

题目:
The twenty-first century is a biology-technology developing century. We know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Finding the longest common subsequence between DNA/Protein sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence of it.

For example, given “ACGT”,“ATGC”,“CGTT” and “CAGT”, you can make a sequence in the following way. It is the shortest but may be not the only one.

Input
The first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any sequence is between 1 and 5.
Output
For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
Sample Input
1
4
ACGT
ATGC
CGTT
CAGT
Sample Output
8

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
 
using namespace std;
const int MAXN = 10;
const int inf = 1e9;
int n,m;
//记录字符串
char s[MAXN][MAXN];
//记录字符串的长度
int sl[MAXN];
//记录字符串当前匹配到哪了
int point[MAXN];
char DNA[]="AGCT";
int check()
{
    int res = 0;
    for(int i = 0; i < n; ++i)res = max(res,sl[i] - point[i]);
    return res;
}
int flag;
void dfs(int sum)
{
    int c = check();
    //如果找到了解
    if(!c)
    {
        flag = 1;
        return;
    }
    //当前不可能符合条件
    if(sum < c)return;
    //保存现场
    int temp[10];
    for(int i = 0; i < n; ++i)temp[i] = point[i];
    //搜索开始
    //表示是否有变化
    bool ok = 0;
    //枚举添加上的字符
    for(int i = 0; i < 4; ++i)
    {
        //更新point的状态
        for(int j = 0; j < n; ++j)
        {
            if(s[j][point[j]] == DNA[i])
            {
                point[j]++;
                ok = 1;
            }
        }
        //如果更新了
        if(ok)
        {
            //继续枚举下一个字符
            dfs(sum-1);
            if(flag)return;
            //恢复现场
            for(int j = 0 ;j < n; ++j)point[j] = temp[j];
        }
    }
}
 
 
 
 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans = 0;
        for(int i = 0; i < n; ++i)
        {
            scanf("%s",s[i]);
            sl[i] = strlen(s[i]);
            point[i] = 0;
            ans = max(ans,sl[i]);
        }
        flag = 0;
        while(1)
        {
            dfs(ans);
            if(flag)break;
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43967023/article/details/86684958