传送门

首先对于两个排列 $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=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=1e6+;
int n,ans[N];
ll m;
int main()
{
n=read(),m=read(); ll mx=m;
for(int i=;i<=n;i++)
ans[i]=i,m-=i;
if(m<) { printf("-1\n"); return ; }
for(int i=;i<=n/;i++)
{
int now=(n-i+)-i;
if(m<=now)
{
swap( ans[ n-i+ - (now-m) ] , ans[i] );
// 显然 n-i-1 - (now-m) > i,代入一下 now=n-2i+1 即可
m=; break;
}
swap(ans[n-i+],ans[i]); m-=now;
}
printf("%lld\n",mx-m);
for(int i=;i<=n;i++) printf("%d ",i); puts("");
for(int i=;i<=n;i++) printf("%d ",ans[i]); puts("");
return ;
}

Codeforces 1244G. Running in Pairs的更多相关文章

  1. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. CodeForces - 9B - Running Student

    先上题目: B. Running Student time limit per test 1 second memory limit per test 64 megabytes   And again ...

  3. CodeForces - 1189 E.Count Pairs (数学)

    You are given a prime number pp, nn integers a1,a2,…,ana1,a2,…,an, and an integer kk. Find the numbe ...

  4. Codeforces 1404D - Game of Pairs(构造)

    Codeforces 题面传送门 & 洛谷题面传送门 首先注意到 \(\sum\limits_{i=1}^{2n}i=\dfrac{2n(2n+1)}{2}=n(2n+1)\equiv n\p ...

  5. Codeforces 1542E2 - Abnormal Permutation Pairs (hard version)(DP)

    upd on 2021.7.7:修了个 typo Codeforces 题目传送门 & 洛谷题目传送门 首先考虑怎样处理"字典序小"这个问题,按照字典序比大小的套路,我们可 ...

  6. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  7. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

  8. Codeforces Beta Round #9 (Div. 2 Only) B. Running Student 水题

    B. Running Student 题目连接: http://www.codeforces.com/contest/9/problem/B Description And again a misfo ...

  9. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

随机推荐

  1. find命令计算代码行数

    [anonymous@localhost ~/lvs/ipvsadm- -regex '.*Makefile.*' -o -regex '.*\.[ch]' -exec cat {} \; | wc ...

  2. 8. 使用Zuul构建微服务网关

                    使用Zuul构建微服务网关 8.1. 为什么要使用微服务网关 8.2. Zuul简介 8.3. 编写Zuul微服务网关 8.4. Zuul的路由端点 8.5. Zuul ...

  3. 我的zshrc文件设置备份

    # If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin: ...

  4. HTTP缓存机制和原理

    前言 Http 缓存机制作为 web 性能优化的重要手段,对于从事 Web 开发的同学们来说,应该是知识体系库中的一个基础环节,同时对于有志成为前端架构师的同学来说是必备的知识技能.但是对于很多前端同 ...

  5. webpack4 打包 library 遇到的坑

    output: { publicPath: '/', path: path.join(__dirname, 'lib'), filename: 'chart.js', library: 'tchart ...

  6. MongDB的DateZone

    先理解:Date本身是没有格式的,只是一个毫秒数,要显示成某种格式就一定是字符串 https://github.com/ewcmsfree/ewcms/wiki/Help-mongo-java-dri ...

  7. 第10组Alpha冲刺(3/4)

    队名:凹凸曼 组长博客 作业博客 组员实践情况 童景霖 过去两天完成了哪些任务 文字/口头描述 继续学习Android studio和Java 制作登录注册界面前端 展示GitHub当日代码/文档签入 ...

  8. html5验证自适应

    // 移动端跳转 var OS = function() { var a = navigator.userAgent, b = /(?:Android)/.test(a), d = /(?:Firef ...

  9. Linux 查看Tomcat版本信息

    Linux 查看Tomcat版本信息 如果我们想运行在 Linux 下的 Tomcat 版本信息,只需要在 Tomcat 的 bin/ 目录下,运行 version.sh 脚本即可. 1.使用如下命令 ...

  10. jenkins中使用shell脚本必须切换jenkins用户

    https://blog.csdn.net/cdnight/article/details/81078191 安装这篇解决的 正确使用脚本的方式: 1.脚本必须使用jenknins用户,且所有命令必须 ...