题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列)。设第i个序列的各数之和为S(i),你的任务是让所有的S(i)的最大值尽量小。如果有多解,S(1)应尽量小,如果仍有多解,S(2)应尽量小,依此类推。

分析:

1、二分最小值x。

2、判断当前x是否满足条件时,从右往左尽量划分,若cnt<k,则从0开始依次标为分界点,这样可满足S(1),S(2),……,尽量小。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 500 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL a[MAXN];
int m, k;
int cnt;
int vis[MAXN];//分界点
bool judge(LL x){
memset(vis, 0, sizeof vis);
cnt = 0;
int pos = m - 1;
while(pos >= 0){
int num = 0;//当前序列中的元素
LL sum = 0;
while(pos >= 0 && sum + a[pos] <= x){
sum += a[pos];
++num;
--pos;
}
if(!num) return false;//若当前序列中没有元素,说明x太小
if(pos >= 0) vis[pos] = 1;
++cnt;
}
if(cnt > k) return false;
return true;
}
void solve(){
LL l = 0, r = 1e15;
while(l < r){
LL mid = l + (r - l) / 2;
if(judge(mid)) r = mid;
else l = mid + 1;
}
if(judge(r)){
for(int i = 0; i < m - 1 && cnt < k; ++i){
if(!vis[i]){
vis[i] = 1;
++cnt;
}
}
for(int i = 0; i < m; ++i){
if(i) printf(" ");
printf("%lld", a[i]);
if(vis[i]) printf(" /");
}
printf("\n");
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &m, &k);
for(int i = 0; i < m; ++i){
scanf("%lld", &a[i]);
}
solve();
}
return 0;
}

  

最新文章

  1. Elasticsearch——Date Math在索引中的用法详解
  2. 前端网址收集!Amazing! 神奇!
  3. SQLServer数据库的一些全局变量
  4. Hibernate笔记——Hibernate介绍和初次环境配置
  5. 对Java不能多继承,只能单继承,却可以实现多个接口的理解
  6. mysql kill操作
  7. Linux下的TUN/TAP编程
  8. java生成随机字符串
  9. QT调用CURL
  10. 流动python - 什么是魔术方法(magic method)
  11. iOS UIWebView键盘操控
  12. 杭电OJ--自行车计速器
  13. Cocos Creator 动态改变sprite图片
  14. 操作dom获取datatable中的某一行的某一列的数据
  15. JavaScript中的label语句,及应用
  16. sql 获取批处理信息的脚本(优化器在处理批处理时所发生的优化器事件)
  17. Found an unexpected Mach-O header code: 0x72613c21
  18. js 排序
  19. Nutch 使用总结
  20. poj1185 炮兵阵地【状压DP】

热门文章

  1. 大公司病,为什么要PPT背锅?
  2. [Educational Codeforces Round 81 (Rated for Div. 2)]E. Permutation Separation(线段树,思维,前缀和)
  3. JAVA虚拟机:Java技术体系讲解(一)
  4. swoole之建立 http server
  5. 今日份学习:初步的springboot
  6. 2.24 模拟赛 + DIV2 总结
  7. swing开发图形界面工具配置(可自由拖控件上去)
  8. [Codeforces] #603 (Div. 2) A-E题解
  9. 【转】Spark Streaming 实时计算在甜橙金融监控系统中的应用及优化
  10. E. MaratonIME does (not do) PAs