【CodeForces - 612F】Simba on the Circle

2023-10-15 00:50
文章标签 codeforces circle 612f simba

本文主要是介绍【CodeForces - 612F】Simba on the Circle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@Simba on the Circle@

  • @Simba on the Circle@
    • @前排提醒@
    • @题目描述 - English@
    • @题目大意@
    • @分析@
    • @方案@
    • @代码@
    • @ENDING@


@前排提醒@

我的博客太渣了所以大家还是先看看yhn大佬的博客好了
膜拜yhn大佬orz!


@题目描述 - English@

You are given a circular array with n elements. The elements are numbered from some element with values from 1 to n in clockwise order. The i-th cell contains the value ai. The robot Simba is in cell s.

Each moment of time the robot is in some of the n cells (at the begin he is in s). In one turn the robot can write out the number written in current cell or move to the adjacent cell in clockwise or counterclockwise direction. To write out the number from the cell Simba doesn’t spend any time, but to move to adjacent cell Simba spends one unit of time.

Simba wants to write the number from each cell one time, so the numbers will be written in a non decreasing order. Find the least number of time units to write out all numbers.

Input
The first line contains two integers n and s (1 ≤ s ≤ n ≤ 2000) — the number of cells in the circular array and the starting position of Simba.
The second line contains n integers ai (  109 − 10 9  ≤ ai ≤  109 10 9 ) — the number written in the i-th cell. The numbers are given for cells in order from 1 to n. Some of numbers ai can be equal.

Output
In the first line print the number t — the least number of time units.
Each of the next n lines should contain the direction of robot movement and the number of cells to move in that direction. After that movement the robot writes out the number from the cell in which it turns out. The direction and the number of cells should be printed in the form of +x in case of clockwise movement and -x in case of counterclockwise movement to x cells (0 ≤ x ≤ n - 1).
Note that the sum of absolute values of x should be equal to t.

Examples
Input
9 1
0 1 2 2 2 1 0 1 1
Output
12
+0
-3
-1
+2
+1
+2
+1
+1
+1
Input
8 1
0 1 0 1 0 1 0 1
Output
13
+0
+2
+2
+2
-1
+2
+2
+2
Input
8 1
1 2 3 4 5 6 7 8
Output
7
+0
+1
+1
+1
+1
+1
+1
+1
Input
8 1
0 0 0 0 0 0 0 0
Output
7
+0
+1
+1
+1
+1
+1
+1
+1

@题目大意@

一个包含n个房间的环形序列,顺时针依次编号为1~n,第i个房间有一个权值 ai a i
机器人 Simba S i m b a 一开始在第s个房间。每一次 Simba S i m b a 有两个选择:记录当前房间的权值或者沿顺时针或逆时针移动到下一个房间。每移动一次消耗一单位时间,每次记录权值不消耗时间。
Simba S i m b a 想要以非递减的顺序记录权值。请帮它找到一个消耗时间最少的方案。方案的详细输出方法见样例。

@分析@

突破点在于“以非递减的顺序”
显然的这是一个dp的无后效性限制,于是开始无脑地码环形区间dp。
【码着码着发现……woc这个代码复杂度不对吧!这TM是一个代码题啊!】
首先我们存二元组 (val,pos) ( v a l , p o s ) ,即一个房间的权值与下标。以权值为第一关键字,以下标为第二关键字从小到大排序。然后我们相当于要处理两部分:
1)权值相同之间的转移
2)权值不同之间的转移
【废话】
图示1
【注: v1 v 1 < v2 v 2 且不存在 vi v i 使得 v1 v 1 < vi v i < v2 v 2
先考虑权值不同之间的转移。【即图中箭头部分的转移】
定义 end[i] e n d [ i ] 为走完所有满足 val[j]<=val[i] v a l [ j ] <= v a l [ i ] 的j并最终停在i的最短时间
定义 bgn[i] b g n [ i ] 为走完所有满足 val[j]<val[i] v a l [ j ] < v a l [ i ] 的j并最终停在i的最短时间
则对于上面那个图,有:

bgn[i]=minjval[j]=v1{end[j]+min{|pos[i]pos[j]|,n|pos[i]pos[j]|}}(val[i]=v2) b g n [ i ] = min j v a l [ j ] = v 1 { e n d [ j ] + min { | p o s [ i ] − p o s [ j ] | , n − | p o s [ i ] − p o s [ j ] | } } ( v a l [ i ] = v 2 )

【一个选项是逆时针,另一个选项是顺时针】
再考虑权值相同之间的转移。【即图中椭圆内部的转移】
定义 dp[0][l][r] d p [ 0 ] [ l ] [ r ] 为从 pos[l] p o s [ l ] 出发沿顺时针方向到 pos[r] p o s [ r ] 这部分区间全部走完并最终停在 端点的最短时间
定义 dp[1][l][r] d p [ 1 ] [ l ] [ r ] 为从 pos[l] p o s [ l ] 出发沿顺时针方向到 pos[r] p o s [ r ] 这部分区间全部走完并最终停在 端点的最短时间
【区间 [l,r]所表示的意义如下图】
图示2
【注:大家眼睛尖一点自行区分“1”和“l”的区别……画图技术欠佳qwq】
这样有个好处就是我们可以在 意义下进行运算。
初状态:
dp[0][i][i]=dp[1][i][i]=bgn[pos[i]] d p [ 0 ] [ i ] [ i ] = d p [ 1 ] [ i ] [ i ] = b g n [ p o s [ i ] ]

转移式:
dp[0][i][j]=min(dp[0][i+1][j]+(pos[i+1]pos[i]+n)modn,dp[1][i+1][j]+(pos[j]pos[i]+n)modn) d p [ 0 ] [ i ] [ j ] = m i n ( d p [ 0 ] [ i + 1 ] [ j ] + ( p o s [ i + 1 ] − p o s [ i ] + n ) mod n , d p [ 1 ] [ i + 1 ] [ j ] + ( p o s [ j ] − p o s [ i ] + n ) mod n )

dp[1][i][j]=min(dp[0][i][j1]+(pos[j]pos[i]+n)modn,dp[1][i][j1]+(pos[j]pos[j1]+n)modn) d p [ 1 ] [ i ] [ j ] = m i n ( d p [ 0 ] [ i ] [ j − 1 ] + ( p o s [ j ] − p o s [ i ] + n ) mod n , d p [ 1 ] [ i ] [ j − 1 ] + ( p o s [ j ] − p o s [ j − 1 ] + n ) mod n )

终状态:
end[i]=min(dp[0][j][j1],dp[1][j+1][j]) e n d [ i ] = m i n ( d p [ 0 ] [ j ] [ j − 1 ] , d p [ 1 ] [ j + 1 ] [ j ] )

我们只需要在 end[i](val[i]) e n d [ i ] ( v a l [ i ] 最 大 ) 中找答案即可。

@方案@

你以为这样就完了吗?
哇嘎嘎你太天真了你。
咱还要输出方案。
和上面一样,咱分两部分考虑。
对于权值相同之间的转移,用一个 pre[0/1][i][j][3] p r e [ 0 / 1 ] [ i ] [ j ] [ 3 ] 表示 dp[0/1][i][j] d p [ 0 / 1 ] [ i ] [ j ] 是由 dp[pre[0/1][i][j][0]][pre[0/1][i][j][1]][pre[0/1][i][j][2]] d p [ p r e [ 0 / 1 ] [ i ] [ j ] [ 0 ] ] [ p r e [ 0 / 1 ] [ i ] [ j ] [ 1 ] ] [ p r e [ 0 / 1 ] [ i ] [ j ] [ 2 ] ] 转移过来的。
再用另一个 pre2[i][3] p r e 2 [ i ] [ 3 ] 表示 end[i] e n d [ i ] 是由 dp[pre2[i][0]][pre2[i][1]][pre2[i][2]] d p [ p r e 2 [ i ] [ 0 ] ] [ p r e 2 [ i ] [ 1 ] ] [ p r e 2 [ i ] [ 2 ] ]
对于权值不同之间的转移,我们定义 fa[i] f a [ i ] 表示 bgn[i] b g n [ i ] 是由 end[fa[i]] e n d [ f a [ i ] ] 转移过来的。间接表示 dp[0/1][i][i] d p [ 0 / 1 ] [ i ] [ i ] 是由 dp[pre2[fa[i]][0]][pre2[fa[i]][1]][pre2[fa[i]][2]] d p [ p r e 2 [ f a [ i ] ] [ 0 ] ] [ p r e 2 [ f a [ i ] ] [ 1 ] ] [ p r e 2 [ f a [ i ] ] [ 2 ] ]
【变量极多!及其容易搞晕!但是思路是很简单的:记录这个状态是由哪个状态转移过来的】

@代码@

我觉得你们是读不懂的哇嘎嘎。

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 2000;
const int INF = int(1E9);
struct node{int pos, val, pre[3];
}a[MAXN + 5];
bool operator < (node a, node b) {if( a.val == b.val )return a.pos < b.pos;return a.val < b.val;
}
int dp[2][MAXN + 5][MAXN + 5], pre[2][MAXN + 5][MAXN + 5][3];
int bgn[MAXN + 5], end[MAXN + 5], fa[MAXN + 5], n, s, x;
void Print(int x, int y, int z) {if( y == z ) {if( pre[x][y][z][0] != 0 )Print(a[pre[x][y][z][0]].pre[0], a[pre[x][y][z][0]].pre[1], a[pre[x][y][z][0]].pre[2]);if( a[pre[x][y][z][0]].pos <= a[y].pos ) {if( a[y].pos - a[pre[x][y][z][0]].pos < n - a[y].pos + a[pre[x][y][z][0]].pos ) printf("+%d\n", a[y].pos - a[pre[x][y][z][0]].pos);else printf("-%d\n", n - a[y].pos + a[pre[x][y][z][0]].pos);}else {if( a[pre[x][y][z][0]].pos - a[y].pos < n - a[pre[x][y][z][0]].pos + a[y].pos ) printf("-%d\n", a[pre[x][y][z][0]].pos - a[y].pos);else printf("+%d\n", n - a[pre[x][y][z][0]].pos + a[y].pos);}}else {Print(pre[x][y][z][0], pre[x][y][z][1], pre[x][y][z][2]);if( x == 0 ) {if( pre[x][y][z][0] == 0 ) {if( a[pre[x][y][z][1]].pos > a[y].pos )printf("-%d\n", a[pre[x][y][z][1]].pos - a[y].pos);elseprintf("-%d\n", n + a[pre[x][y][z][1]].pos - a[y].pos);//ok}else {if( a[pre[x][y][z][2]].pos > a[y].pos )printf("-%d\n", a[pre[x][y][z][2]].pos - a[y].pos);elseprintf("-%d\n", n + a[pre[x][y][z][2]].pos - a[y].pos);}}else {if( pre[x][y][z][0] == 0 ) {if( a[pre[x][y][z][1]].pos < a[z].pos )printf("+%d\n", a[z].pos - a[pre[x][y][z][1]].pos);//okelseprintf("+%d\n", n - a[pre[x][y][z][1]].pos + a[z].pos);}else {if( a[pre[x][y][z][2]].pos < a[z].pos )printf("+%d\n", a[z].pos - a[pre[x][y][z][2]].pos);//okelseprintf("+%d\n", n - a[pre[x][y][z][2]].pos + a[z].pos);}}}
}
int main() {scanf("%d%d", &n, &s);a[0].pos = s, a[0].val = -INF-5;for(int i=1;i<=n;i++) {scanf("%d", &x);a[i].pos = i, a[i].val = x;}sort(a, a+n+1);int lst = 0;memset(dp, 127, sizeof dp);a[n+1].val = -INF-5;for(int i=1;i<=n+1;i++) {if( a[i].val != a[lst].val ) {int p = i-lst;for(int j=0;j<p;j++) {dp[0][lst+j][lst+j] = dp[1][lst+j][lst+j] = bgn[a[lst+j].pos];pre[0][lst+j][lst+j][0] = pre[1][lst+j][lst+j][0] = fa[a[lst+j].pos];}for(int len=2;len<=i-lst;len++) {for(int j=0;j<p;j++) {int k = (j+len-1) % p;if( dp[1][lst+(j+1)%p][lst+k] + (a[lst+k].pos+n-a[lst+j].pos)%n < dp[0][lst+(j+1)%p][lst+k] + (a[lst+(j+1)%p].pos+n-a[lst+j].pos)%n ) {dp[0][lst+j][lst+k] = dp[1][lst+(j+1)%p][lst+k] + (a[lst+k].pos+n-a[lst+j].pos)%n;pre[0][lst+j][lst+k][0] = 1; pre[0][lst+j][lst+k][1] = lst+(j+1)%p; pre[0][lst+j][lst+k][2] = lst+k;}else {dp[0][lst+j][lst+k] = dp[0][lst+(j+1)%p][lst+k] + (a[lst+(j+1)%p].pos+n-a[lst+j].pos)%n;pre[0][lst+j][lst+k][0] = 0; pre[0][lst+j][lst+k][1] = lst+(j+1)%p; pre[0][lst+j][lst+k][2] = lst+k;}if( dp[1][lst+j][lst+(k+p-1)%p] + (a[lst+k].pos+n-a[lst+(k+p-1)%p].pos)%n < dp[0][lst+j][lst+(k+p-1)%p] + (a[lst+k].pos+n-a[lst+j].pos)%n ) {dp[1][lst+j][lst+k] = dp[1][lst+j][lst+(k+p-1)%p] + (a[lst+k].pos+n-a[lst+(k+p-1)%p].pos)%n;pre[1][lst+j][lst+k][0] = 1; pre[1][lst+j][lst+k][1] = lst+j; pre[1][lst+j][lst+k][2] = lst+(k+p-1)%p;}else {dp[1][lst+j][lst+k] = dp[0][lst+j][lst+(k+p-1)%p] + (a[lst+k].pos+n-a[lst+j].pos)%n;pre[1][lst+j][lst+k][0] = 0; pre[1][lst+j][lst+k][1] = lst+j; pre[1][lst+j][lst+k][2] = lst+(k+p-1)%p;}}}for(int j=1;j<=n;j++)bgn[j] = INF;for(int j=0;j<p;j++) {if( dp[0][lst+j][lst+(j+p-1)%p] < dp[1][lst+(j+1)%p][lst+j] ) {end[lst+j] = dp[0][lst+j][lst+(j+p-1)%p];a[lst+j].pre[0] = 0; a[lst+j].pre[1] = lst+j;a[lst+j].pre[2] = lst+(j+p-1)%p;}else {end[lst+j] = dp[1][lst+(j+1)%p][lst+j];a[lst+j].pre[0] = 1;a[lst+j].pre[1] = lst+(j+1)%p;a[lst+j].pre[2] = lst+j;}for(int k=1;k<=n;k++)if( end[lst+j] + min(n-abs(a[lst+j].pos-k), abs(a[lst+j].pos-k)) < bgn[k] ) {bgn[k] = end[lst+j] + min(n-abs(a[lst+j].pos-k), abs(a[lst+j].pos-k));fa[k] = lst+j;}}if( i == n+1 ) {int minx = lst;for(int j=0;j<p;j++) {if( end[lst+j] < end[minx] )minx = lst + j;}printf("%d\n", end[minx]);Print(a[minx].pre[0], a[minx].pre[1], a[minx].pre[2]);}lst = i;}}
}

@ENDING@

就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。~

这篇关于【CodeForces - 612F】Simba on the Circle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/214263

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

Codeforces#295(Div.2)A、B(模拟+BFS)

解题报告链接:点击打开链接 C. 题目链接:点击打开链接 解题思路: 对于给定的字符串,取出现次数最多的字母(可以同时有多个)。由这些字母组成长度为n的字符串,求有多少种组合。最后用数学知识即可。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <climits>

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co

Codeforces Round #182 (Div. 2)A(水题)

题目链接:http://codeforces.com/contest/302/problem/A 解题思路: 只要通过重新排列使区间内和为0即是1,否则是0. 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#inc

Codeforces Round #233 (Div. 2)A(构造)

题目链接:http://codeforces.com/contest/399/problem/A 解题思路: 构造出来即可,考虑p-k和p+k两个边界分别于1和n作比较,对左右符号特殊处理。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include