# Gym.102059: 2018-2019 XIX Open Cup, Grand Prix of Korea（寒假gym自训第一场）

## E.Electronic Circuit

（选择set其实比较巧妙，因为不用考虑并联。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=100010;
set<int>S[maxn]; queue<int>q;
int main()
{
int N,M,u,x,y;
scanf("%d%d",&N,&M);
rep(i,1,M){
scanf("%d%d",&x,&y);
S[x].insert(y); S[y].insert(x);
}
rep(i,1,N) if(S[i].size()==2) q.push(i);
while(!q.empty()){
u=q.front(); q.pop();
if(S[u].size()!=2) continue;
x=*S[u].begin(); S[u].erase(S[u].begin());
y=*S[u].begin(); S[u].erase(S[u].begin());
S[x].erase(u); S[y].erase(u);
S[x].insert(y); S[y].insert(x);
if(S[x].size()==2) q.push(x);
if(S[y].size()==2) q.push(y);
}
int cnt=0,ok=1;
rep(i,1,N) {
if(S[i].size()==1) cnt++;
if(S[i].size()>1) ok=0;
}
if(ok&&cnt==2) puts("Yes");
else puts("No");
return 0;
}
View Code

## F .Fake Plastic Trees

（读了好久才懂了题意，主要是输出这里，我们构造的东西每次都是重复利用了之前的子树的。

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=100010;
int x[maxn],y[maxn],tot; map<ll,int>mp;
void get(ll num)
{
if(mp.find(num)!=mp.end()) return ;
if(num==1LL) {
x[++tot]=-1; y[tot]=-1; mp[num]=tot;
return ;
}
ll mid=num/2;
get(num-mid); get(mid);
tot++;  x[tot]=mp[num-mid]; y[tot]=mp[mid];
mp[num]=tot;
}
int main()
{
int T; ll N;
scanf("%d",&T);
while(T--){
scanf("%lld",&N);
tot=-1; mp.clear(); get(N);
printf("%d\n",tot+1);
rep(i,0,tot) printf("%d %d\n",x[i],y[i]);
printf("%d\n",tot);
}
return 0;
}
View Code

## H .Fractions

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int main()
{
ll A,B,C,D,ans=0;
scanf("%lld%lld%lld%lld",&A,&B,&C,&D);
rep(i,1,999)
rep(j,1,999-i){
if((__gcd(i,j))==1){
//A<=ix<=B c<=jx<=D
ll tmp=min(D/j,B/i)-max((A-1)/i,(C-1)/j);
if(tmp>0) ans+=tmp;
}
}
printf("%lld\n",ans);
return 0;
}
View Code

## I .Game on Plane

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=5010;
int sg[maxn],vis[maxn];
void init()
{
sg[1]=0; sg[2]=1;
rep(i,3,5000){
rep(j,0,i-2){
vis[sg[j]^sg[i-2-j]]=i;
}
rep(j,0,5000) {
if(vis[j]!=i) { sg[i]=j; break;}
}
}
}
int main()
{
init();
int T,N; scanf("%d",&T);
while(T--){
scanf("%d",&N);
if(!sg[N]) puts("Second");
else puts("First");
}
return 0;
}
View Code

## L .Timsort

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=100010;
int a[maxn],R[maxn][2],ans[maxn][2];
int main()
{
int N,Q,x;
scanf("%d",&N);
rep(i,1,N) scanf("%d",&a[i]);
rep2(i,N,1) {
R[i][0]=R[i][1]=1;
if(i+1<=N&&a[i]<=a[i+1]) R[i][0]=R[i+1][0]+1;
if(i+1<=N&&a[i]>a[i+1]) R[i][1]=R[i+1][1]+1;
}
scanf("%d",&Q);
while(Q--){
scanf("%d",&x);
if(ans[x][0]) printf("%d %d\n",ans[x][0],ans[x][1]);
else {
int A=0,B=0;
rep(i,1,N){
A++; if(i==N) break;
int t=R[i][a[i]>a[i+1]];
if(t<x){
if(i+x-1<=N) B+=x-t;
else B+=N-(i+t-1);
i=i+x-1;
}
else i=i+t-1;
}
ans[x][0]=A; ans[x][1]=B;
printf("%d %d\n",A,B);
}
}
return 0;
}
View Code

posted @ 2019-01-30 19:18  nimphy  阅读(1065)  评论(0编辑  收藏