当前位置: 代码迷 >> 综合 >> codeforces B. Substring Removal Game
  详细解决方案

codeforces B. Substring Removal Game

热度:50   发布时间:2024-02-11 14:32:19.0

在这里插入图片描述

题目

题意:

有两个人,每一次都可以取序列中连续的部分,然后取完的两边会相邻,问先取的那个人最多可以取多少个 1 1 ,两个人都取最优。

思路:

我们直接将连续的 1 1 个数全部提取出来,先取的是拿最大的,后取的第二大 . . . . . ..... 直到取完,所以我们取下标为偶数位的就行了(下标 0 0 开始)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {char c;int sgn;if (c = getchar(), c == EOF) return ;while (c != '-' && (c < '0' || c > '9')) c = getchar();sgn = (c == '-') ? -1:1;ret = (c == '-') ? 0:(c - '0');while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return ;
}
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
char s[110];
bool cmp(int a, int b) {return a > b;}
int main() {int t; read(t); while (t--) {scanf("%s", s);veci v;int len = strlen(s), cnt = 0;for (int i = 0; i < len; i++) {if (s[i] == '1') cnt++;else {if (cnt) v.push_back(cnt);cnt = 0;}}if (cnt) v.push_back(cnt);sort(v.begin(), v.end(), cmp);int sum = 0;for (int i = 0; i < v.size(); i += 2) {sum += v[i];}printf("%d\n", sum);}return 0;
}
  相关解决方案