贪心分治
所謂貪心,就是總是做出在當前看來是最好的選擇,并不是從整體最優(yōu)考慮。
HRBUST1184 ?http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1184
先把接水時間按從小到大的順序排列,這個順序就是最優(yōu)順序,有一個小小的亮點就是運用結構體使得排序后的接水時間在以前數(shù)組中的的序號是如何表示的。
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 #include<string.h> 5 using namespace std; 6 struct node{ 7 int id,t; 8 }ss[100005]; 9 bool cmp(node x,node y){ 10 if(x.t==y.t)//要注意這個if語句,要是沒有這個的話是AC不了的 11 { 12 return x.id<y.id; 13 } 14 return x.t<y.t; 15 } 16 int main(){ 17 int m,n;//double a[100005]; 18 while(~scanf("%d",&n)){ 19 memset(ss,0,sizeof(ss)); 20 for(int i=0;i<n;i++){ 21 ss[i].id=i; 22 cin>>ss[i].t; 23 } 24 sort(ss,ss+n,cmp); 25 for(int i=0;i<n;i++) 26 {if(i==0) 27 cout<<ss[i].id+1; 28 else cout<<" "<<ss[i].id+1;; 29 } 30 cout<<endl; 31 double sum=0;double ans=0; 32 for(int i=0;i<n;i++){ 33 ans+=sum; 34 sum+=ss[i].t; 35 } 36 double aa=ans/n; 37 printf("%.2lf\n",aa); 38 } 39 } View Code?分治
所謂分治,即分而治之,就是把原來的大問題分割成小問題,再用遞歸的方法解決
HDU2035 ?http://acm.hdu.edu.cn/showproblem.php?pid=2035
1 #include<stdio.h> 2 #include<iostream> 3 #include<algorithm> 4 using namespace std; 5 int ss(int a,int b) 6 { 7 if(b==0) 8 return 1; 9 else if(b%2==0){ 10 int ans=ss(a,b/2); 11 return (ans*ans)%1000; 12 } 13 else return (a*ss(a,b-1))%1000; 14 15 } 16 int main() 17 { 18 int a,b; 19 while(~scanf("%d%d",&a,&b)&&(a+b)) 20 { 21 int ans=ss(a,b); 22 cout<<ans<<endl; 23 } 24 } View Code?
轉載于:https://www.cnblogs.com/shangjindexiaoqingnian/p/5722576.html
總結
- 上一篇: 数据库系统概念学习笔记2
- 下一篇: 哪位有方法把 dd/mm/yyyy的字