【codeforces Gym - 100187K】+ 构造 + 贪心
2017-04-12 21:18
423 查看
K. Perpetuum Mobile
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main part is the energy generator which allows the other mobile’s parts work. The generator consists of two long parallel plates with n lasers on one of them and n receivers on another. The generator is considered to be working if each laser emits the beam to some receiver such that exactly one beam is emitted to each receiver.
It is obvious that some beams emitted by distinct lasers can intersect. If two beams intersect each other, one joule of energy is released per second because of the interaction of the beams. So the more beams intersect, the more energy is released. Innokentiy noticed that if the energy generator releases exactly k joules per second, the perpetuum mobile will work up to 10 times longer. The scientist can direct any laser at any receiver, but he hasn’t thought of such a construction that will give exactly the required amount of energy yet. You should help the scientist to tune up the generator.
Input
The only line contains two integers n and k (1 ≤ n ≤ 200000, ) separated by space — the number of lasers in the energy generator and the power of the generator Innokentiy wants to reach.
Output
Output n integers separated by spaces. i-th number should be equal to the number of receiver which the i-th laser should be directed at. Both lasers and receivers are numbered from 1 to n. It is guaranteed that the solution exists. If there are several solutions, you can output any of them.
Examples
input
4 5
output
4 2 3 1
input
5 7
output
4 2 5 3 1
input
6 0
output
1 2 3 4 5 6
逆序数的个数为 k~
一开始升序排列,每次交换当前第一个 L ,和最后一位 R,每次的贡献为 元素个数num += n * 2 - 3,L++,R–,n -= 2;
当 当前次交换 L,R时,加上贡献大于 K,执行 :把剩下的第一个元素L,尽可能的往后移,直到不能在移L < R,或者逆序数达到K为止(最多两次),L++,R–;
AC代码:
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The world famous scientist Innokentiy almost finished the creation of perpetuum mobile. Its main part is the energy generator which allows the other mobile’s parts work. The generator consists of two long parallel plates with n lasers on one of them and n receivers on another. The generator is considered to be working if each laser emits the beam to some receiver such that exactly one beam is emitted to each receiver.
It is obvious that some beams emitted by distinct lasers can intersect. If two beams intersect each other, one joule of energy is released per second because of the interaction of the beams. So the more beams intersect, the more energy is released. Innokentiy noticed that if the energy generator releases exactly k joules per second, the perpetuum mobile will work up to 10 times longer. The scientist can direct any laser at any receiver, but he hasn’t thought of such a construction that will give exactly the required amount of energy yet. You should help the scientist to tune up the generator.
Input
The only line contains two integers n and k (1 ≤ n ≤ 200000, ) separated by space — the number of lasers in the energy generator and the power of the generator Innokentiy wants to reach.
Output
Output n integers separated by spaces. i-th number should be equal to the number of receiver which the i-th laser should be directed at. Both lasers and receivers are numbered from 1 to n. It is guaranteed that the solution exists. If there are several solutions, you can output any of them.
Examples
input
4 5
output
4 2 3 1
input
5 7
output
4 2 5 3 1
input
6 0
output
1 2 3 4 5 6
逆序数的个数为 k~
一开始升序排列,每次交换当前第一个 L ,和最后一位 R,每次的贡献为 元素个数num += n * 2 - 3,L++,R–,n -= 2;
当 当前次交换 L,R时,加上贡献大于 K,执行 :把剩下的第一个元素L,尽可能的往后移,直到不能在移L < R,或者逆序数达到K为止(最多两次),L++,R–;
AC代码:
#include<cstdio> #include<algorithm> using namespace std; const int K = 2e5 + 10; typedef long long LL; int a[K]; int main() { LL n,k; scanf("%lld %lld",&n,&k); for(int i = 1; i <= n; i++) a[i] = i; LL num = 0,pr = n,pl = 1,kl = n; while(num < k){ if(num + kl * 2 - 3 <= k) swap(a[pl++],a[pr--]),num += kl * 2 - 3,kl -= 2; else{ int i,j; for(i = pl + 1,j = 1; i < pr; i++,j++) if(num + j == k) break; num += j; int p = a[pl]; for(j = pl + 1; j <= i; j++) a[j - 1] = a[j]; a[i] = p; pl++,pr--; } } for(int i = 1; i <= n; i++) printf("%d ",a[i]); return 0; }
相关文章推荐
- Codeforces Gym H. Hell on the Markets 贪心
- [可图性判定 Havel-Hakimi定理 构造] Codeforces Gym 100269 NEERC 13 Northern K. Kids in a Friendly Class
- Educational Codeforces Round 20 C 数学/贪心/构造
- Codeforces Gym 100269K Kids in a Friendly Class 构造题
- Codeforces Gym 100187K K. Perpetuum Mobile 构造
- Codeforces Gym 100803C Shopping 贪心
- CodeForces Gym 100685E Epic Fail of a Genie (贪心,控制精度)
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
- Codeforces Gym 100531I Instruction 构造
- CodeForces Gym 100685I Innovative Business (贪心)
- Codeforces Gym - 101341A Streets of Working Lanterns - 2 [贪心]
- [构造] Codeforces Gym 100553 NEERC 14 E. Epic Win!
- Codeforces Gym 100269E Energy Tycoon 贪心
- 【Codeforces Round 375 (Div 2) F】【构造 贪心】st-Spanning Tree 树的特殊最小生成树使得S度不超SD,T度不超TD
- Codeforces Gym 100342H Problem H. Hard Test 构造题,卡迪杰斯特拉
- Codeforces Gym 100342H Hard Test 构造
- Codeforces Gym 100803C Shopping 贪心好题
- Codeforces Gym 101142 J. Java2016 (构造)
- [构造] Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks
- Codeforces Gym 101173 K. Key Knocking (构造)