【刷水-二分答案】BZOJ1650 & BZOJ1639

BZOJ1650-[Usaco2006 Dec]River Hopscotch 跳石子

【题目大意】

数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次条都从一个石子跳到相邻的下一个石子。现在FJ允许你移走M个石子,问移走这M个石子后,相邻两个石子距离的最小值的最大值是多少。

*0和L也是两块石头!

【思路】

二分最小值检验即可。二分都是套路,然而我写错了,烧 杯 把ub敲成了l,忘记了范围其实是[lb,ub)QAQ

检验部分的套路:last记录上一次跳到的石头,只要当前石头和上一次的石头的距离小于dis,那么就移走这块石头。如果移走的石头>m,就说明不可行。这是目前看来最简洁的写法了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int MAXN=50000+50;
 7 int l,n,m,d[MAXN];
 8 
 9 int check(int dis)
10 /*这种检验方式在贪心中经常使用*/
11 {
12     int last=0,tmp=0;
13     for (int i=1;i<=n;i++)
14     {
15         if (d[i]-d[last]<dis)
16         {
17             tmp++;
18             if (tmp>m) return 0;
19         }
20         else last=i;
21     } 
22     return 1;
23 }
24 
25 void init()
26 {
27     scanf("%d%d%d",&l,&n,&m);
28     for (int i=1;i<=n;i++) scanf("%d",&d[i]);
29     sort(d+1,d+n+1);
30     d[0]=0;
31     d[++n]=l;
32 }
33 
34 void bisearch()
35 {
36     int lb=0,ub=l+1;
37     while (lb+1<ub)
38     {
39         int mid=(lb+ub)>>1;
40         if (check(mid)) lb=mid;
41             else ub=mid;
42     }
43     printf("%d",lb);
44 }
45 
46 int main()
47 {
48     init();
49     bisearch();
50     return 0;
51 }

 

BZOJ1639: [Usaco2007 Mar]Monthly Expense 月度开支

【题目大意】

Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的。他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1<=moneyi<=10,000),他想要为他连续的M(1<=M<=N)个被叫做“清算月”的结帐时期做一个预算,每一个“清算月”包含一个工作日或更多连续的工作日,每一个工作日都仅被包含在一个“清算月”当中。 FJ的目标是安排这些“清算月”,使得每个清算月的花费中最大的那个花费达到最小,从而来决定他的月度支出限制。

【思路】

同上。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int MAXN=100000+50;
 8 int n,m,a[MAXN],lb,ub;
 9 
10 int check(int x)
11 {
12     int tmp=0,t=1;
13     for (int i=1;i<=n;i++)
14     {
15         if (tmp+a[i]<=x) tmp+=a[i];
16             else
17             {
18                 t++;
19                 if (t>m) return 0;
20                 tmp=a[i];
21             }
22     }
23     return 1;
24 }
25 
26 void init()
27 {
28     scanf("%d%d",&n,&m);
29     ub=0,lb=0;
30     for (int i=1;i<=n;i++) scanf("%d",&a[i]),ub+=a[i],lb=max(lb,a[i]-1);
31 }
32 
33 void solve()
34 {
35     while (lb+1<ub)
36     {
37         int mid=(lb+ub)>>1;
38         if (check(mid)) ub=mid;
39             else lb=mid;
40     }
41     printf("%d",ub);
42 }
43 
44 int main()
45 {
46     init();
47     solve();
48     return 0;
49 }

 

posted @ 2016-10-24 23:20  iiyiyi  阅读(474)  评论(0编辑  收藏  举报