网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26733#overview
贪心全场!!!!
A题:
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> using namespace std; typedef struct node { double x1; double x2; }point; point position[1010]; bool cmp(point a,point b) { return a.x1<b.x1; } int main() { int n,d; int i; int tempx,tempy; int ans; double temp; int flage; int CASE=1; while(~scanf("%d%d",&n,&d)) { if(n==0&&d==0)break; flage=0; for(i=1;i<=n;i++) { scanf("%d %d",&tempx,&tempy); if(tempy>d){ flage=1;}//此处不可以加break else if(flage==0) { position[i].x1=tempx-sqrt(double(d*d-tempy*tempy)); position[i].x2=tempx+sqrt(double(d*d-tempy*tempy)); } } printf("Case %d: ",CASE++); if(!flage) { sort(position+1,position+1+n,cmp); temp=position[1].x2; ans=1; for(i=2;i<=n;i++) { if(position[i].x1-temp>10e-7) { ans++; temp=position[i].x2; } else { temp=temp<position[i].x2?temp:position[i].x2; } } printf("%d ",ans); } if(flage) printf("-1 "); } return 0; } //网上找的代码.....能AC,0MS。
我自己写的。。。。真心不知道哪错了,一直WA。
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 using namespace std; 5 class A 6 { 7 public: 8 int a; 9 int b; 10 }z[1005]; 11 int dis(int y,int d) 12 { 13 return sqrt(d*d-y*y); 14 } 15 int main() 16 { 17 int n,i,x[1005],y[1005],zhong,sum=0,j,k,t,d,q; 18 while(~scanf("%d%d",&n,&d)) 19 { 20 if(n==0&&d==0) 21 break; 22 sum++; 23 zhong =0; 24 for(i=0;i<n;i++) 25 { 26 scanf("%d%d",&x[i],&y[i]); 27 if(y[i]>d) 28 zhong=1; 29 } 30 if(zhong==1) 31 cout<<"Case "<<sum<<": "<<"-1"<<endl; 32 else if(zhong ==0) 33 { 34 if(n==1) 35 cout<<"Case "<<sum<<": "<<"1"<<endl; 36 else 37 { 38 for(i=0;i<n;i++) 39 { 40 t=dis(y[i],d); 41 z[i].a=x[i]-t; 42 z[i].b=x[i]+t; 43 } 44 for(i=0;i<n-1;i++) 45 for(j=i+1;j<n;j++) 46 { 47 if(z[i].a>z[j].a) 48 { 49 t=z[i].a; 50 z[i].a=z[j].a; 51 z[j].a=t; 52 t=z[i].b; 53 z[i].b=z[j].b; 54 z[j].b=t; 55 } 56 } 57 k=1; 58 q=z[0].b; 59 for(j=1;j<n;j++) 60 { 61 if(q<z[j].a) 62 { 63 k++; 64 q=z[j].b; 65 } 66 else 67 q=q<z[j].b?q:z[j].b; 68 } 69 70 cout<<"Case "<<sum<<": "<<k<<endl; 71 } 72 } 73 } 74 return 0; 75 } //求大神指点......
知道哪错了。。。。精度问题!!!!!!雷达可以不在整数坐标上!!!!!!!
接下来是改过的,能AC的代码:47MS
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; class A { public: double a; //double !!!
double b; }z[1005]; double dis(double y,double d) //注意用double !!!!!
{ return sqrt(d*d-y*y); } int main() { int n,i,zhong,sum=0,j,k; double d,x[1005],y[1005],q,t; while(~scanf("%d%lf",&n,&d)) { if(n==0&&d==0) break; sum++; zhong =0; for(i=0;i<n;i++) { scanf("%lf%lf",&x[i],&y[i]); if(y[i]>d) zhong=1; } if(zhong==1) cout<<"Case "<<sum<<": "<<"-1"<<endl; else if(zhong ==0) { if(n==1) cout<<"Case "<<sum<<": "<<"1"<<endl; else { for(i=0;i<n;i++) { t=dis(y[i],d); z[i].a=x[i]-t; z[i].b=x[i]+t; } for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(z[i].a>z[j].a) { t=z[i].a; z[i].a=z[j].a; z[j].a=t; t=z[i].b; z[i].b=z[j].b; z[j].b=t; } } k=1; q=z[0].b; for(j=1;j<n;j++) { if(q<z[j].a) { k++; q=z[j].b; } else q=q<z[j].b?q:z[j].b; } cout<<"Case "<<sum<<": "<<k<<endl; } } } return 0; }
再就是坑爹的B题,大意是,给出指数和最后的值,求底数.
颤抖吧!!!!!16MS
#include<iostream> #include<math.h> using namespace std; int main() { double s,n; while(cin>>n>>s) cout<<pow(s,1/n)<<endl;; return 0; }
深深的觉得Orz......
C题理解好题意即可。
以下题意解释来自:優YoU http://user.qzone.qq.com/289065406/blog/1299234147
题意比较难懂,其实只要读懂题意,就很简单了。
大意是一个公司在12个月中,或固定盈余s,或固定亏损d.
但记不得哪些月盈余,哪些月亏损,只能记得连续5个月的代数和总是亏损(<0为亏损),而一年中只有8个连续的5个月,分别为1~5,2~6,…,8~12
问全年是否可能盈利?若可能,输出可能最大盈利金额,否则输出“Deficit".
根据经验,贪心选择往往都在极端处(临界点)选择。(其实这题不用贪心,单纯枚举也可以AC,因为不同情况实在太少呐。。。。
不难证明,每连续5个月中,在保证这5个月经营之和为亏损的情况下,亏损的月数肯定应尽量往后选,盈利的月数应尽量往前选。证明省略。
先处理处理完1~5月后,剩下的月份可以根据“连续5个月经营之和为亏损”这个条件进行确定亏损还是盈利。
本题的贪心选择每次仅仅选取其中一种情况(1~5月),因为之后月份无需再选择,所以每次总共只做了一次贪心选择。
实际上;只要讨论5种情况即可;(任一月固定盈余s,或固定亏损d).
SSSSDSSSSDSS 4s<d 保证“连续5个月必亏损”,每连续5个月种至少1个月D,
保证可能有全年最大盈余,每连续5个月中至多4个月S
SSSDDSSSDDSS 3s<2d 保证“连续5个月必亏损”,每连续5个月种至少2个月D,
保证可能有全年最大盈余,每连续5个月中至多3个月S
SSDDDSSDDDSS 2s<3d 保证“连续5个月必亏损”,每连续5个月种至少3个月D,
保证可能有全年最大盈余,每连续5个月中至多2个月S
SDDDDSDDDDSD s<4d 保证“连续5个月必亏损”,每连续5个月种至少4个月D,
保证可能有全年最大盈余,每连续5个月中至多1个月S
DDDDDDDDDDDD s>=4d 保证“连续5个月必亏损”,每连续5个月种至少5个月D,
每月亏损,此情况全年必亏损
要注意的是,前4种情况都仅仅是“可能有全年的盈余”,而不是“一定有全年的盈余”。
但是若果一旦有盈余,必定是最大盈余
把5种情况可以归纳为关于s的判定条件:
0 <= s <1/4d 每连续5个月种至少1个月D
1/4d <= s < 2/3d 每连续5个月种至少2个月D
2/3d <= s < 3/2d 每连续5个月种至少3个月D
3/2d <= s < 4d 每连续5个月种至少4个月D
4d <= s 全年各月必亏损
代码:
#include<iostream> #include<stdio.h> using namespace std; int main() { int sum; double s,d; while(~scanf("%lf%lf",&s,&d)) { sum=0; if(s>=0&&s*4<d) sum=10*s-d*2; else if(s*4>=d&&s*3<d*2) sum=8*s-4*d; else if(s*3>=d*2&&s*2<d*3) sum=6*s-6*d; else if(s*2>=3*d&&s<4*d) sum=3*s-9*d; if(s>=4*d || sum<0) cout<<"Deficit"<<endl; else cout<<sum<<endl; } return 0; }
D题: 来自一学姐,感觉不错~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 #include<algorithm> 3 #include <iostream> 4 using namespace std; 5 const int maxn=100000+10; 6 struct Line 7 { 8 int s; 9 int t; 10 }line[maxn],temp[maxn]; 11 bool cmp(Line a,Line b) 12 { 13 return a.s<b.s||(a.s==b.s&&a.t>b.t); 14 } 15 int main() 16 { 17 int m,k,i,j,ans; 18 int n,cnt,l,r; 19 scanf("%d",&m); 20 cnt=0; 21 for(;;) 22 { 23 scanf("%d%d",&l,&r); 24 if(l==0&&r==0) 25 break; 26 line[++cnt].s=l; 27 line[cnt].t=r; 28 29 } 30 31 sort(line+1,line+cnt+1,cmp); 32 j=1; 33 for(i=2;i<=cnt;i++)//将互不覆盖的线段存起来 34 if(line[i].s>line[j].s&&line[i].t>line[j].t) 35 line[++j]=line[i]; 36 n=j; 37 ans=0; 38 line[n+1].s=m+1; 39 line[n+1].t=m+1; 40 k=0;//第一个临界 41 for(i=1;i<=n;i++) 42 if(line[i+1].s>k&&line[i].s<=k) 43 { 44 k=line[i].t; 45 temp[++ans]=line[i]; 46 if(line[i].t>=m) 47 { 48 printf("%d ",ans); 49 for(j=1;j<=ans;j++) 50 printf("%d %d ",temp[j].s,temp[j].t); 51 return 0; 52 } 53 } 54 printf("No solution "); 55 return 0; 56 }
E题: 先是要读懂题:
以下解释来自:http://blog.sina.com.cn/s/blog_4dc813b20100t133.html
将整个题归结为一棵树,求最多能给多少个节点染色,根节点不染色,且染色规则如下:
(1)每一个节点至多只有一个儿子被染色
(2)如果某个节点被染色,那么它的所有儿子都不能染色。
再就是代码,不过要先去看看vector(向量容器):http://s.acmore.net/show_article/show/41
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<cstring> 5 using namespace std; 6 #define N 500001 7 struct node{ 8 int depth; 9 int i; 10 }order[N]; 11 int parent[N]; 12 int n; 13 bool mark[N]; 14 vector<int> result;//vector(向量容器) 网站:http://s.acmore.net/show_article/show/41 15 bool cmp(const node a,const node b) 16 { 17 return a.depth>b.depth; 18 } 19 int main() 20 { 21 int i; 22 order[1].depth=0; 23 order[1].i=1; 24 while(cin>>n) 25 { 26 for(i=2;i<=n;i++) 27 { 28 cin>>parent[i]; 29 order[i].depth=order[parent[i]].depth+1; 30 order[i].i=i; 31 } 32 sort(order+2,order+n+1,cmp); 33 result.clear(); 34 memset(mark,0,sizeof(mark)); 35 for(i=2;i<=n;i++){ 36 if(!mark[order[i].i]&&!mark[parent[order[i].i]]) 37 { 38 result.push_back(order[i].i); //元素扩展 39 mark[order[i].i]=1; 40 mark[parent[order[i].i]]=1; 41 } 42 } 43 cout<<result.size()*1000<<endl; 44 sort(result.begin(),result.end()); 45 cout<<result[0]; 46 for(i=1;i<result.size();i++){ 47 cout<<" "<<result[i]; 48 } 49 cout<<endl; 50 } 51 return 0; 52 }
F题:
死了N遍:为我默哀吧。。。。还是不知道哪错了......正解参照:http://blog.csdn.net/cfreezhan/article/details/8007554(来自学姐~~~)
1 #include<iostream> 2 #include<stdio.h> 3 #include<algorithm> 4 using namespace std; 5 class diqu 6 { 7 public: 8 int bian; 9 int shu; 10 int leve; 11 }d[1005]; 12 class student 13 { 14 public: 15 int bian; 16 int le; 17 int w; 18 int di; 19 }s[1605]; 20 bool cmp(diqu a,diqu b) 21 { 22 return a.leve>b.leve; 23 } 24 bool cnp(student a,student b) 25 { 26 return a.w>b.w; 27 } 28 bool cn(student a,student b) 29 { 30 return a.bian<b.bian; 31 } 32 int main() 33 { 34 int n,i,j,num=0; 35 scanf("%d",&n); 36 for(i=1;i<=n;i++) 37 { 38 scanf("%d",&d[i].shu); 39 d[i].bian=i; 40 num=num+d[i].shu; 41 } 42 for(i=1;i<=n;i++) 43 scanf("%d",&d[i].leve); 44 for(i=1;i<=num;i++) 45 { 46 s[i].di=-1; 47 s[i].bian=i; 48 scanf("%d",&s[i].le); 49 } 50 for(i=1;i<=num;i++) 51 scanf("%d",&s[i].w); 52 sort(d+1,d+n+1,cmp); 53 sort(s+1,s+num+1,cnp); 54 for(i=1;i<=num;i++) 55 for(j=1;j<=n;j++) 56 if(s[i].le>d[j].leve&&d[j].shu) 57 { 58 s[i].di=d[j].bian; 59 d[j].shu--; 60 break; 61 } 62 i=1; 63 for(j=1;j<=num;j++) 64 { 65 if(s[j].di!=-1) 66 continue; 67 while(d[i].shu==0) 68 i++; 69 s[j].di=d[i].bian; 70 d[i].shu--; 71 } 72 sort(s+1,s+num+1,cn); 73 for(i=1;i<=num-1;i++) 74 printf("%d ",s[i].di); 75 printf("%d ",s[num].di); 76 return 0; 77 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
我擦!!!!最后居然有空格!!!!
最后两句改成:
for(i=1;i<=num;i++)
printf("%d ",s[i].di);