lightoj1048 Conquering Keokradong

来源:互联网 发布:什么是java反射机制 编辑:程序博客网 时间:2024/06/08 01:59

这是一个很不错的二分,,,可是wa了半天没做对,主要是二分最大单个区间的值,,,mid,

当a[i] > mid的时候,low++,当cnt > k时,low++;cnt <= k时,high = mid;

然后就是打印比较容易挂掉;注意下就好了。

// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <limits.h>// #define DEBUG#ifdef DEBUG#define debug(...) printf( __VA_ARGS__ )#else#define debug(...)#endif#define MEM(x,y) memset(x, y,sizeof x)using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int a[1010];int n, k;bool check(int x){    // cout << "x = " << x << endl;    int sum = 0,cnt = 0;    for (int i = 0;i <= n;++i){        sum += a[i];        if (sum > x){//sum = 0 || sum > 0;            cnt++;            sum -= a[i];            // printf("sum = %d\t",sum);            if (sum <= 0) return false;//a[i] > x;->low = mid + 1;            sum = a[i];        }    }    // cout << "cnt = " << cnt << endl;    return cnt <= k;//cnt > k,->low = mid + 1;}int solve(int low,int high){    int mid;    while(low < high){        mid = (low + high) / 2;        if (check(mid)) high = mid;//保存结果,,,cnt <= k;        else low = mid + 1;    }    return high;}int main(){        // freopen("in.txt","r",stdin);    // freopen("out.txt","w",stdout);    int t, icase = 0;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&k);        int low = 0,high = 0;        for (int i = 0;i <= n;++i){            scanf("%d",&a[i]);            low = max(low, a[i]);            high += a[i];        }        int ans = solve(low,high);        printf("Case %d: %d\n",++icase,ans);        int tot,i,j;        for (i = 0,j = 0,tot = 0;i <= n;++i){            tot += a[i];            if (tot > ans || k - j > n - i){                tot -= a[i];                printf("%d\n",tot);                tot = a[i];                j++;            }        }        printf("%d\n",tot);    }    return 0;}


0 0
原创粉丝点击