A. Creep
思路:01插孔放
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int a,b;
void solve()
{
cin>>a>>b;
int c=min(a,b);
if(a<b)
{
for(int i=1;i<=c;i++)cout<<"10";
for(int i=1;i<=b-c;i++)cout<<"1";
}
else
{
for(int i=1;i<=c;i++)cout<<"01";
for(int i=1;i<=a-c;i++)cout<<"0";
}
cout<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
B. Paranoid String
思路:逆向思维,以两个相邻相同元素结尾的子段不成立,总方案减去即可
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
void solve()
{
cin>>n;
string s;
cin>>s;
int sum=n*(n+1)/2;
for(int i=1;i<n;i++)
if(s[i]==s[i-1])sum-=i;
cout<<sum<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
C. Directional Increase
最后一个不为0的数一定是负数(长度大于1)
数组前缀和一定为0
最后一个负数最大
否则不满足
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=2e5+10;
int s[N];
void solve()
{
memset(s,0,sizeof s);
cin>>n;
vector<int>v(n+1);
for(int i=1;i<=n;i++)
{
cin>>v[i];
s[i]=s[i-1]+v[i];
}
bool f=false;
int idx=1;
for(int i=n;i;i--)
if(v[i])
{
idx=i;
break;
}
if(s[n])f=true;
for(int i=1;i<=n;i++)
{
if(s[i]<=0&&i!=idx)f=true;
else if(i==idx)break;
}
if(f)cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}
D. Fake Plastic Trees
叶子节点必须操作一次
贪心
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n;
const int N=1e6+10;
int h[N],e[N],ne[N],w[N],idx;
int l[N],r[N];
int res;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int dfs(int u,int fa)
{
int s=0;
for(int i=h[u];~i;i=ne[i])
{
int j=e[i];
if(j==fa)continue;
s+=dfs(j,u);
}
if(h[u]==-1)
{
res++;
return r[u];
}
if(s>=r[u])return r[u];
if(s>=l[u]) return s;
res++;
return r[u];
}
void solve()
{
cin>>n;
idx=0;
res=0;
memset(h,-1,sizeof h);
for(int i=2;i<=n;i++)
{
int x;
cin>>x;
add(x,i);
}
for(int i=1;i<=n;i++)cin>>l[i]>>r[i];
dfs(1,-1);
cout<<res<<endl;
}
signed main()
{
io;
cin>>_;
while(_--)
solve();
return 0;
}