贪心分治

所谓贪心,就是总是做出在当前看来是最好的选择,并不是从整体最优考虑。

HRBUST1184  http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1184

先把接水时间按从小到大的顺序排列,这个顺序就是最优顺序,有一个小小的亮点就是运用结构体使得排序后的接水时间在以前数组中的的序号是如何表示的。

 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
",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