Codeforces 590D Top Secret Task
3 seconds
256 megabytes
standard input
standard output
A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The problem is that Zuev's base is missing this top-secret troop for some reasons.
The colonel decided to deal with the problem immediately and ordered to line up in a single line all n soldiers of the base entrusted to him. Zuev knows that the loquacity of the i-th soldier from the left is equal to qi. Zuev wants to form the top-secret troop using k leftmost soldiers in the line, thus he wants their total loquacity to be as small as possible (as the troop should remain top-secret). To achieve this, he is going to choose a pair of consecutive soldiers and swap them. He intends to do so no more than s times. Note that any soldier can be a participant of such swaps for any number of times. The problem turned out to be unusual, and colonel Zuev asked you to help.
Determine, what is the minimum total loquacity of the first k soldiers in the line, that can be achieved by performing no more than s swaps of two consecutive soldiers.
The first line of the input contains three positive integers n, k, s (1 ≤ k ≤ n ≤ 150, 1 ≤ s ≤ 109) — the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively.
The second line of the input contains n integer qi (1 ≤ qi ≤ 1 000 000) — the values of loquacity of soldiers in order they follow in line from left to right.
Print a single integer — the minimum possible total loquacity of the top-secret troop.
3 2 2
2 4 1
3
5 4 2
10 1 6 2 5
18
5 2 3
3 1 4 2 5
3
In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order:
- (10, 1, 6 — 2, 5)
- (10, 1, 2, 6 — 5)
The resulting soldiers order is (10, 1, 2, 5, 6).
Minimum possible summary loquacity is equal to 18.
题意
给n个数,至多进行s次相邻元素交换的操作,问前k个元素的最小和是多少?
参考:http://blog.csdn.net/Z_Mendez/article/details/49512305
分析
设状态dp[i][j]为前i个数交换了j次的最小和。转移方程为dp[i+1][j+l-(i+1)]=min(dp[i+1][j+l-(i+1)],dp[i][j]+a[l]).为了没有后效性,先从小到大枚举l,表示从哪里交换过来,然后从大到小枚举i,避免数被重复添加到前k个里。由冒泡排序可知,n个元素的最多交换次数为n*(n-1),即使s很大,也并不会超时。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
using namespace std;
typedef long long LL;
const int maxn = 5e5+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1 int a[],dp[][*]; int main(){
int n,k,s;
scanf("%d%d%d",&n,&k,&s);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
ms(dp,inf);
dp[][]=;
for(int l=;l<=n;l++){
for(int i=l-;i>=;i--){
for(int j=;j<=i*l;j++){
dp[i+][j+l-(i+)]=min(dp[i+][j+l-(i+)],dp[i][j]+a[l]);
}
}
}
int ans=inf;
for(int i=;i<=min(s,n*n);i++) ans=min(dp[k][i],ans);
cout<<ans<<endl;
return ;
}
Codeforces 590D Top Secret Task的更多相关文章
- Codeforces Round #327 (Div. 1) D. Top Secret Task
D. Top Secret Task time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 555D Case of a Top Secret
Case of a Top Secret 感觉除了两个点在那循环的部分, 其他时候绳子的长度每次变为一半一下, 就变成了Log(l)步.. 然后就暴力找就好啦, 循环的部分取个模. #include& ...
- 计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
E. A Simple Task Problem's Link: http://codeforces.com/problemset/problem/558/E Mean: 给定一个字符串,有q次操作, ...
- codeforces 70D Professor's task(动态二维凸包)
题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...
- Codeforces 558E A Simple Task (计数排序&&线段树优化)
题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...
- Codeforces C. A Simple Task(状态压缩dp)
题目描述: A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- 【SRM-06 D】五色战队&&【codeforces 788E】 New task
原题链接:788E - New task Description 游行寺家里人们的发色多种多样,有基佬紫.原谅绿.少女粉.高级黑.相簿白等. 日向彼方:吾令人观其气,气成五彩,此天子气也. 琉璃:我们 ...
- CodeForces 588E A Simple Task(线段树)
This task is very simple. Given a string S of length n and q queries each query is on the format i j ...
随机推荐
- 牛客OI周赛7-普及组
https://ac.nowcoder.com/acm/contest/372#question A.救救猫咪 #include <bits/stdc++.h> using namespa ...
- PAT 1019 数字黑洞
https://pintia.cn/problem-sets/994805260223102976/problems/994805302786899968 给定任一个各位数字不完全相同的4位正整数,如 ...
- Raphaël - JavaScript Vector Library
Raphaël http://dmitrybaranovskiy.github.io/raphael/ // ┌──────────────────────────────────────────── ...
- Jfrog Maven jenkins pipeline 流水线 培训 简单实验
1. 公司购买了一套jfrog artifactory ,然后厂商组织了一次培训 本次简单记录一下 jenkins和jfrog 二进制仓库的简单连接使用 2. 前期环境准备. scp jdk的tar包 ...
- [转帖]GitHub上整理的一些工具
GitHub上整理的一些工具 技术站点 Hacker News:非常棒的针对编程的链接聚合网站 Programming reddit:同上 MSDN:微软相关的官方技术集中地,主要是文档类 inf ...
- Mordern Effective C++ --auto
5. 优先使用auto而非显示类型声明 在C++之中,使用auto关键字声明类型可以将程序员从输入繁琐的类型中解放出来,编译器会自动推导出变量的实际类型. template<typename I ...
- ubuntu美化 mac风格
安装tweak sudo apt install gnome-tweak-tool sudo apt install chrome-gnome-shell https://extensions.gno ...
- ubuntu默认壁纸位置
usr/share/backgrounds和usr/share/wallpapers
- oracle小知识点
一 . procedure和function: procedure和function在语法上几乎完全一样,使用上却有小小的差别, procedure可以单独的调用 在命令行直接exec pro_xxx ...
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...