2020牛客多校第3场[Clam and Fish+贪心]
程序员文章站
2022-08-14 10:16:18
题目链接解题思路:1.根据贪心的思想肯定是如果是第2或者第3种状态肯定是钓鱼且不需要鱼饵的,如果是在第0种状态有鱼饵肯定也钓鱼,对于第1种状态就是可以钓鱼也可以造鱼饵的状态我们如何考虑2.我们从后面看需要鱼饵钓鱼的有第0和第1种状态如果后面这两种状态的个数大于当前的鱼饵个数那么我们就继续造鱼饵,否则我们就直接钓鱼,因为鱼饵太多留着也没用#include #include #include #include...
解题思路:1.根据贪心的思想肯定是如果是第2或者第3种状态肯定是钓鱼且不需要鱼饵的,如果是在第0种状态有鱼饵肯定也钓鱼,对于第1种状态就是可以钓鱼也可以造鱼饵的状态我们如何考虑
2.我们从后面看需要鱼饵钓鱼的有第0和第1种状态如果后面这两种状态的个数大于当前的鱼饵个数那么我们就继续造鱼饵,否则我们就直接钓鱼,因为鱼饵太多留着也没用
#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 2e6+10, mod = 1e9 + 7;
const double eps = 1e-10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{
read(first);
read(args...);
}
char a[N];
int Suff[N];
int fish, clam;
int T;
int main()
{
read(T);
while(T --)
{
int n;
fish = clam = 0;
read(n);
cin >> a;
int len = strlen(a);
Suff[len] = 0;
for(int i = len - 1; i >= 0; -- i)
if(a[i] == '0' || a[i] == '1')
Suff[i] = Suff[i + 1] + 1;
else Suff[i] = Suff[i + 1];
for(int i = 0; i < len; ++ i)
{
if(a[i] == '0')
{
if(clam) clam --, fish ++;
}
else if(a[i] == '1')
{
if(Suff[i] > clam) clam ++;
else
{
if(clam) clam --, fish ++;
}
}
else fish ++;
}
cout << fish << endl;
}
return 0;
}
本文地址:https://blog.csdn.net/weixin_45630722/article/details/107456595
推荐阅读
-
牛客多校第三场 A-Clam and Fish【贪心】+ B-Classical String Problem【思维】
-
2020牛客多校第3场[Clam and Fish+贪心]
-
2020牛客多校第3场:Two Matchings[找规律+dp]
-
2020牛客多校第3场:[Points Construction Problem + 思维题+构造]
-
2020牛客暑期多校第二场 C - Cover the Tree(思维/贪心)
-
牛客多校第三场 A-Clam and Fish【贪心】+ B-Classical String Problem【思维】
-
2020牛客多校第3场[Clam and Fish+贪心]
-
2020牛客多校第3场:Two Matchings[找规律+dp]
-
2020牛客多校第3场:[Points Construction Problem + 思维题+构造]
-
2020牛客暑期多校第二场 C - Cover the Tree(思维/贪心)