HDU - 5933(贪心?)

题意

给个长度为n的序列,你有两种操作,一种是合并相邻的两个数,一种是把一个数划分为两个数。现在想要把这些数平均分为m个,问最小的划分次数。

思路

贪心的划分就行,模拟题意的过程就行。两个合并时要判断一下st是不是为0,不为0cnt才加1。如果大于平均数,判断一下是不是st%temp==0。。。
大水题敲了好久。。

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long ll;
const int maxn = 1e5+5;
const int mod = 1e9+9;
int Case = 1, n, m;
ll cc[maxn], T = 0;
void solve(){
	scanf("%d%d", &n, &m);
	ll sum = 0;
	for(int i = 1; i <= n; i++) {
		scanf("%lld", &cc[i]);
		sum += cc[i];
	}
	if(sum%m) {printf("Case #%lld: -1\n", ++T);return;}
	ll cnt = 0, temp = sum/m, st = 0;
	for(int i = 1; i <= n; i++) {
		if(st) st += cc[i], cnt++;
		else st += cc[i];
		if(st>=temp) {
			cnt += st/temp;
			st = st%temp;
			if(!st) cnt--;
		}
	}
	printf("Case #%lld: %lld\n", ++T, cnt);
}

int main() {
    //ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("/Users/hannibal_lecter/Desktop/code/in.txt", "r", stdin);
    //freopen("/Users/hannibal_lecter/Desktop/code/out.txt","w",stdout);
#endif
	scanf("%d", &Case);
    while(Case--) {
        solve();
    }
    return 0;
}
全部评论

相关推荐

投递完美世界等公司6个岗位 >
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务