【codeforces Gym - 100187K】+ 构造 + 贪心

时间:2021-06-15 15:51:00

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代码:

#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;
}