CV的人有福啦!YTU贪心训练2(部分注释)

警钟长鸣----J题我做了40分钟一直不知道为什么错,后来发现错在哪了结果超时了(dp>搜索)

无聊做了做(第一题就被水了)

1743 Problem A

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100010; 4 int n,k,s[N],a[N],sum[N],ans,x,y,res; 5 int main() 6 { 7 cin>>n>>k; 8 for(int i=0;i<n;i++) cin>>a[i]; 9 sort(a,a+n); 10 while(y<n) 11 { 12 s[x]+=a[y],sum[x]+=s[x]; 13 x++,y++; 14 if(x==k) x=0; 15 } 16 for(int i=0;i<k;i++) res+=sum[i]; 17 printf("%.0lf",(double)res/n); 18 return 0; 19 }

1757 Problem B

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100010; 4 struct node 5 { 6 int x,y; 7 bool operator<(const node &w)const 8 { 9 return x<w.x; 10 } 11 }range[N]; 12 int n,res; 13 int main() 14 { 15 cin>>n; 16 for(int i=0;i<n;i++) 17 { 18 int a,b; 19 cin>>a>>b;if(a>b) swap(a,b); 20 range[i]={a,b}; 21 } 22 sort(range,range+n); 23 int ed=range[0].r; 24 for(int i=1;i<n;i++) 25 { 26 if(range[i].x<=ed) res++; 27 else ed=range[i].y; 28 } 29 cout<<res; 30 return 0; 31 }

1002 Problem C

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100010; 4 int n,m; 5 double res; 6 struct node 7 { 8 double t,v,x; 9 bool operator<(const node &w)const 10 { 11 return x>w.x; 12 } 13 }f[N]; 14 int main() 15 { 16 while(cin>>n>>m&&n!=0) 17 { 18 memset(f,0,sizeof f); 19 res=0; 20 for(int i=1;i<=n;i++) cin>>f[i].t>>f[i].v,f[i].x=f[i].v/f[i].t; 21 sort(f+1,f+n+1); 22 for(int i=1;i<=n;i++) 23 { 24 if(m-f[i].t<0) 25 { 26 res+=f[i].x*(double)m; 27 break; 28 } 29 else m-=f[i].t,res+=(double)f[i].v; 30 } 31 printf("%.2lf\n",res); 32 } 33 return 0; 34 }

2461 Problem D

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100010; 4 int n,res; 5 struct node 6 { 7 int x,y; 8 bool operator<(const node &w )const 9 { 10 return y<w.y; 11 } 12 }ran[N]; 13 int main() 14 { 15 while(cin>>n&&n!=0) 16 { 17 res=0; 18 for(int i=1;i<=n;i++) cin>>ran[i].x>>ran[i].y; 19 sort(ran+1,ran+1+n); 20 int ed=ran[1].y; 21 for(int i=2;i<=n;i++) 22 if(ran[i].x>=ed) 23 { 24 res++; 25 ed=ran[i].x; 26 } 27 cout<<res; 28 } 29 return 0; 30 }

1746 Problem E

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1000010; 4 int n,k,a[N],res=1; 5 int main() 6 { 7 cin>>n>>k; 8 for(int i=0;i<n;i++) cin>>a[i]; 9 sort(a,a+n); 10 int ed=a[0]+k; 11 for(int i=1;i<n;i++) 12 if(a[i]>ed) 13 { 14 res++; 15 ed=a[i]+k; 16 } 17 cout<<res; 18 return 0; 19 }

2897 Problem F

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1000010; 4 int n,k,m,r,res; 5 struct node 6 { 7 int l,r; 8 }p[N]; 9 bool cmp(node a,node b) 10 { 11 if(a.r==b.r) return a.l<b.l; 12 else return a.r<b.r; 13 } 14 int main() 15 { 16 cin>>k; 17 while(k--) 18 { 19 res=1; 20 cin>>n>>r; 21 for(int i=1;i<=n;i++) 22 { 23 int a,b; 24 cin>>a>>b; 25 double x=sqrt(r*r-b*b); 26 p[i].l=a-x,p[i].r=a+x; 27 } 28 sort(p+1,p+1+n,cmp); 29 int ed=p[1].r; 30 for(int i=2;i<=n;i++) 31 if(p[i].l>ed) res++,ed=p[i].r; 32 cout<<res<<endl; 33 } 34 return 0; 35 }

3394 Problem G

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100010; 4 int n,m,res,sum; 5 struct node 6 { 7 double num,v; 8 bool operator<(const node&w)const 9 { 10 return v>w.v;//按价值排序 11 } 12 }f[N]; 13 bool vis; 14 int main() 15 { 16 cin>>n>>m; 17 int p=m; 18 for(int i=0;i<n;i++) 19 { 20 double a,b,c; 21 cin>>a>>b>>c; 22 sum+=b; 23 if(a>b) vis=true;//判断 24 res+=a*c; 25 m-=a; 26 f[i]={b-a,c}; 27 } 28 if(sum<p) vis=true;//判断 29 if(vis) { 30 cout<<"-1"; 31 return 0; 32 } 33 sort(f,f+n); 34 for(int i=0;i<n;i++) cout<<f[i].v<<" "; 35 for(int i=0;i<n;i++) 36 { 37 if(m<=0) break; 38 while(f[i].num&&m!=0)//这里错了,注意啊,m不能等于0 39 { 40 m--; 41 f[i].num--; 42 res+=f[i].v; 43 } 44 } 45 cout<<res; 46 return 0; 47 }

1310 Problem H

1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100; 4 int n,a[5]={1,3,9,27,81},f[N]; 5 string s; 6 bool vis; 7 void dfs(int st,int t,int num,int step) 8 { 9 if(vis) return; 10 if(num==t) 11 { 12 for(int i=0;i<step+1;i++) 13 { 14 if(i<step) cout<<f[i]<<s[i]; 15 else cout<<f[i]; 16 } 17 vis=true; 18 } 19 if(num>t) for(int i=st-1;i>=0;i--) 20 { 21 f[step+1]=a[i],s[step]='-'; 22 dfs(i,t,num-a[i],step+1); 23 f[step+1]=0; 24 } 25 else for(int i=st-1;i>=0;i--) 26 { 27 f[step+1]=a[i],s[step]='+'; 28 dfs(i,t,num+a[i],step+1); 29 f[step+1]=0; 30 } 31 return; 32 } 33 int main() 34 { 35 cin>>n; 36 int p; 37 if(n<=81) 38 {for(int i=0;i<5;i++) if(a[i]>=n){p=i;break;}} 39 else p=4; 40 f[0]=a[p]; 41 dfs(p,n,a[p],0); 42 return 0; 43 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=100000; 4 int n,k,res,a[5]={1,3,9,27,81}; 5 int main() 6 { 7 cin>>n; 8 if(n<=81) 9 {for(int i=0;i<5;i++) if(a[i]>=n) {k=i;break;}}//找到最接近的 10 else k=4; 11 res=a[k]; 12 cout<<a[k]; 13 int num=0; 14 while(res!=n) 15 { 16 int x,c; 17 for(int i=0;i<4;i++) 18 if(a[i]<=abs(n-res)&&a[i+1]>=abs(n-res)) {x=i;break;}//找到处于这之间的 19 if(abs(a[x]-res+n)>abs(a[x+1]-res+n)) c=x+1;//找到最接近的那个 20 else c=x; 21 if(res>n) cout<<'-'<<a[c],res-=a[c];//判断 22 else cout<<'+'<<a[c],res+=a[c]; 23 } 24 return 0; 25 }

3155 Problem I

1 #include<bits/stdc++.h> 2 using namespace std; 3 int a,b,c,d,e,f,res; 4 int op[4]={0,5,3,1}; 5 int main() 6 { 7 while(cin>>a>>b>>c>>d>>e>>f&&(a+b+c+d+e+f)!=0) 8 { 9 res=f+d+e+(c+3)/4; 10 int x=d*5+op[c%4];//先算2x2的箱子 11 if(b>x) res+=(b-x+8)/9; 12 int y=36*res-36*f-25*e-16*d-9*c-4*b; 13 if(a>y) res+=(a-y+35)/36; 14 cout<<res<<endl; 15 } 16 return 0; 17 }

 1914 Problem J (做了好久真的,就被卡了一个条件,做出来了结果还超时,上网搜答案发现要用动态规划,真的崩溃)

1 #include <iostream> 2 using namespace std; 3 int COUNT; 4 int cost[]={1,2,5,10,20,50,100}; 5 int main() 6 { 7 int n; 8 int i; 9 int s[251][102][8]={0}; 10 for(i=0;i<7;i++) 11 { 12 s[ cost[i] ][1][i]=1; 13 } 14 s[1][101][0]=1; 15 for(i=2;i<251;i++) 16 { 17 for(int j=1;j<=i&&j<101;j++) 18 { 19 for(int k=6;k>=0;k--) 20 { 21 if(i>cost[k]) 22 { 23 for(int l=k;l>=0;l--) 24 { 25 s[i][j][k]+=s[ i-cost[k] ][j-1][l]; 26 } 27 } 28 s[i][j][7]+=s[i][j][k]; 29 } 30 s[i][101][0]+=s[i][j][7]; 31 } 32 } 33 while(cin>>n&&n!=0) 34 { 35 cout<<s[n][101][0]<<endl; 36 } 37 return 0; 38 }

 3509 Problem K

1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[1000010],n,res,s[1000010]; 4 int main() 5 { 6 cin>>n; 7 for(int i=1;i<=n;i++) cin>>a[i]; 8 for(int i=1;i<n;i++) 9 { 10 sort(a+1,a+1+n);//每次排序找最小的就可以 11 a[i+1]+=a[i]; 12 res+=a[i+1]; 13 } 14 cout<<res; 15 return 0; 16 }

 


__EOF__

本文作者Sakurajimamai
本文链接https://www.cnblogs.com/o-Sakurajimamai-o/p/17437151.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   o-Sakurajimamai-o  阅读(100)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
-- --
点击右上角即可分享
微信分享提示