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 ...
随机推荐
- 使用python播放音乐
1.首先安装pygame,pip install pygame 2.上代码: import time import pygame #音乐路径 filepath=r"C:\Users\1473 ...
- Guava中Lists.partition(List, size) 方法懒划分/懒分区
目录 Guava中Lists.partition(List, size) 方法懒划分/懒分区 背景 分析 总结 Guava中Lists.partition(List, size) 方法懒划分/懒分区 ...
- 【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)
一.selenium2.0简述 与一般的浏览器测试框架(爬虫框架)不同,Selenium2.0实际上由两个部分组成Selenium+webdriver,Selenium负责用户指令的解释(code), ...
- 搬运 centos7.2 apache 绑定二级目录 访问依然是apache页面
<VirtualHost *:80>ServerName xx.comDocumentRoot /var/www/html/xx</VirtualHost>
- 黑马vue---19、v-for中key的使用注意事项
黑马vue---19.v-for中key的使用注意事项 一.总结 一句话总结: 必须 在使用 v-for 的同时,指定 唯一的 字符串/数字 类型 :key 值 <p v-for="i ...
- 用C#做成的程序如何打包部署,在其他PC机运行
刚刚接触C#,在linux下面做习惯了c和c++,在本机运行OK后,得瑟的去别的机子,居然运行不了,你妹啊,怎么回事,没有.NET Framework,原来和java类似,.NET Framework ...
- mybatisplus
1.selectById 根据主键查询一个对象,如果没有查到,则返回null: GxySchoolDto isExist = gxySchoolMapper.selectById(schoolEnti ...
- dede不同栏目调用不同banner图的方法
用顶级栏目ID 方法: <img src="{dede:global.cfg_templets_skin/}/images/{dede:field.typeid function=&q ...
- kali破解wifi密码
开始 1.选择合适的网卡,有些网卡kali识别不了,我用的网卡信息 2.网卡开启监听模式 3.查看监听模式是否开启成功(网卡名称变成wlan0mon说明已经开启成功) 4.输入“airodump-ng ...
- htop/dstat/top/ps命令的使用
top命令 显示系统中进程信息 [root@node0 ~]# top top - 09:36:45 up 13:39, 3 users, load average: 0.02, 0.03, 0. ...