【CodeForces - 612F】Simba on the Circle

@Simba on the Circle@


@前排提醒@

我的博客太渣了所以大家还是先看看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);
                else
                    printf("-%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);
                else
                    printf("-%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);//ok
                else
                    printf("+%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);//ok
                else
                    printf("+%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@

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值