码迷,mamicode.com
首页 > 其他好文 > 详细

集训手册贪心题练习题

时间:2014-09-26 01:54:18      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   for   sp   div   on   

HDU1009:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 #define N 1005
 6 
 7 struct Room{
 8     int j,f;
 9     bool operator<(const Room &m)const{
10         double rate1 = j*1.0/f;
11         double rate2 = m.j*1.0/m.f;
12         return rate1>rate2;
13     }
14 }room[N];
15 
16 int main()
17 {
18     int n,m;
19     while(~scanf("%d%d",&n,&m)){
20         if(n==-1&&m==-1)
21             break;
22 
23         for(int i=0;i<m;i++){
24             scanf("%d%d",&room[i].j,&room[i].f);
25         }
26         sort(room+0,room+m);
27 
28         double ans = 0;
29         for(int i=0;i<m;i++){
30             if(n>room[i].f)
31             {
32                 ans+=room[i].j;
33                 n-=room[i].f;
34             }
35             else{
36                 ans+= n*1.0/room[i].f * room[i].j;
37                 break;
38             }
39         }
40         printf("%.3f\n",ans);
41     }
42     return 0;
43 }

 HUD 1050

 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 #define MAX 201
 6 int f(int n){
 7     return (n+1)/2;
 8 }
 9 int a[MAX];
10 int main(){
11     int m,n,x,i,j,t,maxm,times,g;
12     cin>>times;
13     for(g=0;g<times;g++){
14         cin>>x;
15         for(i=0;i<x;i++){
16             cin>>m>>n;
17             if(m>n){
18                 t=m;m=n;n=t;
19             }
20             for(j=f(m);j<=f(n);j++) a[j]++;
21         }
22         maxm=0;
23         for(i=1;i<MAX;i++){
24             if(a[i]>maxm) maxm=a[i];
25         }
26         cout<<10*maxm<<endl;
27         memset(a,0,sizeof(a));
28     }
29     return 0;
30 }

HDU 1587
这道题貌似水的过头了、、、每种花数量足够,那么只要找最小值,只买那么一种类型的花就行了,排序都是浪费

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 int price[1000005];
 5 
 6 int main()
 7 {
 8     int n,m;
 9     while(~scanf("%d%d",&n,&m)){
10         for(int i=0;i<n;i++)
11             scanf("%d",price+i);
12 
13         std::sort(price,price+n);
14 
15         int ans = m/price[0];
16 
17         printf("%d\n",ans);
18     }
19 }

 

集训手册贪心题练习题

标签:style   blog   color   io   os   for   sp   div   on   

原文地址:http://www.cnblogs.com/CSU3901130321/p/3993970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!