1919: D



1919: D

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 342   Solved: 99

Submit Status Web Board

Description

晴天想把一个包含n个整数的序列a分成连续的若干段,且和最大的一段的值最小,但他有强迫症,分的段数不能超过m段,然后他就不会分了。。。他想问你这个分出来的和最大的一段的和最小值是多少?

Input

第一行输入一个整数t,代表有t组测试数据。
每组数据第一行为两个整数n,m分别代表序列的长度和最多可分的段数。
接下来一行包含n个整数表示序列。
0<=n<=50000 1<=m<=n,0<=ai<=10000。

Output

输出一个整数表示和最大的一段的最小值。

Sample Input

13 21 3 5

Sample Output

5

HINT

1 3 5 分成一段可以为1 3 5和为9,分成两段可以为1,3 5或者1 3,5,和最大的一段值分别为8,5,所以答案为5

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[60000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int maxx=-1;
        int sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            maxx=max(a[i],maxx);
            sum+=a[i];
        }
        int l=maxx;
        int r=sum;
        int mid;
        int ans;
        while(r>=l)//所求值一定在maxs和sum之间,从中选出最小的
        {
            mid=(l+r)/2;
            int num=0;
            int cnt=0;
            for(int i=0; i<n; i++)
            {
                if(num+a[i]>mid)
                {
                    num=a[i];
                    cnt++;
                }
                else
                {
                    num=num+a[i];
                }
                if(i==n-1)
                {
                    cnt++;
                }
            }
            if(cnt<=m)//若mid满足分m段最大值为mid,则缩小r,判断是否还有最优解
            {
                ans=mid;
                r=mid-1;
            }
            else//若不满足,则加大l的值
            {
               l=mid+1;
            }
        }
      printf("%d\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以参考以下的代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct DateG{ int yy, mm, dd; }; char leapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } struct DateG _DateG(char *st) { struct DateG d; sscanf(st, "%d-%d-%d", &d.yy, &d.mm, &d.dd); return d; } struct DateG Add(struct DateG x, int y) { int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct DateG d = x; while (y > 0) { int daysInYear = leapYear(d.yy) ? 366 : 365; if (y >= daysInYear) { y -= daysInYear; d.yy++; } else { int daysInMonth = monthDays[d.mm]; if (d.mm == 2 && leapYear(d.yy)) { daysInMonth++; } if (y >= daysInMonth - d.dd + 1) { y -= daysInMonth - d.dd + 1; d.dd = 1; d.mm++; if (d.mm > 12) { d.mm = 1; d.yy++; } } else { d.dd += y; y = 0; } } } return d; } char* Date2string(struct DateG x) { static char str[12]; sprintf(str, "%04d-%02d-%02d", x.yy, x.mm, x.dd); return str; } int Days(struct DateG x) { struct DateG d = {1, 1, 1}; int days = 0; while (d.yy < x.yy || d.mm < x.mm || d.dd < x.dd) { days++; d = Add(d, 1); } return days + 1; } struct DateG days2Date(int x) { struct DateG d = {1, 1, 1}; while (x > 0) { int daysInYear = leapYear(d.yy) ? 366 : 365; if (x >= daysInYear) { x -= daysInYear; d.yy++; } else { int daysInMonth = 0; for (int i = 1; i < d.mm; i++) { daysInMonth += monthDays[i]; } if (d.mm == 2 && leapYear(d.yy)) { daysInMonth++; } if (x >= daysInMonth - d.dd + 1) { x -= daysInMonth - d.dd + 1; d.dd = 1; d.mm++; if (d.mm > 12) { d.mm = 1; d.yy++; } } else { d.dd += x; x = 0; } } } return d; } int main() { char st[12]; scanf("%s", st); struct DateG d1 = _DateG(st), d2 = Add(d1, 60 * 365); printf("%s\n", Date2string(d2)); d2 = Add(d2, -1000); printf("%s\n", Date2string(d2)); d2 = _DateG("2020-1-23"); printf("%.2lf\n", (Days(d2) - Days(d1)) / 365.0); return 0; } ``` 其中,`leapYear`函数用于判断一个年份是否为闰年;`_DateG`函数将字符串类型的日期转换为`DateG`类型;`Add`函数用于计算一个日期加上若干天之后的日期;`Date2string`函数将`DateG`类型的日期转换为字符串类型;`Days`函数计算自公元1年1月1日到指定日期共经历了多少天;`days2Date`函数是`Days`函数的逆函数,用于由自公元1年1月1日历经指定的天数后的日期(返回年月日构成的日期)。 在`main`函数中,我们首先读入一个日期,然后用`Add`函数计算出这个日期加上60年之后的日期,并输出。接着用`Add`函数计算出上一个日期往前推1000天的日期,并输出。最后,我们用`Days`函数计算出两个日期之间相差的天数,除以365得到年数,输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值