【codeforces】Codeforces Round #642 (Div. 3)

比赛传送门

A. Most Unstable Array

B. Two Arrays And Swaps

C. Board Moves

D. Note that the answer exists and unique.

AC代码


A. Most Unstable Array

找规律发现:

  • \(n = 1\)时为\(0\)

  • \(n = 2\)时为\(n\)

  • \(n > 2\)时为\(2n\)

B. Two Arrays And Swaps

【贪心】依次b数组最大的与a数组最小的交换。

C. Board Moves

【贪心】【数学】因为\(n\)只能是奇数,所以把集中的点放在正中间即可,然后就是公式推导。

/*
n = 7时的情况:
3333333
3222223
3211123
3210123
3211123
3222223
3333333
*/

D. Note that the answer exists and unique.

【优先队列】【模拟】这题会重载运算符与优先队列的话就只是简单的模拟题:区间大的优先,如果区间大小都一样就按照从左到右的顺序来。


AC代码

A

#include<iostream>
#include<cstdio>
using namespace std;

int T, n, m;

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d %d", &n, &m);
        if(n == 1) printf("0\n");
        else if(n == 2) printf("%d\n", m);
        else printf("%d\n", m * 2);
    }
    return 0;
}

B

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int T, n, k;
int a[102], b[102];

bool cmp(int a, int b){
    return a > b;
}

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d %d", &n, &k);
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);
        for(int i = 0; i < n; i++) scanf("%d", &b[i]);
        sort(a, a + n);
        sort(b, b + n, cmp);

        int tmp = 0;
        for(int i = 0; i < k; i++){
            if(a[i] < b[tmp]){
                a[i] = b[tmp++];
            }
        }

        int ans = 0;
        for(int i = 0; i < n; i++) {
//            printf("%d ", a[i]);
            ans += a[i];
        }
        printf("%d\n", ans);
    }
    return 0;
}

C

#include<iostream>
#include<cstdio>
using namespace std;

typedef long long LL;
LL ans[500005];

void build(){
    LL t = 1;
    for(LL i = 1; i <= 500000; i += 2){
        if(i == 1) ans[i] = 0;
        else {
            ans[i] += ans[i - 2] + ((i * 4 - 4) * t);
            t++;
        }
    }
}

int main()
{
    build();
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        printf("%I64d\n", ans[n]);
    }
    return 0;
}

D

//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int T, n, tmp;
int num[200005];
bool take[200005];
struct node{
    int l, r;
};
struct node2{
    bool operator() (node a, node b){ // 重载运算符
        if(a.r - a.l == b.r - b.l) return a.l > b.l; // 如果区间相等,就先从左边开始
        return a.r - a.l < b.r - b.l; // 区间大的优先
    }
};
priority_queue<node, vector<node>, node2> q;

void solve(){
    while(!q.empty()) q.pop(); // 把上次还没出来的先都出来
    q.push({1, n});
    while(!q.empty()){
        node now = q.top(); q.pop();

        if(now.l > now.r) continue ;

        if(now.l == now.r){
            if(num[now.l] != 0) continue ;
            num[now.l] = tmp++;
            continue ;
        }

        if((now.r - now.l + 1) % 2 == 1){
            int mid = (now.r + now.l) / 2;
            num[mid] = tmp++;
                q.push({now.l, mid - 1});
                q.push({mid + 1, now.r});
        }
        else {
            int mid = (now.r + now.l - 1) / 2;
            num[mid] = tmp++;
                q.push({now.l, mid - 1});
                q.push({mid + 1, now.r});
        }
    }
}

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        memset(num, 0, sizeof(num));
        tmp = 1;
        solve();
        for(int i = 1; i <= n; i++){
            if(i != 1) printf(" ");
            printf("%d", num[i]);
        }
        printf("\n");
    }
    return 0;
}

posted on 2020-05-15 10:51  Ayasan  阅读(164)  评论(0编辑  收藏  举报