1. 程式人生 > >AtCoder Regular Contest 103 題解

AtCoder Regular Contest 103 題解

print -s struct namespace type iostream con clas inline

C-/\/\/\

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define Cpy(x,y) memcpy(x,y,sizeof(x))
#define Set(x,y) memset(x,y,sizeof(x))
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const int N=100010;
const int mod=19940417;
const int base=26;
const dd eps=1e-6;
const int inf=2147483647;
const ll INF=1ll<<60;
const ll P=100000;
il ll read(){
  RG ll data=0,w=1;RG char ch=getchar();
  while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar();
  if(ch==‘-‘)w=-1,ch=getchar();
  while(ch<=‘9‘&&ch>=‘0‘)data=data*10+ch-48,ch=getchar();
  return data*w;
}
 
il void file(){
  srand(time(NULL)+rand());
  freopen(FILE".in","r",stdin);
  freopen(FILE".out","w",stdout);
}
 
int n,t[2][N],pre[2][N],suf[2][N],ans;
 
int main()
{
  n=read();ans=n;
  for(RG int i=1;i<=n;i++)t[i&1][read()]++;
  for(RG int i=1;i<=100000;i++)
    pre[0][i]=max(pre[0][i-1],t[0][i]),pre[1][i]=max(pre[1][i-1],t[1][i]);
  for(RG int i=100000;i;i--)
    suf[0][i]=max(suf[0][i+1],t[0][i]),suf[1][i]=max(suf[1][i+1],t[1][i]);
  for(RG int i=1;i<=100000;i++)
    ans=min(ans,n-(t[0][i]+max(pre[1][i-1],suf[1][i+1])));
  printf("%d\n",ans);
  return 0;
}

D-Robot Arms

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define Cpy(x,y) memcpy(x,y,sizeof(x))
#define Set(x,y) memset(x,y,sizeof(x))
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const int N=100010;
const int mod=19940417;
const int base=26;
const dd eps=1e-6;
const int inf=2147483647;
const ll INF=1ll<<60;
const ll P=100000;
il ll read(){
  RG ll data=0,w=1;RG char ch=getchar();
  while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar();
  if(ch==‘-‘)w=-1,ch=getchar();
  while(ch<=‘9‘&&ch>=‘0‘)data=data*10+ch-48,ch=getchar();
  return data*w;
}
 
il void file(){
  srand(time(NULL)+rand());
  freopen(FILE".in","r",stdin);
  freopen(FILE".out","w",stdout);
}
 
int n,m,x[N],y[N],p[N],q[N],k[N],s[N];
il void er(){puts("-1");exit(0);}
int main()
{
  n=read();m=31;
  for(RG int i=1;i<=n;i++){
    x[i]=read();y[i]=read();
    p[i]=x[i]+y[i];q[i]=x[i]-y[i];
    x[i]=abs(x[i]);y[i]=abs(y[i]);
    if(i!=1&&((x[i-1]+y[i-1])&1)!=((x[i]+y[i])&1))er();
  }
  m+=(!((x[n]+y[n])&1));
  printf("%d\n",m);
  for(RG int i=0;i<31;i++)printf("%d ",1<<i);
  if(!((x[n]+y[n])&1))printf("1 ");puts("");
  for(RG int i=1;i<=n;i++){
    memset(k,0,sizeof(k));memset(s,0,sizeof(s));
    //printf("%d,%d\n",p[i],q[i]);
    if(m==32){k[32]=s[32]=1;p[i]--;q[i]--;}
    for(RG int j=30;~j;j--)
      if(p[i]>0)k[j+1]=1,p[i]-=(1<<j);
      else k[j+1]=0,p[i]+=(1<<j);
    for(RG int j=30;~j;j--)
      if(q[i]>0)s[j+1]=1,q[i]-=(1<<j);
      else s[j+1]=0,q[i]+=(1<<j);
    for(RG int j=1;j<=m;j++)
      if(k[j]&&s[j])putchar(‘R‘);
      else if(k[j]&&!s[j])putchar(‘U‘);
      else if(!k[j]&&s[j])putchar(‘D‘);
      else if(!k[j]&&!s[j])putchar(‘L‘);
    puts("");
  }
  return 0;
}

E-Tr/ee

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define Cpy(x,y) memcpy(x,y,sizeof(x))
#define Set(x,y) memset(x,y,sizeof(x))
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const int N=100010;
const int mod=19940417;
const int base=26;
const dd eps=1e-6;
const int inf=2147483647;
const ll INF=1ll<<60;
const ll P=100000;
il ll read(){
  RG ll data=0,w=1;RG char ch=getchar();
  while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar();
  if(ch==‘-‘)w=-1,ch=getchar();
  while(ch<=‘9‘&&ch>=‘0‘)data=data*10+ch-48,ch=getchar();
  return data*w;
}
 
il void file(){
  srand(time(NULL)+rand());
  freopen(FILE".in","r",stdin);
  freopen(FILE".out","w",stdout);
}
 
int n,m;char s[N];bool vis[N];
struct edge{int u,v;}E[N];
il void er(){puts("-1");exit(0);}
int main()
{
  scanf("%s",s+1);n=strlen(s+1);if(s[n]==‘1‘)er();
  for(RG int i=1,j=n-1;i<=j;i++,j--){
    if(s[i]!=s[j])er();
    else if(s[i]==‘0‘){
      if(i==1)er();     
    }
    else{
      vis[i]=1;
    }
  }
  for(RG int i=n/2+1;i<n;i++)E[++m]=(edge){i,n};
  for(RG int i=n/2,p=n;i;i--){
    E[++m]=(edge){i,p};if(vis[i])p=i;
  }
  for(RG int i=1;i<=m;i++)printf("%d %d\n",E[i].u,E[i].v);
  return 0;
}

F-Distance Sums

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define Cpy(x,y) memcpy(x,y,sizeof(x))
#define Set(x,y) memset(x,y,sizeof(x))
#define FILE "a"
#define mp make_pair
#define pb push_back
#define RG register
#define il inline
using namespace std;
typedef unsigned long long ull;
typedef vector<int>VI;
typedef long long ll;
typedef double dd;
const int N=1<<17;
const int mod=998244353;
const int base=26;
const dd eps=1e-6;
const int inf=2147483647;
const ll INF=1ll<<60;
const ll P=100000;
il ll read(){
  RG ll data=0,w=1;RG char ch=getchar();
  while(ch!=‘-‘&&(ch<‘0‘||ch>‘9‘))ch=getchar();
  if(ch==‘-‘)w=-1,ch=getchar();
  while(ch<=‘9‘&&ch>=‘0‘)data=data*10+ch-48,ch=getchar();
  return data*w;
}
 
il void file(){
  srand(time(NULL)+rand());
  freopen(FILE".in","r",stdin);
  freopen(FILE".out","w",stdout);
}
 
int n,m;ll sz[N],o[N];
struct node{ll d,id;}t[N];
bool cmp_d(node a,node b){return a.d<b.d;}
bool cmp_id(node a,node b){return a.id<b.id;}
il void er(){puts("-1");exit(0);}
struct edge{int u,v;}E[N];
 
int head[N],nxt[N<<1],to[N<<1],cnt;
il void add(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;}
ll up[N],down[N],w[N];
void dfs1(int u,int ff){
  sz[u]=1;
  for(RG int i=head[u];i;i=nxt[i]){
    RG int v=to[i];if(v==ff)continue;
    dfs1(v,u);sz[u]+=sz[v];
    up[u]+=up[v]+sz[v];
  }
}
void dfs2(int u,int ff){
  for(RG int i=head[u];i;i=nxt[i]){
    RG int v=to[i];if(v==ff)continue;
    down[v]=down[u]+up[u]-(up[v]+sz[v])+(n-sz[v]);
    dfs2(v,u);
  }
}
 
int main()
{
  n=read();
  for(RG int i=1;i<=n;i++){t[i].d=read();t[i].id=i;}
  sort(t+1,t+n+1,cmp_d);
  for(RG int i=1;i<=n;i++)o[i]=t[i].d,sz[i]=1;
  for(RG int i=n,p;i!=1;i--){
    p=lower_bound(o+1,o+n+1,t[i].d-n+2ll*sz[i])-o;
    if(o[p]!=t[i].d-n+2ll*sz[i]||p>=i)er();
    E[++m]=(edge){t[p].id,t[i].id};sz[p]+=sz[i];
  }
  for(RG int i=1;i<=m;i++)add(E[i].u,E[i].v),add(E[i].v,E[i].u);
  dfs1(1,0);dfs2(1,0);sort(t+1,t+n+1,cmp_id);
  for(RG int i=1;i<=n;i++)w[i]=down[i]+up[i];
  for(RG int i=1;i<=n;i++)if(w[i]!=t[i].d)er();
  for(RG int i=1;i<=m;i++)printf("%d %d\n",E[i].u,E[i].v);
  return 0;
}

AtCoder Regular Contest 103 題解