abc360
A - A Healthy Breakfast
https://atcoder.jp/contests/abc360/tasks/abc360_a
https://atcoder.jp/contests/abc360/submissions/55049596
int main() { string s; cin >> s; int ri, mi; for(int i=0; i<s.size(); i++){ if (s[i] == 'R'){ ri = i; } else if (s[i] == 'M'){ mi = i; } } if (ri < mi){ cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
B - Vertical Reading
https://atcoder.jp/contests/abc360/tasks/abc360_b
https://atcoder.jp/contests/abc360/submissions/55073344
string s, t; int main() { cin >> s >> t; int len = s.size(); vector<char> cache[105]; for(int w=1; w<=len-1; w++){ for(int i=0; i<105; i++){ cache[i].clear(); } int iter = 0; while(iter < len){ int row = iter/w; cache[row].push_back(s[iter]); iter++; } /* iterate every column */ for(int i=0; i<w; i++){ string res; /* iterate every row */ for(int j=0; j<100; j++){ if (i < cache[j].size()){ res += cache[j][i]; } else { break; } } if (res == t){ cout << "Yes" << endl; return 0; } } } cout << "No" << endl; return 0; }
C - Move It
https://atcoder.jp/contests/abc360/tasks/abc360_c
https://atcoder.jp/contests/abc360/submissions/55082543
typedef long long ll; ll n; ll a[100005]; ll w[100005]; map<ll, vector<ll>> buckets; int main() { cin >> n; for(int i=0; i<n; i++){ cin >> a[i]; } for(int i=0; i<n; i++){ cin >> w[i]; } for(int i=0; i<n; i++){ int bucket_number = a[i]; buckets[bucket_number].push_back(w[i]); } ll sum = 0; for(auto& one: buckets){ vector<ll>& weights = one.second; sort(weights.begin(), weights.end()); for(int i=0; i<weights.size()-1; i++){ sum += weights[i]; } } cout << sum << endl; return 0; }
D - Ghost Ants
https://atcoder.jp/contests/abc360/tasks/abc360_d
https://atcoder.jp/contests/abc360/submissions/55099937
typedef long long ll; ll n, t; string s; ll x[200005]; int main() { cin >> n >> t; cin >> s; for(int i=0; i<n; i++){ cin >> x[i]; } vector<ll> positive, negative; for(int i=0; i<n; i++){ if (s[i] == '1'){ positive.push_back(x[i]); } else { negative.push_back(x[i]); } } sort(positive.begin(), positive.end()); sort(negative.begin(), negative.end()); ll cnt = 0; for(ll i=0; i<positive.size(); i++){ vector<ll>::iterator range1, range2; range1 = upper_bound(negative.begin(), negative.end(), positive[i]); range2 = upper_bound(negative.begin(), negative.end(), positive[i]+2*t); ll diff = distance(range1, range2); cnt += diff; } cout << cnt << endl; return 0; }
E - Random Swaps of Balls
https://atcoder.jp/contests/abc360/tasks/abc360_e
https://atcoder.jp/contests/abc360/submissions/55215609
typedef long long ll; const ll m = 998244353; ll fp(ll x, ll p){ ll qp = x; ll ret = 1; while(p){ if (p&1) ret = ret * qp % m; qp = qp * qp % m; p >>= 1; } return ret; } ll inv(ll x){ return fp(x, m-2); } ll n, k; int main() { cin >> n >> k; ll invn = inv(n); ll invn1 = inv(n-1); /* state transfer after one swap action, 1 = first position, other = other n-1 position. src pos -> dst pos - action probability to make this transfer 1 -> 1 - stay 1 -> other - move other -> other - stay other -> 1 - move/(n-1) */ ll move = 2ll * (n-1) % m * invn % m * invn % m; ll stay = (1 - move + m) % m; ll move1 = move * invn1 % m; array<ll, 2> dp{}; // in the begining, the black ball is on 1st position with 100% probability dp[0] = 1; for(ll i=0; i<k; i++){ array<ll, 2> ndp{}; // new dp /* there are two status transfers to acquire dst pos == 1: 1 -> 1 suppose src pos is 1st, i.e. black ball is on 1st position action probability to keep dest pos == 1 is stay other -> 1 suppose src pos is xth, x=2..n, i.e. black ball is on any other positions action probability to get dest pos == 1 is move1 */ ndp[0] = (1ll * dp[0] * stay % m + 1ll * dp[1] * move1 % m) % m; /* there are two status transfers to acquire dst pos == any of other positions: 1 -> other suppose src pos is 1st, i.e. black ball is on 1st position action probability to keep dest pos == other is move other -> other suppose src pos is xth, x=2..n, i.e. black ball is on any other positions action probability to get dest pos == other is 1 - move1 */ ndp[1] = (1ll * dp[0] * move % m + 1ll * dp[1] * ((1 - move1 + m) % m) % m) % m; // dp.swap(ndp); dp = ndp; } /* get final probability */ ll sum = (dp[0] + 1ll * (n + 2ll) % m * (n - 1ll) % m * inv(2) % m * dp[1] % m * invn1 % m) % m; cout << sum << endl; return 0; }
reference:
https://www.cnblogs.com/Lanly/p/18277192#e---random-swaps-of-balls-abc360-e
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。