wenbao与贪心

说起贪心无非就是从大到小或者从小到大,重点是知道从哪个地方进行贪心

喷泉覆盖问题

http://acm.nyist.net/JudgeOnline/problem.php?pid=12

如果要从坐标或者是半径贪心显然太麻烦,不如换个思路,枚举出每个圆与上边界的交点,依次贪心。。。。(从左边满足覆盖的前提下找到能够达到的最右边)

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 using namespace std;
 5 const int  maxn = 1e3+10;
 6 int main(){
 7     int t;
 8     cin>>t;
 9     while(t--){
10         int n, flag = 0, num = 0;
11         double w, h, a, b, H, x, y;
12         pair<double, double> p[maxn];
13         cin>>n>>w>>h;
14         H = h/2;
15         int t = 0;
16         while(n--){
17             cin>>a>>b;
18             if(b > H){
19                 x = a - sqrt(pow(b,2) - pow(H,2));
20                 y = a + sqrt(pow(b, 2) - pow(H, 2));
21                 if(!((x < 0 && y < 0) || (x > w && y > w))){
22                     p[t].first = x;
23                     p[t].second = y;
24                     //cout<< x << y <<endl;
25                     t++;
26                 }
27             }
28         }
29         sort(p, p+t);
30         int m = 0;
31         double l = 0, r = 0;
32         while( l < w){
33             for(int i = m; i < t; i++){
34                 //cout<<"***************"<<endl;
35                 if(p[i].first <= l ){
36                     if(p[i].second > r)  m = i, r = p[i].second;
37                 }
38                 else break;
39             }    
40             num++;
41             l = r;
42             if( num > t+1) break;
43             //cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@"<<num<<endl;
44         }
45         if(num > t+1) cout<<0<<endl;
46         else{
47             cout<<num<<endl;
48         }
49         //cout<<num<<endl;
50     }
51     return 0;
52 }

 

 

 

@南阳理工oj

  过桥问题:http://acm.nyist.net/JudgeOnline/problem.php?pid=47

  n个人一个手电筒,求最短的过桥时间

 1 #include <stdio.h>
 2 #include <algorithm>
 3 using namespace std;
 4 int a[1009], n, t;
 5 int q(int x){
 6     if(x <= 2) return max(a[0], a[1]);
 7     else if(x == 3) return a[0] + a[1] + a[2];
 8     else{
 9         if(2*a[1] < a[0]+a[x-2]) return q(x-2) + a[0] + 2*a[1] + a[x-1];
10         else return q(x-2) + 2*a[0] + a[x-1] + a[x-2];
11     } 
12 }
13 int main(){
14     scanf("%d", &t);
15     while(t--){
16         scanf("%d", &n);
17         for(int i = 0; i < n; i++) scanf("%d", &a[i]);
18         sort(a, a+n); printf("%d\n",q(n));
19     }
20     return 0;
21 }
22         

 

 

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:52  wenbao  阅读(95)  评论(0编辑  收藏  举报