Hello 2024

在这里插入图片描述

Hello 2024

A. Wallet Exchange

题意:Alice和Bob各有a和b枚硬币,每次他们可以选择交换硬币或者保留,然后扣除当前一枚手中的硬币,当一方没得扣另一方就赢了。

思路:Alice先手,所以当硬币和为奇数时Alice必输,反之当总和为偶数时Bob必输。

AC code:

void solve() {int x, y; cin >> x >> y;int sum = x + y;if(sum % 2 == 0) cout << "Bob" << endl;else cout << "Alice" << endl;
} 

B. Plus-Minus Split

题意:略

思路:按字符串原顺序分割,则直接从头到尾跑一遍,统计最终分数,即为最终结果。

AC code:

void solve() {string s;cin >> n >> s;int now = 0, cnt = 0;for (int i = 0; i < n; i ++) {if (s[i] == '+') now += 1;else now -=1;}cnt = abs(now);cout << cnt << endl;
}

C. Grouping Increases

题意:给出正整数序列a,将序列a分割成两个子序列x和y,a中的元素必须出现在x或y中的一个,x和y均为a的子序列,x和y可以为空;

现在需要分割序列a,最小化x和y序列中的递增数对的个数。

思路:贪心,顺序枚举每一个a中元素,用两个整数x和y分别记录当前分割后的子序列的最后一个元素:

  • 当前x和y其中一个满足添加新的末尾元素而不增加答案时,更新该末尾元素;
  • 当x和y均满足时,将新的末尾元素添加至当前较小的末尾;
  • 当x和y均不满足时,同样要添加到较小的末尾,可以最小化递增数对

AC code:

void solve() {int n;cin >> n;for (int i = 0; i < n; i ++) {cin >> a[i];}int x = -1, y = -1;int cnt = 0;for (int i = 0; i < n; i ++) {if (x == -1) {x = a[i];continue;}if (a[i] <= x) {if (a[i] <= y) {if (x < y) x = a[i];else y = a[i];} else {x = a[i];}} else {if (y == -1) {y = a[i];continue;}if (a[i] <= y) {y = a[i];continue;}if (x < y) x = a[i];else y  = a[i];cnt ++;}} cout << cnt << endl;
}