Codeforces 1244G. Running in Pairs
首先对于两个排列 $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的更多相关文章
- codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- CodeForces - 9B - Running Student
先上题目: B. Running Student time limit per test 1 second memory limit per test 64 megabytes And again ...
- 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 ...
- Codeforces 1404D - Game of Pairs(构造)
Codeforces 题面传送门 & 洛谷题面传送门 首先注意到 \(\sum\limits_{i=1}^{2n}i=\dfrac{2n(2n+1)}{2}=n(2n+1)\equiv n\p ...
- Codeforces 1542E2 - Abnormal Permutation Pairs (hard version)(DP)
upd on 2021.7.7:修了个 typo Codeforces 题目传送门 & 洛谷题目传送门 首先考虑怎样处理"字典序小"这个问题,按照字典序比大小的套路,我们可 ...
- Codeforces 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- 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 ...
- 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 ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
随机推荐
- centos7下面装fastdfs
首先我完全是按照这个链接上面来的,其中也算有点小错https://blog.csdn.net/ty5546/article/details/79245648 环境准备编译环境 yum install ...
- ubuntu 安装eclipse for c++
linux的GUI和windos比起来实在逊色,虽然它的终端模式(命令行模式)非常强大.linux发行版ubuntu的GUI相对其他版本要华丽一些,所以最近由redhat转向ubuntu进行linux ...
- IntelliJ IDEA-配置文件位置
关于配置文件的位置 一旦开始使用IDEA之后,就需要做很多的配置相关工作,使得IDEA越来越符合你的个人习惯,让你使用起来得心应手.而这些配置信息,都保存在C盘,比如我的就会默认保存在如图所示的位置 ...
- JS各循环的差别
1.最普通的for循环: for(var i=0;i<arr.length;i++){ } 特点:只能针对数组循环,不能引用于非数组对象 2.for(var i in obj){ } 特点:用于 ...
- Nginx-HTTP之静态网页访问流程分析一
假设访问静态网页的配置如下: worker_processes 1; error_log stderr debug; daemon off; master_process on; events { w ...
- VIM | vim操作大全
1. 关于Vim vim是Linux下第二强大的编辑器. 虽然emacs是公认的世界第一,我认为使用emacs并没有使用vi进行编辑来得高效. 如果是初学vi,运行一下vimtutor是个聪明的决定. ...
- HttpClient提交数据
用代码模拟浏览器的行为 * 轻量级的开源的框架 * Android在6.0 23 以后移除了httpclient ,所以开发中用的少了 * 编写步骤: 1. 打开浏览器 2. 输入网址 3. 敲回车 ...
- 【转载】网页JS获取当前地理位置(省市区)
眼看2014又要过去了,翻翻今年的文章好像没有写几篇,忙真的或许已经不能成为借口了,在忙时间还是有的,就像海绵里的水挤挤总会有滴.真真的原因是没有学习过什么新的技术,工作过程中遇到的问题也不是非常难并 ...
- Mysql 查询当前数据上一条和下一条的记录
获取当前文件上一条与下一条记录的原理是上一条的sql语句,从news表里按从大到小的顺序选择一条比当前ID小的新闻,下一条的sql语句,从news表里按从小到大的顺序选择一条比当前ID大的新闻. 如果 ...
- WebServer_参考
参考:http://blog.csdn.net/cjsafty/article/details/9323425 这里顺便记录下几个页面 lajphttps://code.google.com ...