Codeforces 1244G. Running in Pairs

Stella981
• 阅读 426

传送门

首先对于两个排列 $A,B$ 我们可以把 $A$ 从小到大排序并把 $B$ 重新和 $A$ 一一对应

显然这样不会影响 $\sum_{i=1}^{n}max(A_i,B_i)$ 的值

所以直接把第一个排列固定为 $1,2,3,...,n$

然后考虑第二个排列 $B$ 怎么排比较好

首先最少的时间一定就是 $B_i=i$ 的情况

然后考虑让时间变大,容易想到把 $B_1$ 和 $B_n$ 交换,变成 $n,2,3,...,n-1,1$

那么时间增加了 $n-1$,一直操作最后 $B$ 就变成 $n,n-1,n-2,...,3,2,1$

那么容易想到贪心,每次都先加得比较大,最后快超过的时候再加一个比较小的

容易证明这个显然是最优的,因为一开始就加大的之后的选择就有更多空间(有更多比较小的值可以增加)

不然到时候还剩下一些时间,发现增加的量都很大那么就没法加了

(可能看代码更容易理解?)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
    return x*f;
}
const int N=1e6+7;
int n,ans[N];
ll m;
int main()
{
    n=read(),m=read(); ll mx=m;
    for(int i=1;i<=n;i++)
        ans[i]=i,m-=i;
    if(m<0) { printf("-1\n"); return 0; }
    for(int i=1;i<=n/2;i++)
    {
        int now=(n-i+1)-i;
        if(m<=now)
        {
            swap( ans[ n-i+1 - (now-m) ] , ans[i] );
            // 显然 n-i-1 - (now-m) > i,代入一下 now=n-2i+1 即可
            m=0; break;
        }
        swap(ans[n-i+1],ans[i]); m-=now;
    }
    printf("%lld\n",mx-m);
    for(int i=1;i<=n;i++) printf("%d ",i); puts("");
    for(int i=1;i<=n;i++) printf("%d ",ans[i]); puts("");
    return 0;
}
点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Codeforces Round #479 (Div. 3) F. Consecutive Subsequence
标签:DP题目链接(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F977%2Fproblem%2FF)
Stella981 Stella981
2年前
Comet OJ
题意https://www.cometoj.com/contest/52/problem/C?problem\_id2416(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.cometoj.com%2Fcontest%2F52%2Fproblem%2FC%3Fprob
Stella981 Stella981
2年前
Codeforces Round #611 (Div. 3)
原题面:https://codeforces.com/contest/1283(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fcodeforces.com%2Fcontest%2F1283)A.MinutesBeforetheNewYear题目大意:给定时间,问距离零点
Stella981 Stella981
2年前
Educational Codeforces Round 73 (Rated for Div. 2)
传送门(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fcodeforces.com%2Fcontest%2F1221)A.2048Game乱搞即可。<details<summaryCode</summaryinclude<bits/std
Stella981 Stella981
2年前
Codeforces Round #616 (Div. 2)
A.EvenButNotEven(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F1291%2Fproblem%2FA)题意:给你一个很长的数,可以删减里面的任意数字,要求本身不能除以2,但是该数的各位和能除以2,输出任意符合
Stella981 Stella981
2年前
Codeforces Round #565 (Div. 3) C. Lose it!
链接:https://codeforces.com/contest/1176/problem/C(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fcodeforces.com%2Fcontest%2F1176%2Fproblem%2FC)题意:Youare
Stella981 Stella981
2年前
CF 833 B. The Bakery
B.TheBakeryhttp://codeforces.com/contest/833/problem/B(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F833%2Fproblem%2FB)题
Stella981 Stella981
2年前
85D Sum of Medians
传送门(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F85%2Fproblem%2FD)题目Inonewellknownalgorithmoffindingthe_k_\thorderst
Stella981 Stella981
2年前
E. You Are Given Some Strings...
E.YouAreGivenSomeStrings...(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fcontest%2F1202%2Fproblem%2FE)AC自动机求一个串$t$中包含子串$s\_{i}s\_{j}$的个数。
Stella981 Stella981
2年前
Gym101889B. Buggy ICPC(打表)
比赛链接:传送门(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcodeforces.com%2Fgym%2F101889)题目:!(https://oscimg.oschina.net/oscnet/54993c8b5f90544695020de0694c1