任意门: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. 数据结构(二) --- 伸展树(Splay Tree)

    文章图片和代码来自邓俊辉老师课件 概述 伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由丹尼尔·斯立特Daniel Sleator ...

  2. Reactjs事件处理的三种写法

    目录 前言 1. 在回调函数中使用箭头函数 2. 在构造器中绑定this 3. 使用类字段语法 事件参数的传递. 总结 前言 Reactjs中事件处理,与DOM元素处理类似,但也有一些不同的语法. R ...

  3. eclipse切换workspace后配置问题

    正常情况下如果切换了eclipse的workspace后,需要重新配置eclipse,但是可以将原工作目录中的.metadata/.plugins/org.eclipse.core.runtime拷贝 ...

  4. jvm内置锁synchronized不能被中断

    很久没看技术书籍了,今天看了一下<七周七并发模型>前面两章讲的java,写的还是有深度的.看到了一个有demo,说jvm内置锁synchronized是不能被中断的.照着书上写了个demo ...

  5. JDBC编程错误:Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)

    出现上面的错误是因为连接数据库的用户名不对或密码赋值不对,请对用户名和密码进行检查. 或者在程序中没有获取到正确的用户名或密码.看是否少写了用户名或密码.

  6. JAVA版-微信高清语音.speex转.wav格式

    功能介绍: PC端将.speex文件解码为*.wav文件 使用场景: 在MAC/Linux下Java JNI 调用C Speex,后端Java处理微信服务器下载下来的微信高清语音.speex解码为.w ...

  7. zookeeper学习实践1-实现分布式锁

    引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...

  8. thinkphp怎么把数据库中的列的值存到下拉框中

    1. 先去数据库中查值,查询整个数据表,结果为二维数组. $project = M("project"); $cell = $project->where(array('st ...

  9. GEOS编译

    GEOS是开源的空间运算引擎,最近用到,在这里记录下. 目录 GEOS简介 GEOS编译 一.GEOS简介 GEOS(几何引擎 - 开源)是一个具有完整空间查询和分析功能的C++库.它包括所有Open ...

  10. 【Udacity】机器学习性能评估指标

    评估指标 Evaluation metrics 机器学习性能评估指标 选择合适的指标 分类与回归的不同性能指标 分类的指标(准确率.精确率.召回率和 F 分数) 回归的指标(平均绝对误差和均方误差) ...