题目链接:https://codeforces.com/contest/1858
A. Buttons
从自身角度出发,我想赢就得保证我的按钮尽可能多所以,大家都优先按\(c\),然后分析先后顺序即可
#include<iostream>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<stack>
#include<queue>
#include<algorithm>
#include<time.h>
#include<random>
#include<string>
#include<string.h>
#include<cctype>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll gcd(ll x, ll y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
ll ksm(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans = ans%mod*(x%mod)%mod;
x = x%mod*(x%mod)%mod;
y >>= 1;
}
return ans%mod%mod;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
ll a,b,c;
cin>>a>>b>>c;
//swap(b,c);
ll j=c%2;
if(j==0)
{
if(a<=b)
{
cout<<"Second"<<endl;
}
else
cout<<"First"<<endl;
}
else
{
if(b<=a)
{
cout<<"First"<<endl;
}
else
cout<<"Second"<<endl;
}
}
}
B. The Walkway
题目乍看比较繁琐且翻译有问题?
一个饼干店的有无只会影响周围饼干店到这个饼干店的途中的吃饼干次数
分类讨论下即可
#include<iostream>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<stack>
#include<queue>
#include<algorithm>
#include<time.h>
#include<random>
#include<string>
#include<string.h>
#include<cctype>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll gcd(ll x, ll y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
ll ksm(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans = ans%mod*(x%mod)%mod;
x = x%mod*(x%mod)%mod;
y >>= 1;
}
return ans%mod%mod;
}
ll a[250000];
ll b[250000];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m,d;
cin>>n>>m>>d;
for(ll i=0;i<=m;i++)b[i]=0;
for(ll i=1;i<=m;i++)
{
cin>>a[i];
}
ll op=0;ll k=0;
ll ok=0;
for(ll i=1;i<=m;i++)
{
ll ans=0;
ll cnt=0;
if(i==1)
{
if(a[i]==1)
{
op=0;
k=1;
ok++;
}
else
{
ans=(a[i]-1-1)/d+2+((a[i+1]-1)-a[i])/d+1;
cnt=(a[i+1]-1-1)/d+2;
//cout<<ans<<" "<<cnt<<endl;
ok+=(a[i]-1-1)/d+2;
if(ans-cnt>op)
{
op=ans-cnt;
k=1;
}
else if(ans-cnt==op)k++;
}
}
else if(i==m)
{
ans=(a[i]-1-a[i-1])/d+1+(n-a[i])/d;
cnt=(n-a[i-1])/d;
ok+=ans;
if(ans-cnt>op)
{
op=ans-cnt;
k=1;
}
else if(ans-cnt==op)k++;
}
else
{
ans=(a[i]-1-a[i-1])/d+1+(a[i+1]-1-a[i])/d+1;
cnt=(a[i+1]-1-a[i-1])/d+1;
ok+=(a[i]-1-a[i-1])/d+1;
if(ans-cnt>op)
{
op=ans-cnt;
k=1;
}
else if(ans-cnt==op)k++;
}
}
// cout<<ok<<endl;
cout<<ok-op<<" "<<k<<endl;
}
}
C. Yet Another Permutation Problem
优先考虑二倍递增,这样子一定gcd种类最多
#include<iostream>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<stack>
#include<queue>
#include<algorithm>
#include<time.h>
#include<random>
#include<string>
#include<string.h>
#include<cctype>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll gcd(ll x, ll y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
ll ksm(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans = ans%mod*(x%mod)%mod;
x = x%mod*(x%mod)%mod;
y >>= 1;
}
return ans%mod%mod;
}
ll a[250000];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
//map<ll,ll>q;
set<ll>f;
for(ll i=1;i<=n;i++)
{
f.insert(i);
}
ll pd=0;
for(ll i=1;i<=n;i++)
{
if(pd==0)
{
pd=*f.begin();
a[i]=pd;
f.erase(pd);
}
else
{
while(pd<=n)
{
pd*=2;
if(f.find(pd)!=f.end())
{
a[i]=pd;
f.erase(pd);
break;
}
}
if(pd>n)
{
pd=*f.begin();
a[i]=pd;
f.erase(pd);
}
}
}
//cout<<f.size()<<endl;
for(ll i=1;i<=n;i++)cout<<a[i]<<" ";
cout<<endl;
}
}
D. Trees and Segments
首先发现如果要求出\(a⋅l0+l1\)的最大值,并不是\(l0\)越大越好。所以考虑前缀求出每个\(l0\)对应的每个\(l1\)最大值
由于不能\((n^3)\),所以直接用前缀和后缀数组去维护从某个点(且有多少改变机会)出发后的最长冷杉长度。
于是最后暴力下\(l0\)对应的\(l1\)最大值,答案得解
#include<iostream>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<unordered_set>
#include<stack>
#include<queue>
#include<algorithm>
#include<time.h>
#include<random>
#include<string>
#include<string.h>
#include<cctype>
using namespace std;
typedef int ll;
mt19937 rnd(time(0));
const ll mod = 1e9 + 7;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll gcd(ll x, ll y)
{
if (y == 0)
return x;
else
return gcd(y, x % y);
}
ll ksm(ll x, ll y)
{
ll ans = 1;
while (y)
{
if (y & 1)
ans = ans%mod*(x%mod)%mod;
x = x%mod*(x%mod)%mod;
y >>= 1;
}
return ans%mod%mod;
}
ll a[3500];
ll b[3005][3005];
ll c[3005][3005];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,g;
cin>>n>>g;
string f;
cin>>f;
for(ll i=0;i<=n+1;i++)
{
for(ll k=0;k<=g+1;k++)
{
b[i][k]=0;
c[i][k]=0;
}
}
//cout<<99<<endl;
for(ll i=0;i<=g;i++)
{
ll sz=0;
deque<ll>q;
for(ll j=0;j<n;j++)
{
if(q.empty())
{
q.push_back(j);
if(f[j]=='0')sz++;
while(sz>i)
{
if(f[q.front()]=='0')sz--;
b[q.front()][i]=max(b[q.front()][i],(ll)q.size()-1);
q.pop_front();
}
}
else
{
q.push_back(j);
if(f[j]=='0')sz++;
while(sz>i)
{
if(f[q.front()]=='0')sz--;
b[q.front()][i]=max(b[q.front()][i],(ll)q.size()-1);
q.pop_front();
}
}
if(!q.empty())
b[q.front()][i]=max((ll)q.size(),b[q.front()][i]);
}
while(!q.empty())
{
b[q.front()][i]=max(b[q.front()][i],(ll)q.size());
q.pop_front();
}
for(ll j=n-1;j>=0;j--)b[j][i]=max(b[j+1][i],b[j][i]);
}
for(ll i=0;i<=g;i++)
{
ll sz=0;
deque<ll>q;
for(ll j=n-1;j>=0;j--)
{
if(q.empty())
{
q.push_back(j);
if(f[j]=='0')sz++;
while(sz>i)
{
if(f[q.front()]=='0')sz--;
c[q.front()][i]=max(c[q.front()][i],(ll)q.size()-1);
q.pop_front();
}
}
else
{
q.push_back(j);
if(f[j]=='0')sz++;
while(sz>i)
{
if(f[q.front()]=='0')sz--;
c[q.front()][i]=max(c[q.front()][i],(ll)q.size()-1);
q.pop_front();
}
}
if(!q.empty())
c[q.front()][i]=max((ll)q.size(),c[q.front()][i]);
}
while(!q.empty())
{
c[q.front()][i]=max(c[q.front()][i],(ll)q.size());
q.pop_front();
}
for(ll j=1;j<=n-1;j++)c[j][i]=max(c[j-1][i],c[j][i]);
}
for(ll i=0;i<=n-1;i++)
{
for(ll j=g;j>=1;j--)
{
b[i][j]=max(b[i][j-1],b[i][j]);
c[i][j]=max(c[i][j],c[i][j-1]);
}
}
// cout<<b[0][0]<<endl;
/*for(ll i=0;i<n;i++)
{
for(ll j=0;j<=g;j++)
cout<<b[i][j]<<" "<<c[i][j]<<endl;
}*/
pair<ll,ll>ans[5000];
ll op=0;
op=1;
ans[op]={0,b[0][g]};
//cout<<99<<endl;
for(ll i=1;i<=n;i++)
{
ll cnt=0;
ll sz=0;
ll u=0;
ll pd=0;
deque<ll>q;
for(ll j=0;j<n;j++)
{
q.push_back(j);
if(f[j]=='1')sz++;
while(sz>g)
{
if(f[q.front()]=='1')sz--;
q.pop_front();
}
while((ll)q.size()>i)
{
if(f[q.front()]=='1')sz--;
q.pop_front();
}
if(q.size()==i)
{
pd=1;
if(q.front()==0&&q.back()==n-1)
u=max(u,0);
else if(q.front()==0)
u=max(u,b[q.back()+1][g-sz]);
else if(q.back()==n-1)
u=max(u,c[q.front()-1][g-sz]);
else
u=max({u,b[q.back()+1][g-sz],c[q.front()-1][g-sz]});
}
}
if(pd)
{
op++;
ans[op]={i,u};
}
}
//cout<<55<<endl;
for(ll i=1;i<=n;i++)
{
ll u=0;
for(ll j=1;j<=op;j++)
{
ll x=ans[j].first;
ll y=ans[j].second;
u=max(u,i*x+y);
}
cout<<u<<" ";
}
cout<<endl;
}
}
标签:sz,893,题解,ll,Codeforces,ans,front,include,mod
From: https://www.cnblogs.com/cjcf/p/18473313