Codeforces Round #585 (Div. 2)
A. Yellow Cards
思路:水题
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
int a1, a2, k1, k2, n, sum, res, cnt1, cnt2;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> a1 >> a2 >> k1 >> k2 >> n;
if (k1 > k2){
swap(a1, a2);
swap(k1, k2);
}
sum = a1 * (k1 - 1) + a2 * (k2 - 1);
res = max(0, min(a1 + a2, n - sum));
cnt1 = min(a1, n / k1);
n -= cnt1 * k1;
cnt2 = min(a2, n / k2);
cout << res << " " << cnt1 + cnt2 << "\n";
return 0;
}
B. The Number of Products
思路:将正数令为0 负数令为1 统计前缀和 然后从头扫一遍 维护前缀和为奇数的个数和前缀和为偶数的个数
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 2e5 + 10;
int n;
int a[N], sum[N];
ll ans1, ans2, cnt1, cnt2;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i ++ ){
cin >> a[i];
if (a[i] > 0)
a[i] = 0;
else
a[i] = 1;
}
for (int i = 1; i <= n; i ++ )
sum[i] = (sum[i - 1] + a[i]) % 2;
cnt1 ++ ;
for (int i = 1; i <= n; i ++ ){
if (sum[i]) {
ans1 += cnt2;
ans2 += cnt1;
cnt2 ++ ;
}
else{
ans1 += cnt1;
ans2 += cnt2;
cnt1 ++ ;
}
}
cout << ans2 << " " << ans1 << "\n";
return 0;
}
C. Swap Letters
思路:首先a和b都为奇数时不能变为相同的 然后两个ab或ba可以一步变成相同的 ab和ba要两步
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
int n, cnta, cntb;
string s, t;
vector<int> pos_ab, pos_ba;
vector<pair<int, int> > ans;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s >> t;
for (int i = 0; i < n; i ++ ){
if (s[i] == 'a')
cnta ++ ;
else
cntb ++ ;
if (t[i] == 'a')
cnta ++ ;
else
cntb ++ ;
if (s[i] != t[i]){
if (s[i] == 'a')
pos_ab.push_back(i + 1);
else
pos_ba.push_back(i + 1);
}
}
if ((cnta & 1) || (cntb & 1)){
cout << "-1\n";
return 0;
}
for (int i = 0; i + 1 < pos_ab.size(); i += 2)
ans.push_back(make_pair(pos_ab[i], pos_ab[i + 1]));
for (int i = 0; i + 1 < pos_ba.size(); i += 2)
ans.push_back(make_pair(pos_ba[i], pos_ba[i + 1]));
if (pos_ab.size() & 1){
ans.push_back(make_pair(pos_ab.back(), pos_ab.back()));
ans.push_back(make_pair(pos_ab.back(), pos_ba.back()));
}
cout << ans.size() << "\n";
for (auto it: ans)
cout << it.first << " " << it.second << "\n";
return 0;
}
D. Ticket Game
思路:博弈论
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
int n, sum1, sum2, c1, c2;
string s;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> s;
for (int i = 0; i < n / 2; i ++ ){
if (s[i] == '?')
c1 ++ ;
else
sum1 += s[i] - '0';
}
for (int i = n / 2; i < n; i ++ ){
if (s[i] == '?')
c2 ++ ;
else
sum2 += s[i] - '0';
}
if (!c1 && !c2){
if (sum1 == sum2)
cout << "Bicarp\n";
else
cout << "Monocarp\n";
}
else if (c1 == c2){
if (abs(sum1 - sum2) >= 1)
cout << "Monocarp\n";
else
cout << "Bicarp\n";
}
else if (c1 > c2){
if (sum2 - sum1 == 9 * (c1 - c2) / 2)
cout << "Bicarp\n";
else
cout << "Monocarp\n";
}
else if (c1 < c2){
if (sum1 - sum2 == 9 * (c2 - c1) / 2)
cout << "Bicarp\n";
else
cout << "Monocarp\n";
}
return 0;
}