Codeforces Round 892 (Div. 2)题解记录
题目链接:https://codeforces.com/contest/1859
A. United We Stand
选最大的数即可注意题目输出格式
#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[2005];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll cnt=0;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]==a[1])cnt++;
}
if(cnt==n)cout<<-1<<endl;
else
{
sort(a+1,a+1+n);
cnt=0;
for(ll i=1;i<=n;i++)if(a[i]==a[n])cnt++;
cout<<n-cnt<<" "<<cnt<<endl;
for(ll i=1;i<=n;i++)
{
if(a[i]!=a[n])
cout<<a[i]<<" ";
}
cout<<endl;
for(ll i=1;i<=n;i++){
if(a[i]==a[n])
cout<<a[n]<<" ";
}
cout<<endl;
}
}
}
B. Olya and Game with Arrays
翻译有点难看,意思对于每个数组最多可以转移一个数到其他的数组
首先所有数组中的最小元素一定会拿,所以就找第二小的最小值,然后都丢到那个数组去即可
#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[250005];
set<pair<ll,ll>>q[250000];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll ans=0;
for(ll j=1;j<=n;j++)//模拟折返点
{
ll cnt=0;
ll u=0;
for(ll k=1;k<j;k++)
{
cnt+=k*k;
u=max(u,k*k);
}
for(ll k=j;k<=n;k++)
{
cnt+=k*(n-k+j);
u=max(u,k*(n-k+j));
}
ans=max(ans,cnt-u);
}
cout<<ans<<endl;
}
}
C. Another Permutation Problem
题目数据范围小,模拟所有折返点即可
#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[250005];
set<pair<ll,ll>>q[250000];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
ll ans=0;
for(ll j=1;j<=n;j++)//模拟折返点
{
ll cnt=0;
ll u=0;
for(ll k=1;k<j;k++)
{
cnt+=k*k;
u=max(u,k*k);
}
for(ll k=j;k<=n;k++)
{
cnt+=k*(n-k+j);
u=max(u,k*(n-k+j));
}
ans=max(ans,cnt-u);
}
cout<<ans<<endl;
}
}
D. Andrey and Escape from Capygrad
维护最大位置,用栈带着排好顺序的数跑,如果维护的最大位置<=此时位置那就丢出stack在这个最大位置,剩下的小于\(a[i]\)且没加入\(stack\)的
就不用变位置
#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;
}
map<ll,ll>g1,g2;
map<ll,ll>k;
set<ll>f;
struct s
{
ll x,y;
}p[850000];
ll a[850000];
ll ans[850000];
bool cmp(s x,s y)
{
return x.x<y.x;
}
int main()
{
fio();
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
g1.clear();
f.clear();
k.clear();
for(ll i=1;i<=n;i++)
{
ll l1,r1,a1,b1;
cin>>l1>>r1>>a1>>b1;
g1[l1]=max(g1[l1],b1);
k[l1]++;//大左边界
f.insert(r1);
f.insert(l1);
f.insert(b1);
}
ll oi=0;
for(auto j:f)
{
oi++;
a[oi]=j;
}
ll uo;
cin>>uo;
for(ll i=1;i<=uo;i++)
{
ll ko;
cin>>ko;
p[i].x=ko,p[i].y=i;
}
sort(p+1,p+1+uo,cmp);
ll po=1;
ll cnt=0;
stack<pair<ll,ll>>cf;
for(ll i=1;i<=oi;i++)
{
if(i==1)
{
while(p[po].x<a[i]&&po<uo+1)
{
ans[p[po].y]=p[po].x;
po++;
}
if(k[a[i]]>0)
{
cnt=max(cnt,g1[a[i]]);
}
while(p[po].x<=cnt&&po<uo+1)
{
cf.push({p[po].x,p[po].y});
po++;
}
if(cnt==a[i])
{
while(!cf.empty())
{
ans[cf.top().second]=cnt;
cf.pop();
}
}
}
else if(i==oi)//大右边界
{
//cout<<cf.size()<<endl;
if(cnt<a[i])
{
while(p[po].x<a[i]&&po<uo+1)
{
ans[p[po].y]=p[po].x;
po++;
}
while(!cf.empty())
{
ans[cf.top().second]=cnt;
cf.pop();
}
}
//cout<<cf.size()<<endl;
if(k[a[i]]>0)
{
cnt=max(cnt,g1[a[i]]);
}
while(p[po].x<=cnt&&po<uo+1)
{
cf.push({p[po].x,p[po].y});
po++;
}
if(cnt==a[i])
{
while(!cf.empty())
{
ans[cf.top().second]=cnt;
cf.pop();
}
}
while(po<uo+1)
{
ans[p[po].y]=p[po].x;
po++;
}
}
else if(i!=oi)
{
//cout<<po<<endl;
if(cnt<a[i])
{
while(p[po].x<a[i]&&po<uo+1)
{
ans[p[po].y]=p[po].x;
po++;
}
while(!cf.empty())
{
ans[cf.top().second]=cnt;
cf.pop();
}
}
//cout<<po<<endl;
//cout<<p[po].x<<endl;
/*if(a[i]==16)
{
cout<<cf.size()<<endl;
cout<<po<<endl;
}*/
if(k[a[i]]>0)
{
cnt=max(cnt,g1[a[i]]);
}
//cout<<a[i]<<endl;
while(p[po].x<=cnt&&po<uo+1)
{
cf.push({p[po].x,p[po].y});
po++;
}
//cout<<po<<endl;
if(cnt==a[i])
{
while(!cf.empty())
{
ans[cf.top().second]=cnt;
cf.pop();
}
}
}
}
for(ll i=1;i<=uo;i++)cout<<ans[i]<<" ";
cout<<endl;
}
}