跳到正文

题解

P11723 [JOIG 2025] カードゲーム 4 / Card Game 4 题解

原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟
题目
P11723

版本

  1. v1当前

开始解题!

因为我们要使得答案最大,所以我们肯定是要从大到小进行排序,但是题目中又要求了要奇偶性两两相同,所以我们利用结构体,把 aia_i 和 ai mod 2a_i \bmod 2(即 aia_i 的奇偶性)绑定起来。

然后,假设现在我们要找到 aia_i 中奇数的前 kk 大,那么我们有如下排序策略:

  • 假设现在对 axa_x 和 aya_y 进行排序
  • 如果 ax mod 2=ay mod 2a_x \bmod 2 = a_y \bmod 2,那么我们就把较大者排在前面
  • 否则,我们就把这其中是奇数的排在前面

所以 cmp 函数可以这么写:

C++
bool cmp1(node x, node y) {
    if (x.v == y.v) {
        return x.a > y.a;
    } else {
        return x.v > y.v;
    }
}

其中 aa 是原本的数值,vv 是其对应的奇偶性。

要取偶数的前 kk 大也是同理,只是在判断奇偶性时,因为偶数对 22 取模的结果是 00,所以符号进行了转变,代码如下:

C++
bool cmp2(node x, node y) {
    if (x.v == y.v) {
        return x.a > y.a;
    } else {
        return x.v < y.v;
    }
}

注意要排两次序,因为可以奇数在前也可以偶数在前,每次排完序就先统计当前状态所对应的答案,统计答案的过程如下:

  • 排序完成之后,在前 kk 个元素中进行统计答案,注意判断当前扫到的元素的奇偶性是否与上一个元素的奇偶性相同,如果不相同说明这个答案无法成立。
  • 最后注意如果你从 a1a_1 开始统计答案要记得初始化 a0a_0 的奇偶性,否则会炸。

代码是简单的,缺省源有点长,主要的部分在主函数中。

code :

C++
#include <bits/stdc++.h>
#define ll long long
#define db double
#define vec vector
#define pll pair<ll, ll>
#define mkp make_pair
#define endl "\n"

using namespace std;

const ll mod = 998244353;

const ll maxn = 3;

namespace fastio {
	char buf[1 << 21], *p1 = buf, *p2 = buf;

	const ll getc() {
	    return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
	}

	const ll read() {
		ll x = 0, f = 1;

		char ch = getc();

		while (ch < '0' || ch > '9') {
			if (ch == '-') f = -1; ch = getc();
		}

		while (ch >= '0' && ch <= '9') {
			x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
		}

		return x * f;
	}

	const void write(ll x) {
		if (x < 0) {
			putchar('-'), x = -x;
		}

	    ll sta[35], top = 0;

	    do {
	        sta[top++] = x % 10, x /= 10;
	    } while (x);

	    while (top) putchar(sta[--top] + 48);
	}
}

namespace math {
    vector<ll> fac, inv;

    void init(ll sz) {
        fac.resize(sz + 5), inv.resize(sz + 5);

        fac[0] = fac[1] = inv[0] = inv[1] = 1;

        for (ll i = 2; i <= sz; i++) {
            fac[i] = fac[i - 1] % mod * i % mod;

            inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod;
        }

        for (ll i = 2; i <= sz; i++) {
            inv[i] *= inv[i - 1] % mod, inv[i] %= mod;
        }
    }

    ll A(ll n, ll m) {
        return fac[n] % mod * inv[n - m] % mod;
    }

    ll C(ll n, ll m) {
        return fac[n] % mod * inv[m] % mod * inv[n - m] % mod;
    }
}

ll qpow(ll a, ll b, ll m = 1) {
    if (b == 0) return 1 % m;

    ll ans = 1;

	while (b) {
        if (b & 1) ans = ans * a % m;

        a = a * a % m;

        b >>= 1;
    }

    return ans;
}

ll gcd(ll a, ll b) {
	return a == 0 || b == 0 ? max(a,b) : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a,b) * b;
}

ll lowbit(ll x) {
    return x & (-x);
}

ll popcount(ll x){
    ll res = 0;

    while (x) {
        x -= lowbit(x);

        res ++;
    }

    return res;
}

struct BIT {
	ll n = 0; std::vector<ll> c;

	BIT (ll sz) {
		n = sz, c.resize(sz + 5);
	};

	void add(ll x, ll y) {
		if (n == 0) {
			return ;
		} else {
			for (ll i = x; i <= n; i += lowbit(i)) {
				c[i] += y;
			}
		}

		return ;
	}

	ll sum(ll x) {
		ll res = 0;

		for (ll i = x; i; i -= lowbit(i)) {
			res += c[i];
		}

		return res;
	}
};

struct DSU { // 并查集
    ll n = 0; vector<ll> fa;

    DSU (ll sz) {
        n = sz, fa.resize(sz + 5);
    };

    void init() {
        if (n == 0) {
            return ;
        }

        for (ll i = 1; i <= n; i++) {
            fa[i] = i;
        }
    }

    ll find(ll x) {
        return x == fa[x] ? x : find(fa[x]);
    }

    void merge(ll x, ll y) {
        x = find(x), y = find(y);

        if (x != y) {
            fa[x] = y;
        }
    }
};

// template<typename tp1,typename tp2,int N>
// struct Htb{
    // static constexpr int M=1e7+19;
    // int hd[M+3],to[N],ct;
    // tp1 ed[N];tp2 w[N];
    // static int hc(ul v){
        // v^=v<<13,v^=v>>7;
        // return (v^(v<<17))%M;
    // }
    // void ins(tp1 x,tp2 y){
        // int &p=hd[hc(x)];
        // ed[++ct]=x,to[ct]=p;
        // w[p=ct]=y;
    // }
    // int count(tp1 x){
        // for(int i=hd[hc(x)];i;i=to[i])
            // if(ed[i]==x)return 1;
        // return 0;
    // }
    // pair<tp2,bool>find(tp1 x){
        // for(int i=hd[hc(x)];i;i=to[i])
            // if(ed[i]==x)return mkp(w[i],true);
        // return mkp(0,false);
    // }
    // int operator[](tp1 x){
        // int &p=hd[hc(x)];
        // for(int i=p;i;i=to[i])
            // if(ed[i]==x)return i;
        // ed[++ct]=x,to[ct]=p;
        // return p=ct;
    // }
    // void clear(){while(ct)hd[hc(ed[ct--])]=0;}
// };

#define rd fastio::read
#define wt fastio::write
#define gc fastio::getc

ll n, m, q; string s;

ll opt, l, r, ans = 0;

struct node {
    ll a, v;
} a[200005] ;

bool cmp1(node x, node y) {
    if (x.v == y.v) {
        return x.a > y.a;
    } else {
        return x.v > y.v;
    }
}

bool cmp2(node x, node y) {
    if (x.v == y.v) {
        return x.a > y.a;
    } else {
        return x.v < y.v;
    }
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

    ll i, j, k, x, y, z, res = 0, now;

    n = rd(), m = rd();

    for (i = 1; i <= n; i++) a[i].a = rd(), a[i].v = a[i].a % 2;

    sort(a + 1, a + n + 1, cmp1);

    a[0].v = 1;

    for (i = 1; i <= m; i++) {
        if (a[i].v != a[i - 1].v) {
            ans = 0;

            break;
        } else {
            ans += a[i].a;
        }
    }

    sort(a + 1, a + n + 1, cmp2);

    a[0].v = 0;

    for (i = 1; i <= m; i++) {
        if (a[i].v != a[i - 1].v) {
            res = 0;

            break;
        } else {
            res += a[i].a;
        }
    }

    ans = max(ans, res);

    wt(ans);

	return 0;
}

AC record

原文