A、Creep
瞎搞,发现0101,最后剩下的差值的绝对值一定是最小的
#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int,int>PII;
const int M=2e5+5;
int a[M];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--)
{
int n,m;cin>>n>>m;
int f=1;
while(n||m)
{
if(f)
{
cout<<"0";
if(m)f=0;
n--;
}
else{
cout<<"1";
m--;
if(n)f=1;
}
}
cout<<"\n";
}
return 0;
}
B、Paranoid String
双指针,记得开ll
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
const int M=2e6+5;
int a[M];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--)
{
int n;cin>>n;
char x[M];cin>>x+1;
int l=1,r=1;
int ans=n;
while(r<=n)
{
if(x[l]!=x[r])ans+=r-1,l=r;
r++;
}
cout<<ans<<"\n";
}
return 0;
}
C、Directional Increase
倒着模拟一下
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
const int M=2e6+5;
int a[M],now[M];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--)
{
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>a[i],now[i]=0;
int r=n;bool flag=1;
while(a[r]==0&&r)r--;
for(int i=1;i<r;i++)now[i]++;
while(r){
if(now[r]<=a[r]&&r!=1)flag=0;
if(r==1&&now[r]!=a[r])flag=0;
now[r-1]+=now[r]-a[r]-1;
r--;
}
if(flag==0)cout<<"No\n";else cout<<"Yes\n";
}
return 0;
}
D、Fake Plastic Trees
从叶子结点开始往上做,叶子节点必定会+1,之后就是看从路径上某一个点到叶子节点的所有点的总和是否满足大于该节点的最小范围l,满足的话继续向上模拟,不满足的话就说明需要开一条新的到这个节点的路径,使得这个节点满足大于最小范围l的条件。
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
const int M=2e5+5;
int a[M],ans;
vector<int>v[M];
int l[M],r[M];
void dfs(int u)
{
int s=0;
for(int i=0;i<v[u].size();i++)
{
int j=v[u][i];
dfs(j);
s+=a[j];
}
if(s>=l[u])a[u]=min(r[u],s);
else {
ans++;
a[u]=r[u];
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;
while(T--)
{
ans=0;
int n;cin>>n;
for(int i=2;i<=n;i++){
int t;cin>>t;
v[t].push_back(i);
}
for(int i=1;i<=n;i++)cin>>l[i]>>r[i];
dfs(1);
for(int i=1;i<=n;i++)a[i]=0,v[i].clear();
cout<<ans<<"\n";
}
return 0;
}
后面没补