Codeforces Global Round 2 题解
题目链接:https://codeforces.com/contest/1119
A. Ilya and a Colorful Walk
题意:
给出n个数,问从一个数到另外一个不同数的最长距离是多少。
题解:
从前往后,从后往前扫两遍即可。证明可用反证法,这里略去。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 3e5 + 5; int n; int c[N]; int main() { ios::sync_with_stdio(false);cin.tie(0); cin >> n; for(int i = 1; i <= n ;i++) { cin >> c[i] ; } int ans = 0; for(int i = 2; i <= n ;i++) { if(c[i] != c[1]) ans=max(i - 1, ans); } for(int i = n - 1; i >= 1; i--) { if(c[i] != c[n]) ans=max(n - i, ans); } cout << ans; return 0 ; }
B. Alyona and a Narrow Fridge
题意:
给一个冰箱n行2列,然后n个瓶子,每个瓶子都有一个高度。现在要把最多的前k个瓶子放进冰箱里面去,最后会形成若干个隔层,每个隔层中最多两个瓶子。现在要求确定这个k最大为多少。
题解:
放瓶子的话肯定是贪心放的,将最高的尽可能放入一个隔层中。最后二分判断一下就行了。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3 + 5; int n, h; int a[N], b[N]; bool check(int x) { for(int i = 1; i <= x ; i++) b[i] = a[i]; sort(b + 1, b + x + 1) ; int tmp = h; for(int i = x; i >= 1; i -= 2) { int j = max(1, i - 1) ; tmp -= max(b[i], b[j]) ; if(tmp < 0) break ; } return tmp >= 0; } int main() { ios::sync_with_stdio(false);cin.tie(0); cin >> n >> h; for(int i = 1; i <= n ;i++) cin >> a[i] ; int l = 0, r = n + 1, mid ; while(l < r) { mid = (l + r) >> 1; if(check(mid)) l = mid + 1; else r = mid; } cout << l - 1; return 0 ; }