Problem A
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 1

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.
2 3 3 T 1 2 T 3 2 Q 2 3 4 T 1 2 Q 1 T 1 3 Q 1
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
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.
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
3 8 5 8
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
2 1 2 2 4
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
显然,作为多年拼搏的商人。XHD不会坐以待毙的。
一天,当他正在苦思冥想解困良策的时候,突然想到了自己的传家宝,那是公司成立的时候,父亲作为贺礼送来的一个锦囊,徐父当时交代。不到万不得已的时候。不要打开它。
“如今不正是最须要的时候吗?”,一边想,XHD一边找到了这个精心保管的锦囊,打开一看,里面仅仅有一句话“杭城北麓千人洞有宝”。
二话不说,XHD拿起一个大口袋就出发了,这个千人洞他是知道的,小的时候,爸爸以前带他来过这个隐蔽的路口,并告诉他,这是千人洞。他如今才明确爸爸当初这句话的含义。
虽然有点印象,XHD还是花了非常大的精力才找到这个异常隐蔽的洞口,走进一看。差点儿惊呆了,真的是眼花缭乱。只是虽然宝贝的种类不少。可是每种宝贝的量并不多。当然,每种宝贝单位体积的价格也不一样,为了拯救HDU,如今请你帮忙尽快计算出来XHD最多能带回多少价值的宝贝?(如果宝贝能够切割。切割后的价值和相应的体积成正比)
<="" div="">
2 2 3 1 2 3 0
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
2 2 6 08:00 09:00 5 08:59 09:59 2 6 08:00 09:00 5 09:00 10:00
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
。。
。以后从头開始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
2 20 40
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
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.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
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
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
随后是 N 行输入。每行的格式为: m Type_1:price_1 Type_2:price_2 ... Type_m:price_m 当中正整数 m 是这张发票上所开物品的件数。Type_i 和 price_i 是第 i 项物品的种类和价值。
物品种类用一个大写英文字母表示。当N为0时,所有输入结束。对应的结果不要输出。
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
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
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.
2 3 7
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; }