AtCoder AGC032D Rotation Sort (DP)
题目链接
https://atcoder.jp/contests/agc032/tasks/agc032_d
题解
又是一道神仙题啊啊啊啊。。。atcoder题真的做不来啊QAQ
第一步又是神仙转化: 对于把第一个挪到最后其他左移这件事情,可以转化为把第一个挪到最后和最后的下一个之间的某个位置(非整数),右移同理。
于是问题就变成了: 有\(N\)个数一开始每个数有个位置,现在可以花\(A\)的代价把一个数往右移到任意位置(不一定非是整数),\(B\)的代价把一个数往左移到任意位置,然后求将它们排序的最小代价和!
这个东西又是一个简单的dp,设\(dp[i][j]\) (\(0\le j\le n\))表示前\(i\)个数排好序,第\(i\)个放在区间\([j,j+1)\)内的最小代价,转移非常容易(注意要对不移动的情况单独考虑),前缀和优化,时间复杂度\(O(N^2)\).
代码
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#include<algorithm>
#define llong long long
using namespace std;
inline int read()
{
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
if(f) return x;
return -x;
}
const int N = 5000;
const llong INF = 10000000000000000ll;
int p[N+3],pp[N+3];
llong dp[N+3][N+5],sdp[N+3][N+5];
int n; llong arga,argb;
void update(llong &x,llong y) {x = x<y?x:y;}
int main()
{
scanf("%d%lld%lld",&n,&arga,&argb);
for(int i=1; i<=n; i++) {scanf("%d",&p[i]); pp[p[i]] = i;}
for(int i=0; i<=n; i++) for(int j=0; j<=n; j++) dp[i][j] = sdp[i][j] = INF;
for(int i=0; i<=n; i++) sdp[0][i] = dp[0][i] = 0ll;
for(int i=1; i<=n; i++)
{
for(int j=0; j<=n; j++)
{
if(j==pp[i]) {update(dp[i][j],sdp[i-1][j-1]);}
llong tmp = sdp[i-1][j]+(j<pp[i]?argb:arga);
update(dp[i][j],tmp);
// printf("dp[%d][%d]=%lld\n",i,j,dp[i][j]);
}
sdp[i][0] = dp[i][0];
for(int j=1; j<=n; j++) sdp[i][j] = min(sdp[i][j-1],dp[i][j]);
}
llong ans = sdp[n][n];
printf("%lld\n",ans);
return 0;
}
AtCoder AGC032D Rotation Sort (DP)的更多相关文章
- AGC032D Rotation Sort
题目传送门 Description 给定\(N\)的排列(\(N\leq5000\)),将任一区间最左侧的数插到该区间最右边的代价为\(A\),将任一区间最右侧的数插到该区间最左边的代价为\(B\), ...
- AT4519-[AGC032D]Rotation Sort【dp】
正题 题目链接:https://www.luogu.com.cn/problem/AT4519 题目大意 给出一个长度为\(n\)的排列,每次可以选择一个区间,然后花费\(A\)的代价向左旋转(最左边 ...
- HDU 1160 FatMouse's Speed (sort + dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体 ...
- AtCoder AGC001E BBQ Hard (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_e 题解: 求\(\sum^n_{i=1}\sum^n_{j=i+1} {A_i+A_j+B ...
- AtCoder AGC004E Salvage Robots (DP)
题目链接 https://atcoder.jp/contests/agc004/tasks/agc004_e 题解 本题的难度不在于想到大体思路,而在于如何把代码写对.. 首先我们可以不让机器人动,让 ...
- AtCoder AGC009E Eternal Average (DP)
题目链接 https://atcoder.jp/contests/agc009/tasks/agc009_e 题解 又被劝退了... 第一步转化非常显然: 就等价于一开始有一个数\(1\), 有\(\ ...
- Atcoder ARC101 E 树dp
https://arc101.contest.atcoder.jp/tasks/arc101_c 题解是也是dp,好像是容斥做的,但是看不懂,而且也好像没讲怎么变n^2,看了写大佬的代码,自己理解了一 ...
- AtCoder AGC002F Leftmost Ball (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f 题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设\(dp[i ...
- hdu 4001 To Miss Our Children Time( sort + DP )
To Miss Our Children Time Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Jav ...
随机推荐
- java的hashCode和equals为什么要同时重写?
原因: java规范:相等的对象必须具有相等的散列码(hashCode) 同时对于HashSet和HashMap这些基于散列值(hash)实现的类.key的判断是通过hashCode完成,且散列也是通 ...
- python多任务——协程的使用
使用yield完成多任务 import time def test1(): while True: print("--1--") time.sleep(0.5) yield Non ...
- jQuery可拖拽旋转的3D图片墙
在线演示 本地下载
- 89. Gray Code (Java)
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- LaTeX的tasks宏包
tasks 宏包 LaTeX的列表(list)通常是将项(item,条目)一个一个垂直的平行显示,所谓"列"表的由来. 水平分列列表,即将多个项分散到各列而不是一列,在出考卷的选择 ...
- elastic 查询
1.match 查询 GET /_index/_search { "query": { "match": { " } } } 1.match 多条件查 ...
- 谈谈我所理解的HashMap和HashTable
HashMap和HashTable之间的联系和区别如下: 1.HashMap几乎可以等价于Hashtable,但是它们之间继承不同:HashMap extends AbstractMap implem ...
- 详解thinkphp+redis+队列的实现代码
1,安装Redis,根据自己的PHP版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbinary.dll,php_redis.dll扩展此处需要注意你的php版本如图: ...
- Django学习系列15:把POST请求中的数据存入数据库
要修改针对首页中的POST请求的测试.希望视图把新添加的待办事项存入数据库,而不是直接传给响应. 为了测试这个操作,要在现有的测试方法test_can_save_a_post_request中添加3行 ...
- Java8中重要的收集器Collector
Collector介绍 Java8的stream api能很方便我们对数据进行统计分类等工作,函数式编程的风格让我们方便并且直观地编写统计代码. 例如: Stream<Integer> s ...