CF#538(div2) B. Yet Another Array Partitioning Task 【YY】
任意门:http://codeforces.com/contest/1114/problem/B
B. Yet Another Array Partitioning Task
2 seconds
256 megabytes
standard input
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.
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).
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.
9 2 3
5 2 5 2 4 1 1 3 2
21
3 5
6 1 4
4 1 3 2 2 3
12
1 3 5
2 1 2
-1000000000 1000000000
0
1
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】的更多相关文章
- Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏【转】
Java虚拟机性能管理神器 - VisualVM(7) 排查JAVA应用程序线程泄漏[转] 标签: javajvm线程泄漏 2015-03-11 19:47 1098人阅读 评论(0) 收藏 举报 ...
- Java虚拟机性能管理神器 - VisualVM(6) 排查JAVA应用程序内存泄漏【转】
Java虚拟机性能管理神器 - VisualVM(6) 排查JAVA应用程序内存泄漏[转] 标签: javajvm内存泄漏监控工具 2015-03-11 18:30 1870人阅读 评论(0) 收藏 ...
- Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁【转】
Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁[转] 标签: javajvm监控工具性能优化 2015-03-11 19:59 1948人阅读 评论(0) 收藏 ...
- 机器人操作系统(ROS)教程4:ROS的框架【转】
转自:http://www.arduino.cn/thread-11351-1-1.html 在进行ROS的代码开发前,有必要了解一些ROS的概念.首先,ROS的系统代码分为两部分:main和univ ...
- linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-119723.html linux设备驱动归纳总结(十三):1.触摸屏与ADC时钟 xxxxxxxxxx ...
- inux设备驱动归纳总结(五):2.操作硬件——IO内存【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-80627.html inux设备驱动归纳总结(五):2.操作硬件——IO内存 xxxxxxxxxxxx ...
- linux设备驱动归纳总结(五):1.在内核空间分配内存【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-79134.html linux设备驱动归纳总结(五):1.在内核空间分配内存 xxxxxxxxxxxx ...
- linux设备驱动归纳总结(四):1.进程管理的相关概念【转】
本文转载自;http://blog.chinaunix.net/uid-25014876-id-64866.html linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxx ...
- linux设备驱动归纳总结(三):5.阻塞型IO实现【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-60025.html linux设备驱动归纳总结(三):5.阻塞型IO实现 xxxxxxxxxxxxxx ...
随机推荐
- [转]Oracle中没有 if exists(...)
本文转自:http://blog.csdn.net/hollboy/article/details/7550171 对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法, ...
- C#中去除字符串里的多个空格且保留一个空格
static void Main(string[] args) { // 首先定义一个名为str 的字符串 string str="2 3 4 保留一个空格 ss ...
- 001.開始使用ASP.NET Web API 2(一)
原文鏈接:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web ...
- 使用OpenSSL(Windows x64版)将pem格式证书转换为p12格式
今天同事遇到一个问题,他获得的证书只有pem格式,而服务器要求提交p12格式,一时搞不定,来找我帮忙. 我之前也从未接触过证书类型的转换,所以上网大致搜索了一下,又亲自动手试了试,现将有关心得经验记录 ...
- 二、spring-boot-devtools热部署
springboot提供了热部署,需要添加依赖: <dependency> <groupId> org.springframework.boot</groupId> ...
- 互联网轻量级框架SSM-查缺补漏第八天(MyBatis插件plugin使用及原理)
简言:今天进行第八天的记录(只是写了八天).有的时候看的多,有的时候看的少,看的少的时候就攒几天一起写了.而今天这个插件我昨天写了一下午,下班没写完就回去了,今天把尾收了,再加上一个过程图方便下面原理 ...
- mysql 的用法
SELECT CASEWHEN //当count(userId) = 0的时候 让其为null 不然报警告 // count(DISTINCT userId) 不用处理 count(userId ...
- 修改vue的配置项支持生产环境下二级目录访问的方法
本文主要记录如何配置vue的打包文件配置项,使打包后的文件可以支持二级目录的访问. 1.常规打包 在实际的项目中,我们通常都使用 npm run build 直接打包文件后丢到服务器上访问 打包后的文 ...
- C语言——顺序表插入、删除、定位运算算法
说明:将元素x插入到顺序表L的第i个数据元素之前,这个i是从1开始的,但是程序中数组都是从0算起的,不要混淆了. 头文件: header.h // 顺序表的结构定义 #define Maxsize 1 ...
- 22_CopyOnWrite容器
[简述] Copy-On-Write简称COW,是一种程序设计中的优化策略. JDK里的COW容器分为两种:CopyOnWriteArrayList 和 CopyOnWriteArraySet. Co ...