周结2020.5.10

这周还是做了些二分三分的题,都是套路,这周也没有新的训练就自己找了些信息学奥赛一本通上的题,这周的cf打了两场,周三那场醉了,第一次感觉能上分了,没想到网站蹦了,昨晚的dive 4的前三题很简单,不过后面几题也没做,但终于成绩反弹了

1436:数列分段II

时间限制: 1000 ms 内存限制: 65536 KB
提交数: 1346 通过数: 627
【题目描述】
对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小。

关于最大值最小:

例如一数列4 2 4 5 1要分成3段

将其如下分段:

[4 2][4 5][1]

第一段和为6,第2段和为9,第3段和为1,和最大值为9。

将其如下分段:

[4][2 4][5 1]

第一段和为4,第2段和为6,第3段和为6,和最大值为6。

并且无论如何分段,最大值不会小于6。

所以可以得到要将数列4 2 4 5 1要分成3段,每段和的最大值最小为6。

【输入】
第1行包含两个正整数N,M,第2行包含N个空格隔开的非负整数A[i],含义如题目所述。

【输出】
仅包含一个正整数,即每段和最大值最小为多少。

【输入样例】
5 3
4 2 4 5 1
【输出样例】
6

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<functional>

using namespace std;

int n,m,ans,l=0,r=0;
int a[100001];
int sum[100001]; 

int check(int x)
{
    int cnt=1,now=0;
    for(int i=1;i<=n;i++)
    {
        if(now+a[i]<=x) now+=a[i];
        else
        {
            cnt++;
            now=a[i];
        }
    }
    return cnt<=m;
}

int main()
{
     scanf("%d%d",&n,&m);
     for(int i=1;i<=n;i++)
     {
         scanf("%d",&a[i]);
         l=max(l,a[i]);
         r+=a[i];
     }
     
     while(l<=r)
     {
         int mid=(r+l)/2;
         if(check(mid)) 
         {
             ans=mid;
             r=mid-1;
        }
        else l=mid+1;
     }
       
    printf("%d",ans);
    
}

Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance before he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input
Line 1: Three space-separated integers: L, N, and M
Lines 2… N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.
Output
Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int M=100001;
bool cmp(int a,int b)
{
    return a<b;
}
long long a[M];
int main()
{
    long long len;
    long long left,mid,right;
    int n,m,num,last;
    while(cin>>len>>n>>m)
    {
        left=1;
        right=len;
        a[0]=0;
        a[n+1]=len;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        sort(a,a+n+2,cmp);
        while(left<=right)
        {
            num=0;last=0;
            mid=(left+right)/2;
            for(int i=1;i<=n;i++)
            {
                if(a[i]-last<mid)
                    num++;
                else
                    last=a[i];
            }
            if(num<=m)
                left=mid+1;
            else
                right=mid-1;
        }
        cout<<right<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值