任意门:http://codeforces.com/contest/1114/problem/B

B. Yet Another Array Partitioning Task

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

An array bb is called to be a subarray of aa if it forms a continuous subsequence of aa, that is, if it is equal to alal, al+1al+1, ……, arar for some l,rl,r.

Suppose mm is some known constant. For any array, having mm or more elements, let's define it's beauty as the sum of mm largest elements of that array. For example:

  • For array x=[4,3,1,5,2]x=[4,3,1,5,2] and m=3m=3, the 33 largest elements of xx are 55, 44 and 33, so the beauty of xx is 5+4+3=125+4+3=12.
  • For array x=[10,10,10]x=[10,10,10] and m=2m=2, the beauty of xx is 10+10=2010+10=20.

You are given an array a1,a2,…,ana1,a2,…,an, the value of the said constant mm and an integer kk. Your need to split the array aa into exactly kksubarrays such that:

  • Each element from aa belongs to exactly one subarray.
  • Each subarray has at least mm elements.
  • The sum of all beauties of kk subarrays is maximum possible.
Input

The first line contains three integers nn, mm and kk (2≤n≤2⋅1052≤n≤2⋅105, 1≤m1≤m, 2≤k2≤k, m⋅k≤nm⋅k≤n) — the number of elements in aa, the constant mm in the definition of beauty and the number of subarrays to split to.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109).

Output

In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.

In the second line, print k−1k−1 integers p1,p2,…,pk−1p1,p2,…,pk−1 (1≤p1<p2<…<pk−1<n1≤p1<p2<…<pk−1<n) representing the partition of the array, in which:

  • All elements with indices from 11 to p1p1 belong to the first subarray.
  • All elements with indices from p1+1p1+1 to p2p2 belong to the second subarray.
  • …….
  • All elements with indices from pk−1+1pk−1+1 to nn belong to the last, kk-th subarray.

If there are several optimal partitions, print any of them.

Examples
input

Copy
9 2 3
5 2 5 2 4 1 1 3 2
output

Copy
21
3 5
input

Copy
6 1 4
4 1 3 2 2 3
output

Copy
12
1 3 5
input

Copy
2 1 2
-1000000000 1000000000
output

Copy
0
1
Note

In the first example, one of the optimal partitions is [5,2,5][5,2,5], [2,4][2,4], [1,1,3,2][1,1,3,2].

  • The beauty of the subarray [5,2,5][5,2,5] is 5+5=105+5=10.
  • The beauty of the subarray [2,4][2,4] is 2+4=62+4=6.
  • The beauty of the subarray [1,1,3,2][1,1,3,2] is 3+2=53+2=5.

The sum of their beauties is 10+6+5=2110+6+5=21.

In the second example, one optimal partition is [4][4], [1,3][1,3], [2,2][2,2], [3][3].

题意概括:

将长度为 N 的数串分成 K 段,每段取 M 大值,要求每段 M 大值最后相加的总和最大,求分段方案,并输出断点。

解题思路:

暴力,最优的方案当然是选出 M*K 个的值都是最大啦,先降序排序 取 前 M*K 个。

暴力扫一遍原序列,如果遇到 这些前 M*K 大的值就取,取到 M 个就分段。

到这里好像没毛病....然而 wa。

有一个细节遗漏了,就是第 M*K 大的值如果不止一个,而是有多个,但这些值按照排序并为在前 M*K 中,但在原序列里,这些值处在比他们大得值前面,那么我们扫描原序列时就会把这些值加进去,而有可能省略了后面更大的值,这个方案肯定不是最优的。

所以第 M*K 个值需要特判!

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 2e5+;
int num[MAXN];
int numa[MAXN];
int cnt, anum;
int ans[MAXN], nn;
int N, M, K;
map<int, bool>mmp;
bool cmp(int a, int b)
{
return a > b;
} int main()
{
scanf("%d %d %d", &N, &M, &K);
for(int i = ; i <= N; i++){
scanf("%d", &num[i]);
numa[cnt++] = num[i];
//num[i].no = i;
}
LL ans_sum = 0LL;
sort(numa, numa+cnt, cmp);
for(int i = ; i < M*K; i++){
ans_sum += numa[i];
//mmp[numa[i]] = true;
//printf("%d\n", numa[i]);
}
LL lst = numa[M*K-];
//printf("lst:%d\n", lst);
int j = M*K-;
while(numa[j] == lst && j >= ){
anum++;
j--;
} int tt = ;
for(int i = ; i <= N; i++){
if(num[i] > lst){
tt++;
if(tt == M){
ans[nn++] = i;
tt = ;
}
}
else if(num[i] == lst && anum){
anum--;
tt++;
if(tt == M){
ans[nn++] = i;
tt = ;
}
}
} printf("%I64d\n", ans_sum);
for(int i = ; i < nn-; i++){
printf("%d ", ans[i]);
}
puts("");
return ;
}

CF#538(div2) B. Yet Another Array Partitioning Task 【YY】的更多相关文章

  1. Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏【转】

    Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏[转] 标签: javajvm线程泄漏 2015-03-11 19:47 1098人阅读 评论(0) 收藏 举报   ...

  2. Java虚拟机性能管理神器 - VisualVM(6) 排查JAVA应用程序内存泄漏【转】

    Java虚拟机性能管理神器 - VisualVM(6) 排查JAVA应用程序内存泄漏[转] 标签: javajvm内存泄漏监控工具 2015-03-11 18:30 1870人阅读 评论(0) 收藏  ...

  3. Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁【转】

    Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁[转] 标签: javajvm监控工具性能优化 2015-03-11 19:59 1948人阅读 评论(0) 收藏  ...

  4. 机器人操作系统(ROS)教程4:ROS的框架【转】

    转自:http://www.arduino.cn/thread-11351-1-1.html 在进行ROS的代码开发前,有必要了解一些ROS的概念.首先,ROS的系统代码分为两部分:main和univ ...

  5. linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-119723.html linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟 xxxxxxxxxx ...

  6. inux设备驱动归纳总结(五):2.操作硬件——IO内存【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-80627.html inux设备驱动归纳总结(五):2.操作硬件——IO内存 xxxxxxxxxxxx ...

  7. linux设备驱动归纳总结(五):1.在内核空间分配内存【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-79134.html linux设备驱动归纳总结(五):1.在内核空间分配内存 xxxxxxxxxxxx ...

  8. linux设备驱动归纳总结(四):1.进程管理的相关概念【转】

    本文转载自;http://blog.chinaunix.net/uid-25014876-id-64866.html linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxx ...

  9. linux设备驱动归纳总结(三):5.阻塞型IO实现【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-60025.html linux设备驱动归纳总结(三):5.阻塞型IO实现 xxxxxxxxxxxxxx ...

随机推荐

  1. windows下查看 mysql二进制日志文件

    有时候需要将linux中的mysql从线上linux种down到windows查看,但是这种binlog日志是二进制的,应该怎么查看呢? 使用window上的mysqlbinlog.exe将其转码到另 ...

  2. 多线程FTP下载日志脚本

    #!/bin/bash ip_list=`cat $1` thead_num=5tmp_fifofile="/tmp/$$.fifo"mkfifo "$tmp_fifof ...

  3. 一:Redis安装

    redis在Linux上的安装 1)安装redis编译的c环境,yum install gcc-c++ 2)将redis-2.6.16.tar.gz上传到Linux系统中 3)解压到/usr/loca ...

  4. Nginx 503错误总结

    nginx 503错误(Service Temporarily Unavailable  服务暂时不可用): 503是一种HTTP状态码,由于临时的服务器维护或者过载,服务器当前无法处理请求.这个状况 ...

  5. php 正则验证

      PHP 正则验证字符串是否为数字 方法一: php中利用正则表达式验证字符串是否为数字一件非常容易的事情,最主要的是如何写好正则表达式以及掌握正则表达式的写法,在此利用正则表达式的方式来列举一下判 ...

  6. css3打包后自动追加前缀插件:autoprefixer

    用vue-cli构建的项目脚手架已经帮你把autoprefixer的配置做好了,自己不需要做什么改动就会自动加前缀: 下面一起看看涉及到autoprefixer这个插件的一些配置: 1,postcss ...

  7. jQuery数据缓存$.data 的使用以及源码解析

    一.实现原理: 对于DOM元素,通过分配一个唯一的关联id把DOM元素和该DOM元素的数据缓存对象关联起来,关联id被附加到以jQuery.expando的值命名的属性上,数据存储在全局缓存对象jQu ...

  8. Algorithm——最长公共前缀

    一.问题 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...

  9. 类中调用界面ActiveX控件报错当前线程不在单线程单元中因此无法实例化 ActiveX 控件的解决办法

    解决办法是Form类中定义一个静态的ActiveX对象,在formload中将界面上的ActiveX对象赋值给新定义的对象,类中访问该静态对象即可. public static AxClientDri ...

  10. VS code 自定义快捷输入

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 位置 ctrl+shift+p 搜索: snippets 输入类型: 比如 ...