# Codeforces 1305 (div1+div2) 题解 (ABCDEFG)

div1,2合场。我感觉这次比赛总体不难，考察思维为主

## A. Kuroni and the Gifts

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
typedef long long ll;
ll read(){ll x; if(scanf("%lld",&x)==-1)exit(0); return x;}
const int N=100010;
int a[N],b[N];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
while(t--){
sort(a,a+n); sort(b,b+n);
repeat(i,0,n)printf("%d ",a[i]); puts("");
repeat(i,0,n)printf("%d ",b[i]); puts("");
}
return 0;
}


## B. Kuroni and Simple Strings

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
string s;
int count(int l,int r,char c){ //在区间[l,r]内有多少个字符c
int ans=0;
repeat(i,l,r+1)
ans+=s[i]==c;
return ans;
}
void print(int l,int r,char c){ //输出区间[l,r]内所有字符c的位置
repeat(i,l,r+1)
if(s[i]==c)
cout<<i+1<<' ';
}
int len;
int solve(){ //找到临界位置
cin>>s;
len=s.length();
repeat(i,-1,len){
if(count(0,i,'(')==count(i+1,len-1,')'))
return i;
}
return -1; //程序根本不会运行到这里所以请忽略，只是为了防止烦人的编译器警告
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int p=solve(),cnt=count(0,p,'(');
if(cnt==0)cout<<0<<endl;
else{
cout<<1<<endl;
cout<<cnt*2<<endl;
print(0,p,'(');
print(p+1,len-1,')');
}
return 0;
}


## C. Kuroni and Impossible Calculation

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
typedef long long ll;
const int N=200010;
int a[N],h[N],cnt[N];
int n,m;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
repeat(i,0,n)cin>>a[i],cnt[a[i]%m]++,h[a[i]%m]=a[i];
repeat(i,0,m)if(cnt[i]>1)cout<<0<<endl,exit(0);
ll ans=1%m;
repeat(i,0,m)
if(cnt[i])
repeat(j,i+1,m)
if(cnt[j]){
if(h[i]>h[j])
ans=ans*(i-j)%m;
else
ans=ans*(j-i)%m;
}
cout<<(ans+m)%m<<endl;
return 0;
}


## D. Kuroni and the Celebration

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
typedef long long ll;
const int N=1010;
int n; set<int> a[N]; //用set存不常见，注意风险
int x,ans;
void q(){
if(a[x].empty()){ //如果x没边了
cout<<"! "<<x<<endl;
exit(0);
}
int y=*a[x].begin();
if(a[x].size()==1 && a[y].size()==1){ //如果x只连y，y只连x
cout<<"? "<<x<<' '<<y<<endl;
cout.flush();
cin>>ans;
cout<<"! "<<ans<<endl;
exit(0);
}
if(a[y].size()==1){ //如果y只有一条边（找不到z）就把y当成x，重新来一次
x=y;
return;
}
int z=*a[y].begin();
if(z==x)z=*(++a[y].begin());
cout<<"? "<<x<<' '<<z<<endl;
cout.flush();
cin>>ans;
//砍边时刻
a[x].erase(y);
a[y].erase(x);
a[y].erase(z);
a[z].erase(y);
x=ans;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
repeat(i,0,n-1){
int x,y; cin>>x>>y;
a[x].insert(y);
a[y].insert(x);
}
x=1;
while(1)q();
return 0;
}


## E. Kuroni and the Score Distribution

（为什么要 $1e4$ 这么多呢，因为我可以保证前面任意一个数都小于 $1e4$

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
typedef long long ll;
const int N=1010;
int n,m;
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
repeat(i,1,n+1){
int l=i%2,r=i-2;
int s=(l+r)*(r-l+2)/4;
if(s>=m){
repeat(j,1,i)cout<<j<<' ';
cout<<i+(s-m)*2<<' ';
int k=9e8;
repeat(j,0,n-i)
cout<<k<<' ',k+=10000;
exit(0);
}
}
cout<<-1<<endl;
return 0;
}


## F. Kuroni and the Punishment

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
typedef long long ll; const int inf=~0u>>2;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=200010;
#define int ll
set<int> s;
int n,a[N];
void fac(int n){
for(int i=2;i*i<=n;i++)
if(n%i==0){
s.insert(i);
while(n%i==0)n/=i;
}
if(n>1)s.insert(n);
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
repeat(i,0,n)cin>>a[i];
repeat_back(i,3,n)swap(a[i],a[rnd()%i]);
repeat(i,0,min(30ll,n)){
fac(a[i]);
fac(a[i]+1);
fac(a[i]-1);
}
int ans=inf;
for(auto d:s){
int now=0;
repeat(i,0,n){
if(a[i]<d)now+=d-a[i];
else now+=min(a[i]%d,d-a[i]%d);
}
ans=min(ans,now);
}
cout<<ans<<endl;
return 0;
}


## G. Kuroni and Antihype

#include <bits/stdc++.h>
using namespace std;
#define repeat(i,a,b) for(int i=(a),_=(b);i<_;i++)
#define repeat_back(i,a,b) for(int i=(b)-1,_=(a);i>=_;i--)
typedef long long ll;
const int N=1<<18;
struct DSU{ //合并：d[x]=d[y]，查找：d[x]==d[y]
int a[N];
void init(int n){iota(a,a+n+1,0);}
int &operator[](int x){
return a[x]==x?a[x]:(a[x]=(*this)[a[x]]);
}
}d;
ll ans=0;
int n,cnt[N],vis[N];
int getcnt(int x){
if(vis[x])return 1;
vis[x]=1;
return cnt[x];
}
int connect(int x,int p){
x=d[x]; p=d[p]; if(x==p)return 0;
d[x]=p;
return getcnt(x)+getcnt(p)-1;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
d.init(N-1);
repeat(i,0,n){
int x; cin>>x;
cnt[x]++;
ans-=x;
}
cnt[0]++;
repeat_back(s,0,N)
for(int x=s;x;x=(x-1)&s){ //遍历子集
int p=s^x; //x,p可以互相邀请，他们的边权为s
if(cnt[x] && cnt[p]){
ans+=(ll)s*connect(x,p);
}
}
cout<<ans<<endl;
return 0;
}

posted @ 2020-03-04 14:51  axiomofchoice  阅读(476)  评论(0编辑  收藏