Codeforces Global Round 14 D. Phoenix and Socks 思维

D. Phoenix and Socks
在这里插入图片描述

solution

除去相同颜色的已配对袜子,剩余袜子遍历颜色考虑:

  1. 当 前 l 总 大 于 r 总 且 a [ i ] [ 0 ] > 1 , 则 只 需 一 次 把 l 变 成 r 的 费 用 当前l_总大于r_总且a[i][0]>1,则只需一次把l变成r的费用 lra[i][0]>1lr
  2. 当 前 r 总 大 于 l 总 且 a [ i ] [ 1 ] > 1 , 则 只 需 一 次 把 r 变 成 l 的 费 用 当前r_总大于l_总且a[i][1]>1,则只需一次把r变成l的费用 rla[i][1]>1rl
  3. 剩 下 来 的 任 意 一 只 l 袜 子 , 都 没 有 一 只 同 色 r 袜 子 , 费 用 e 1 + e 2 2 + ∣ e 1 − e 2 ∣ 2 剩下来的任意一只l袜子,都没有一只同色r袜子,费用\frac{e1+e2}{2}+\frac{|e1-e2|}{2} lr2e1+e2+2e1e2

code

/*SiberianSquirrel*/
/*CuteKiloFish*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int vis[300010][2];

void solve() {
    int T; cin >> T; while (T--) {
        int n, l, r; cin >> n >> l >> r;
        for (int i = 1; i <= n; ++ i)
            for (int j = 0; j < 2; ++ j)
                vis[i][j] = 0;
        for (int i=1; i<=n; i++) {
            int x; cin >> x;
            if(i <= l) vis[x][0] ++;
            else vis[x][1] ++;
        }
        int cost = 0;
        for (int i = 1; i <= n; ++ i) {
            if (vis[i][0] && vis[i][1]) {
                int minn = min(vis[i][0], vis[i][1]);
                vis[i][0] -= minn;
                vis[i][1] -= minn;
            }
        }
        for (int i = 1, temp = l - r; i <= n; ++ i) {
            while (vis[i][0] > 1 && temp > 0) {
                temp -= 2;
                vis[i][0] -= 2;
                cost ++;
            }
            while (vis[i][1] > 1 && temp < 0) {
                temp += 2;
                vis[i][1] -= 2;
                cost ++;
            }
        }
        int e1 = 0, e2 = 0;
        for (int i = 1; i <= n; i++) {
            e1 += vis[i][0];
            e2 += vis[i][1];
        }
        cost += (e1 + e2) / 2 + abs(e1 - e2) / 2;
        cout << cost << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值