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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值