传送门

首先对于两个排列 $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. 利用Apache shiro SimpleHash 加密字符串

    1.导入包 import org.apache.shiro.crypto.hash.SimpleHash; 1 2.代码 import org.apache.shiro.crypto.hash.Sim ...

  2. 使用io/ioutil进行读写文件

    读文件: package main import ( "fmt" "io/ioutil" ) func main() { b, err := ioutil.Re ...

  3. Ajax/Highcharts—动态图表

    前面写过“Highcharts的用法总结”,当然了,在实际应用中,图表数据都是要从后台获取的,根据之前的使用,贴一些例子来分享学习. 首先,如果没有获取后台数据,又希望呈现一个动态图表的话,可以很轻易 ...

  4. ubuntu如何删除刚添加的源?

    答: sudo add-apt-repository -r <source_url> 如: sudo add-apt-repository -r ppa:linaro-maintainer ...

  5. redis的服务器信息状态信息查看

    Redis的服务器信息状态信息查看 Redis的提供了一个信息命令查看Redis的服务器的信息,类似的Linux提供一个顶级命令查看系统的信息 redis-cli info # Server #服务器 ...

  6. Spring Boot_打造企业级微信点餐系统_汇总贴

    2019更新版 Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统 H:\BaiDu\微服务0830\2019微服务时代Spring Boot双版本(1.5-2.1)  打造企业级 ...

  7. Docker Registry使用记录

    一.介绍 有时我们的服务器无法访问互联网,或者你不希望将自己的镜像放到公网当中,那么你就需要Docker Registry,它可以用来存储和管理自己的镜像,即私有镜像库. 二.使用 2.1 获取最新镜 ...

  8. [Mac]安装pyspider的大坑

    1.切记这是一个大坑. 2.我在用mac电脑安装pyspider的时候,原以为pip install pyspider 就万事大吉,合家欢乐了,but the question 比较多. 第一个问题: ...

  9. CetOS 服务不支持 chkconfig 的解决方法

    今天在添加Elasticsearch系统自动启动服务的时候,提示 “服务 elasticsearch 不支持 chkconfig ”,如下图: 后来查找了下原因,是脚本编写的不符合规范,缺少关键的前三 ...

  10. Expecting "jsp:param" standard action with "name" and "value" attributes

    浏览器访问报如下jsp标签错误: 根据提示,定位到jsp页面124行,代码如下: 查找原因,当<jsp:include></jsp:include>标签中没有参数时,不允许有空 ...