Problem A
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 1
#includeint 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:\n",++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:\n",++cnt); flag=0; } printf("%d %d %d\n",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.
/*题意:将一块木块砍一刀,当前木块是多长就须要多少金币,求花最少的金币砍成你想要的木块长度 *///哈夫曼树+优先队列 poj 3253 //每次实现最小的两个数相加 #include#include #include 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 1) { a=q.top(); q.pop(); b= q.top(); q.pop(); sum+=a+b; q.push(a+b); } printf("%I64d\n",sum); }}
Problem C
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 114 Accepted Submission(s) : 80
#includeint 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\n",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="">
//背包问题 #include#include 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 =arr[i].kinds) { sum+=arr[i].price*arr[i].kinds; m-=arr[i].kinds; } else { sum+=m*arr[i].price; break; } } printf("%d\n",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
/*题意为:用最少的座位数迎接在不同一时候间段来的客人 */#include#include #include 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 max)//当数组a纪录的座位数大于max。要把max更新 max=a[j]; } } printf("%d\n",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
。。
。以后从头開始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
#include#include #include 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("\n"); } return 0;}
Problem G
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 40000/20000K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
#includeint 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
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时,所有输入结束。对应的结果不要输出。
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.
#include#include 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\n",i); break; } } } return 0;}