第二周个人赛

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 1
Problem Description
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. 

His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls. 
Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
 

Input
The first line of the input is a single positive integer T(0 < T <= 100). For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000). Each of the following Q lines contains either a fact or a question as the follow format: T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different. Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
 

Output
For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
 

Sample Input
2 3 3 T 1 2 T 3 2 Q 2 3 4 T 1 2 Q 1 T 1 3 Q 1
 

Sample Output
Case 1: 2 3 0 Case 2: 2 2 1 3 3 2
 


#include<stdio.h>
int parent[101010];
int num[101001];//表示移动次数 
int rank[100010];//表示该地有多少颗龙珠 
int find(int x)
{
	if(x==parent[x]) return x;
	
    int t=parent[x];
    parent[x]=find(parent[x]);//压缩路径 ,都指向根节点 
    num[x]+=num[t];//每一个球移动的次数等于本身移动的个数加上父节点移动的次数
    return parent[x];	 

//	int r=x;
//	while(r!=parent[r])
//	{
//		num[r]+=num[parent[r]]; //num须要通过加上父节点来更新 
//		r=parent[r];						
//	}		
//	int i=x,j;//路径压缩 
//	while(i!=r)
//	{		
//		j=parent[i];
//		parent[i]=r;
//		i=j;
//	}
//	return r;
}

void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		parent[fx]=fy;//把fx的根节点赋予fy,即把城市fx的龙珠给城市移到fy 
		num[fx]=1;//头结点移动一次 
		rank[fy]+=rank[fx];//根节点代表城市 
	}
}

int main()
{
	int t;
	int n,m,a,b;
	int i;
	char ch;
	int cnt=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		getchar();
		for(i=1;i<=n;++i)
		{
			parent[i]=i;
			num[i]=0;
			rank[i]=1; 
		}
		int flag=1;
//		printf("Case %d:
",++cnt);
		while(m--)
		{
			scanf("%c",&ch);
			if(ch=='T')
			{
				scanf("%d%d",&a,&b);
				getchar();
				join(a,b);//把a所在的城市的龙珠调到城市b 
			}
			else if(ch=='Q')
			{
				scanf("%d",&a);
				getchar();
				int temp=find(a); //找龙珠a在哪个城市,在找的时候不停的会加上它的父节点 
				if(flag) {
					printf("Case %d:
",++cnt);
					flag=0;
				}
				printf("%d %d %d
",temp,rank[temp],num[a]); 
			} 
		}
	}
	return 0;
}


Problem B

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 2
Problem Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

 

Input
Line 1: One integer N, the number of planks 
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
 

Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts
 

Sample Input
3 8 5 8
 

Sample Output
34
 


/*
题意:将一块木块砍一刀,当前木块是多长就须要多少金币,
求花最少的金币砍成你想要的木块长度 
*/
//哈夫曼树+优先队列 poj 3253 

//每次实现最小的两个数相加 
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int main()
{
	int n,i;
	__int64 a,b;
	__int64 m;
	scanf("%d",&n);
	{
		priority_queue<__int64,vector<__int64>,greater<__int64> >q;
		
		for(i=0;i<n;++i)		
		{
			scanf("%I64d",&m);
			q.push(m);
		}								
		__int64 sum=0;
		while(q.size()>1)
		{
			a=q.top();
			q.pop();
			b= q.top();
			q.pop();
			sum+=a+b;			
			q.push(a+b);	
		}
		printf("%I64d
",sum);
	}
}

Problem C

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 114   Accepted Submission(s) : 80
Problem Description
给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形。下图为高为2。宽为4的网格.
 

Input
第一行输入一个t, 表示有t组数据,然后每行输入n,m,分别表示网格的高和宽 ( n < 100 , m < 100).
 

Output
每行输出网格中有多少个矩形.
 

Sample Input
2 1 2 2 4
 

Sample Output
3 30
 


#include<stdio.h>
int main()
{
    int t;
    __int64 res,m,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d %I64d",&m,&n);
        res=(m+1)*m/2*(n+1)*n/2;
        printf("%I64d
",res);
    }
    return 0;
}


Problem D

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 145   Accepted Submission(s) : 61
Problem Description
话说上回讲到海东集团面临内外交困,公司的元老也仅仅剩下XHD夫妇二人了。

显然,作为多年拼搏的商人。XHD不会坐以待毙的。
  一天,当他正在苦思冥想解困良策的时候,突然想到了自己的传家宝,那是公司成立的时候,父亲作为贺礼送来的一个锦囊,徐父当时交代。不到万不得已的时候。不要打开它。

“如今不正是最须要的时候吗?”,一边想,XHD一边找到了这个精心保管的锦囊,打开一看,里面仅仅有一句话“杭城北麓千人洞有宝”。


  二话不说,XHD拿起一个大口袋就出发了,这个千人洞他是知道的,小的时候,爸爸以前带他来过这个隐蔽的路口,并告诉他,这是千人洞。他如今才明确爸爸当初这句话的含义。
  虽然有点印象,XHD还是花了非常大的精力才找到这个异常隐蔽的洞口,走进一看。差点儿惊呆了,真的是眼花缭乱。只是虽然宝贝的种类不少。可是每种宝贝的量并不多。当然,每种宝贝单位体积的价格也不一样,为了拯救HDU,如今请你帮忙尽快计算出来XHD最多能带回多少价值的宝贝?(如果宝贝能够切割。切割后的价值和相应的体积成正比)

 

Input
输入包括多个測试实例,每一个实例的第一行是两个整数v和n(v,n<100)。分别表示口袋的容量和宝贝的种类。接着的n行每行包括2个整数pi和mi(0<pi,mi<10),分别表示某种宝贝的单位价格和相应的体积。v为0的时候结束输入。

<="" div="">

 

Output
对于每一个測试实例,请输出XHD最多能取回多少价值的宝贝。每一个实例的输出占一行。
 

Sample Input
2 2 3 1 2 3 0
 

Sample Output
5 经过锦囊相助,HDU会脱离危机吗? 欲知后事怎样,且听下回分解——
 


//背包问题 

#include<cstdio>
#include<algorithm>
using namespace std;
struct valu
{
    int price;
    int kinds;
}arr[110];

int cmp(valu a,valu b)
{
    return a.price>b.price;//这里是单位价格 
}
int main()
{
    int m,n;
    int i;
    while(scanf("%d",&m),m)
    {
        scanf("%d",&n);
        for(i=0;i<n;++i)
        {
            scanf("%d%d",&arr[i].price,&arr[i].kinds);            
        }
        sort(arr,arr+n,cmp);
        int sum=0;
        for(i=0;i<n;++i)
        {
            if(m>=arr[i].kinds)
            {
                sum+=arr[i].price*arr[i].kinds;
                m-=arr[i].kinds;
            }
            else
            {
                sum+=m*arr[i].price;
                break;
            }
        }
        printf("%d
",sum);
    }
    return 0;
}


Problem E

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 150   Accepted Submission(s) : 6
Problem Description
TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the ith group in sum. Assuming that each customer can own only one chair. Now we know the arriving time STi and departure time EDi of each group. Could you help TIANKENG calculate the minimum chairs he needs to prepare so that every customer can take a seat when arriving the restaurant?
 

Input
The first line contains a positive integer T(T<=100), standing for T test cases in all. Each cases has a positive integer n(1<=n<=10000), which means n groups of customer. Then following n lines, each line there is a positive integer Xi(1<=Xi<=100), referring to the sum of the number of the ith group people, and the arriving time STi and departure time Edi(the time format is hh:mm, 0<=hh<24, 0<=mm<60), Given that the arriving time must be earlier than the departure time. Pay attention that when a group of people arrive at the restaurant as soon as a group of people leaves from the restaurant, then the arriving group can be arranged to take their seats if the seats are enough.
 

Output
For each test case, output the minimum number of chair that TIANKENG needs to prepare.
 

Sample Input
2 2 6 08:00 09:00 5 08:59 09:59 2 6 08:00 09:00 5 09:00 10:00
 

Sample Output
11 6
 

/*
题意为:用最少的座位数迎接在不同一时候间段来的客人 

*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1000010];
int main()
{
	int t,n,s;
	int i,j;
	int max;
	int hour1,hour2,min1,min2; 
	scanf("%d",&t);
	while(t--)
	{
		memset(a,0,sizeof(a));//一開始要清零 
		scanf("%d",&n);
		max=-100;//纪录最小的座位 
		for(i=0;i<n;++i)
		{
			scanf("%d %d:%d %d:%d",&s,&hour1,&min1,&hour2,&min2);//s表示这个时间段来的人数 
			int sum1=hour1*60+min1;//每一个时间都有一个sum1和sum2这个区间 
			int sum2=hour2*60+min2;
			
			for(j=sum1;j<sum2;++j)//当两区间有公共部分时,数组a[i]刚才也存了一个数了 
			{
				a[j]+=s;
				if(a[j]>max)//当数组a纪录的座位数大于max。要把max更新 
					max=a[j];
			}
		}
		printf("%d
",max);	
	}
	return 0;
}



Problem F

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 85   Accepted Submission(s) : 25
Problem Description
某部队进行新兵队列训练,将新兵从一開始按顺序依次编号。并排成一行横队,训练的规则例如以下:从头開始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢。再从头開始进行一至三报数。凡报到三的出列,剩下的向小序号方向靠拢,继续从头開始进行一至二报数。

。。

。以后从头開始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。


 

Input
本题有多个測试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
 

Output
共同拥有N行,分别相应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。

 

Sample Input
2 20 40
 

Sample Output
1 7 19 1 19 37
 

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int main()
{
	int t,i;
	int n,m;
	scanf("%d",&t);
	while(t--)
	{		
		int a[100010]={0};
		scanf("%d",&n);
		for(i=1;i<=n;++i)
		{
			a[i]=i;
		}
		int m=n;
		if(n<=3)
		{
			printf("1");
			for(i=2;i<=n;++i)
			{
				printf(" %d",i);
			}
			puts("");
			continue;
		}
		while(1)
		{
			int num=0;
			for(i=1;i<=n;++i)
			{
				if(a[i])				
					++num;				
				if(a[i]&&num==2)
				{
					num=0;
					m--;
					a[i]=0;
				}
			}
			if(m<=3) break;
			num=0;
			for(i=1;i<=n;++i)
			{
				if(a[i])
					num++;
				if(a[i]&&num==3)
				{
					num=0;
					m--;
					a[i]=0;
				}
			}
			if(m<=3) break;
		}	
		printf("1");
		for(i=2;i<=n;++i)
		{		
			if(a[i])			
				printf(" %d",a[i]);			
		}
		printf("
");
	} 
	return 0;
}


Problem G

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 40000/20000K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1
Problem Description
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). 
Once a member in a group is a suspect, all members in the group are suspects. 
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.
 

Input
The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
 

Output
For each case, output the number of suspects in one line.
 

Sample Input
100 4 2 1 2 5 10 13 11 12 14 2 0 1 2 99 2 200 2 1 5 5 1 2 3 4 5 1 0 0 0
 

Sample Output
4 1 1
 


#include<stdio.h>
int n;
int per[30300];
void init()
{
	for(int i=0;i<=n;++i)
	{
		per[i]=i;
	}
}

int find(int x)
{
	if(x==per[x]) return x;
	return per[x]=find(per[x]);
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
	}
}
int main()
{
	int T,m;
	int a,temp,b;
	while(~scanf("%d%d",&n,&T),n+T)
	{
		init();
		while(T--)
		{
			scanf("%d",&m);
			scanf("%d",&a);
			for(int i=1;i<m;++i)
			{
				scanf("%d",&b);				
				join(a,b);
			}
		}
		int sum=0;
		temp=find(0);
		for(int i=0;i<n;++i)
		{
			if(find(i)==temp) sum++;
		}
		printf("%d
",sum);
	}
	return 0;
}



Problem H

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 3
Problem Description
现有一笔经费能够报销一定额度的发票。同意报销的发票类型包含买图书(A类)、文具(B类)、差旅(C类)。要求每张发票的总额不得超过1000元。每张发票上,单项物品的价值不得超过600元。现请你编敲代码。在给出的一堆发票中找出能够报销的、不超过给定额度的最大报销额。


 

Input
測试输入包括若干測试用例。每一个測试用例的第1行包括两个正数 Q 和 N,当中 Q 是给定的报销额度。N(<=30)是发票张数。

随后是 N 行输入。每行的格式为: m Type_1:price_1 Type_2:price_2 ... Type_m:price_m 当中正整数 m 是这张发票上所开物品的件数。Type_i 和 price_i 是第 i 项物品的种类和价值。

物品种类用一个大写英文字母表示。当N为0时,所有输入结束。对应的结果不要输出。

 

Output
对每一个測试用例输出1行。即能够报销的最大数额,精确到小数点后2位。
 

Sample Input
200.00 3 2 A:23.50 B:100.00 1 C:650.00 3 A:59.99 A:120.00 X:10.00 1200.00 2 2 B:600.00 A:400.00 1 C:200.50 1200.50 3 2 B:600.00 A:400.00 1 C:200.50 1 A:100.00 100.00 0
 

Sample Output
123.50 1000.00 1200.50
 

//待续



Problem I

Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 28   Accepted Submission(s) : 7
Problem Description
  In the RPG game “go back ice age”(I decide to develop the game after my undergraduate education), all heros have their own respected value, and the skill of killing monsters is defined as the following rule: one hero can kill the monstrers whose respected values are smaller then himself and the two respected values has none common factor but 1, so the skill is the same as the number of the monsters he can kill. Now each kind of value of the monsters come. And your hero have to kill at least M ones. To minimize the damage of the battle, you should dispatch a hero with minimal respected value. Which hero will you dispatch ?

There are Q battles, in each battle, for i from 1 to Q, and your hero should kill Mi ones at least. You have all kind of heros with different respected values, and the values(heros’ and monsters’) are positive.

 

Input
The first line has one integer Q, then Q lines follow. In the Q lines there is an integer Mi, 0<q<=1000000, 0<mi<="10000." <="" div="">
 

Output
For each case, there are Q results, in each result, you should output the value of the hero you will dispatch to complete the task.
 

Sample Input
2 3 7
 

Sample Output
5 11
 

//找比他大的素数

#include<stdio.h>
#include<string.h>
int a[100050];
void IsPrime()
{
	a[1]=1;
	a[0]=1;
	int i,j;
	for(i=2;i<100000;++i)
	{
		for(j=2*i;j<100000;j+=i)
		{
			a[j]=1;
		}
	}
}
int main()
{	
	
	int t,n,i;
	IsPrime();
	scanf("%d",&t);	
	while(t--)
	{
		scanf("%d",&n);
		for(i=n+1;i<100000;++i)
		{
			if(!a[i])
			{
				printf("%d
",i);
				break;
			}
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/gavanwanggw/p/7299951.html