比赛链接:https://codeforces.com/contest/1370
D. Odd-Even Subsequence
题目大意:
想法:
直接考虑去二分答案。每次二分去 check 一下是否符合题目意思的要求【分此时答案是奇数位置 和 偶数位置 两个去判断】
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <cmath> 6 7 #define ll long long 8 #define ull unsigned long long 9 #define ls nod<<1 10 #define rs (nod<<1)+1 11 #define pii pair<int,int> 12 #define mp make_pair 13 #define pb push_back 14 #define INF 0x3f3f3f3f 15 #define max(a, b) (a>b?a:b) 16 #define min(a, b) (a<b?a:b) 17 18 19 const double eps = 1e-8; 20 const int maxn = 2e5 + 10; 21 const ll MOD = 1e9 + 7; 22 const int mlog=20; 23 24 int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } 25 26 using namespace std; 27 28 int a[maxn]; 29 int n,k; 30 31 bool check(int mid) { 32 int cnt = 1; 33 for (int i = 1;i <= n;i++) { 34 if ((cnt & 1)) { 35 if (a[i] <= mid) 36 cnt++; 37 } 38 else 39 cnt++; 40 } 41 if (cnt > k) 42 return true; 43 cnt = 1; 44 for (int i = 1;i <= n;i++) { 45 if (!(cnt & 1)) { 46 if (a[i] <= mid) 47 cnt++; 48 } 49 else 50 cnt++; 51 } 52 if (cnt > k) 53 return true; 54 return false; 55 } 56 int main() { 57 ios::sync_with_stdio(false); 58 cin >> n >> k; 59 int l = 1,r = 1; 60 for (int i = 1;i <= n;i++) { 61 cin >> a[i]; 62 r = max(r,a[i]); 63 } 64 int ans = r; 65 while (l <= r) { 66 int mid = (l + r) >> 1; 67 if (check(mid)) { 68 ans = mid; 69 r = mid - 1; 70 } 71 else 72 l = mid + 1; 73 } 74 cout << ans << endl; 75 return 0; 76 }
E. Binary Subsequence Rotation
题目大意:
想法:
只有 a 和 b 不同的时候我们才需要考虑去旋转 【也就是两种情况】
a: 1 0
b: 0 1
这种情况我们需要去旋转。也就是 最近的 1(0) 考虑去和 0(1)进行旋转 ,我们考虑 10 这种 和 01 这种的旋转就可以了。
#include <cstdio> #include <iostream> #include <algorithm> #include <vector> #include <cmath> #define ll long long #define ull unsigned long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f #define max(a, b) (a>b?a:b) #define min(a, b) (a<b?a:b) const double eps = 1e-8; const int maxn = 1e5 + 10; const ll MOD = 1e9 + 7; const int mlog=20; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; string a,b; cin >> a >> b; int cnt = 0; for (int i = 0;i < n;i++) { cnt += (a[i] - '0'); } for (int i = 0;i < n;i++) { cnt -= (b[i] - '0'); } if (cnt != 0) { cout << -1 << endl; return 0; } cnt = 0; int ans = 0; for (int i = 0;i < n;i++) { if (a[i] != b[i] && (a[i]-'0')) cnt++; if (a[i] != b[i] && !(a[i]-'0')) cnt--; if (cnt < 0) cnt = 0; ans = max(ans,cnt); } cnt = 0; for (int i = 0;i < n;i++) { if (a[i] != b[i] && (a[i]-'0')) cnt--; if (a[i] != b[i] && !(a[i]-'0')) cnt++; if (cnt < 0) cnt = 0; ans = max(ans,cnt); } cout << ans << endl; return 0; }