2024 ICPC Asia Taiwan Online Programming Contest题解记录
比赛链接:https://codeforces.com/gym/105383/problem
A. Animal Farm
找个最大pig,然后所有比他小的其他种类生物一直加就好了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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;
}
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
string f[150000];
ll a[150000];
int main()
{
fio();
ll n;
cin>>n;
ll cnt=0;
for(ll i=1;i<=n;i++)
{
cin>>f[i]>>a[i];
if(f[i]=="pig")
{
cnt=max(cnt,a[i]);
}
}
ll ans=cnt;
for(ll i=1;i<=n;i++)
{
if(f[i]!="pig"&&a[i]<cnt)
{
ans+=a[i];
}
}
cout<<ans<<endl;
}
B. Business Magic
先把负数乘3倍,然后算最大和区间,如果和为正数则这段区间全乘二,且所有负数变成正数;否则所有负数变正数加起来就可以了
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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;
}
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll a[450000];
ll b[450000];
int main()
{
fio();
ll n;
cin>>n;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]<0)b[i]=3*a[i];
else b[i]=a[i];
}
ll l=0,r=0,cnt=0;
ll f=0;
deque<ll>q;
for(ll i=1;i<=n;i++)
{
if(q.empty())
{
q.push_back(i);
f+=b[i];
while(!q.empty()&&f<0)
{
f-=b[q.front()];
q.pop_front();
}
if(!q.empty())
{
if(f>cnt)
{
cnt=f;
l=i,r=i;
}
}
}
else
{
q.push_back(i);
f+=b[i];
while(!q.empty()&&f<0)
{
f-=b[q.front()];
q.pop_front();
}
if(!q.empty())
{
if(f>cnt)
{
cnt=f;
l=q.front(),r=q.back();
}
}
}
}
//cout<<l<<" "<<r<<endl;
if(l==0&&r==0)
{
ll ans=0;
for(ll i=1;i<=n;i++)ans+=abs(a[i]);
cout<<ans<<endl;
}
else
{
ll ans=0;
for(ll i=1;i<=n;i++)
{
if(i>=l&&i<=r)
ans+=a[i]*2;
else
ans+=abs(a[i]);
}
cout<<ans<<endl;
}
}
D. Disbursement on Quarantine Policy
分类讨论即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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;
}
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll add(ll x,ll y)
{
return (x%mod+y%mod)%mod;
}
ll p1=ksm(2,mod-2)%mod;
string f[6000];
ll ans=0;
ll n,m,d1,d2,d3;
ll a[4]={0,1,0,-1};
ll b[4]={1,0,-1,0};
ll c[4]={-1,1,1,-1};
ll d[4]={-1,-1,1,1};
void ck(ll x,ll y)
{
if(f[x][y]=='V')
{
ans=(ans%mod+d1%mod)%mod;
return ;
}
else if(f[x][y]=='?')
{
ll nx;
ll ny;
ll pd=0;
ll pd1=0;
ll cnt=0;
ll cnt1=0;
for(ll i=0;i<=3;i++)
{
nx=x+a[i];
ny=y+b[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m)//四角
{
if(f[nx][ny]=='V')pd=1;
if(f[nx][ny]=='?')cnt++;
}
}
for(ll i=0;i<=3;i++)
{
nx=x+c[i];
ny=y+d[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m)//八角
{
if(f[nx][ny]=='V')pd1=1;
if(f[nx][ny]=='?')cnt1++;
}
}
if(pd)//四角有v
{
ll u=(p1*d1)%mod;
ans=add(ans,u);
ll k=(p1*d2)%mod;
ans=add(ans,k);
return ;
}
else if(pd1)//八角
{
ans=(ans%mod+p1*d1%mod)%mod;
ll u=((((1-ksm(p1,cnt)%mod+1000*mod)%mod%mod)*d2%mod)%mod+1000*mod)%mod;
ll y=(ksm(p1,cnt)%mod*d3%mod)%mod;
ans=(ans%mod+(p1%mod*((u%mod+y%mod)%mod))%mod)%mod;
}
else
{
ans=(ans%mod+p1*d1%mod)%mod;
ll u=(((1-ksm(p1,cnt)%mod+1000*mod)%mod)*d2%mod+1000*mod)%mod;
ll y=(((1-ksm(p1,cnt1)%mod+1000*mod)%mod)*d3%mod+1000*mod)%mod;
y=(y%mod*ksm(p1,cnt)%mod)%mod;
ans=(ans%mod+(p1%mod*((u%mod+y%mod)%mod))%mod)%mod;
}
return ;
}
else
{
ll nx;
ll ny;
ll pd=0;
ll pd1=0;
ll cnt=0;
ll cnt1=0;
for(ll i=0;i<=3;i++)
{
nx=x+a[i];
ny=y+b[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
{
if(f[nx][ny]=='V')pd=1;
if(f[nx][ny]=='?')cnt++;
}
}
for(ll i=0;i<=3;i++)
{
nx=x+c[i];
ny=y+d[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
{
if(f[nx][ny]=='V')pd1=1;
if(f[nx][ny]=='?')cnt1++;
}
}
if(pd)
{
ans=(ans%mod+d2%mod)%mod;
}
else if(pd1)
{
ll u=(((1-ksm(p1,cnt)%mod+1000*mod)%mod)*d2%mod+1000*mod)%mod;
ll y=(((ksm(p1,cnt)%mod)%mod)*d3%mod)%mod;
ans=(ans%mod+(u%mod+y%mod)%mod)%mod;
}
else
{
ll u=(((1-ksm(p1,cnt)%mod+1000*mod)%mod)*d2%mod+1000*mod)%mod;
ll y=(ksm(p1,cnt)%mod*d3%mod)%mod;
y=(y%mod*((1-ksm(p1,cnt1)+1000*mod)%mod)%mod)%mod;
ans=(ans%mod+(u%mod+y%mod)%mod)%mod;
}
return ;
}
}
int main()
{
fio();
// cout<<p1<<endl;
cin>>n>>m>>d1>>d2>>d3;
d1%=mod;
d2%=mod;
d3%=mod;
for(ll i=1;i<=n;i++)cin>>f[i],f[i]='0'+f[i];
for(ll i=1;i<=n;i++)
{
for(ll j=1;j<=m;j++)
{
ck(i,j);
}
}
cout<<ans<<endl;
}
I. In Search of the Lost Array
本题在于如何暴力,直接用因数建立桥梁即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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;
}
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
ll cnt[150000];
ll a[150000];
map<pair<ll,ll>,bool>q;
vector<ll>g[250000];
bool vis[250000];
ll vi[250000];
ll ans[250000];
ll n;
void ck(ll x,ll sd)
{
ans[sd]=x;
for(auto j:g[x])
{
if(vi[j*x]<cnt[j*x])
{
vi[j*x]++;
ck(j,sd+1);
vi[j*x]--;
}
}
if(sd==n)
{
cout<<"Yes"<<endl;
for(ll i=1;i<=n;i++)cout<<ans[i]<<" ";
cout<<endl;
exit(0);
}
}
int main()
{
// ll n;
ll op;
cin>>n;
for(ll i=1;i<n;i++)
{
cin>>a[i];
cnt[a[i]]++;
for(ll j=1;j<=100;j++)
{
if(a[i]%j==0&&a[i]/j<=100)
{
if(q[{j,a[i]/j}]==0)
{
q[{j,a[i]/j}]=1;
g[j].push_back(a[i]/j);
g[a[i]/j].push_back(i);
vis[a[i]/j]=vis[j]=1;
}
}
}
}
for(ll i=1;i<=100;i++)
{
if(vis[i])
ck(i,1);
}
cout<<"No"<<endl;
}
J. Just Round Down
直接long double +long long
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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()
{
long double n;
cin>>n;
cout<<(ll)n<<endl;
}
K. Kingdom's Development Plan
和codeforces div3的一道题很像,于是参考了那个的判环思路,
然后入度+优先队列即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
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;
}
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<ll>g[250000];
bool vis[250000];
bool vi[250000];
ll pd=0;
ll l=0;
ll b[2500000];
ll in[2500000];
ll ans1=0;
vector<ll>op;
ll n,m;
void dfs(ll x)
{
if(vi[x])
{
pd=1;
return ;
}
if(vis[x])return;
vis[x]=1;
ans1++;
vi[x]=1;
for(auto j:g[x])
{
dfs(j);
}
vi[x]=0;
}
void to()
{
priority_queue<ll,vector<ll>,greater<ll>>q;
for(ll i=1;i<=n;i++)
{
if(in[i]==0)
q.push(i);
}
while(!q.empty())
{
ll j=q.top();
q.pop();
for(auto x:g[j])
{
in[x]--;
if(in[x]==0)
{
q.push(x);
}
}
op.push_back(j);
}
}
int main()
{
fio();
cin>>n>>m;
while(m--)
{
ll l,r;
cin>>l>>r;
g[l].push_back(r);
in[r]++;
}
for(ll i=1;i<=n;i++)
{
if(vis[i])continue;
else if(in[i]==0)
{
dfs(i);
}
}
if(pd||ans1!=n)
cout<<"IMPOSSIBLE"<<endl;
else
{
to();
for(auto j:op)
{
cout<<j<<" ";
}
cout<<endl;
}
}