zoukankan      html  css  js  c++  java
  • 2019 浙大校赛解题过程及题解

    解题过程


     开场lfw看J,被孪生素数误导,没有看出水题的本质,byf及时提醒后,lfw忘记N=1的情况要特判。之后byf写E,忘记开long long,wa+1。Shl看出A题思路,告诉lfw开始写A,lfw忘记先排序,WA+1。byf看出B题做法,lfw写java过掉,shl与lfw讨论G,lfw开始写G,byf看出C可以hash暴力,lfwG忘记清空一个值,导致特殊情况会加两次,wa+1,byf使用mp作hash,带logn常数TLE,随后转成long long的hash值,使用unordered_map过掉。最后lfw和shl讨论出H,lfw开始写H,H没过样例,shl和byf讨论出D题随机,开始上机写D,其中我们都不知道,swap(a[i],a[len-1]).a[i].pop_back()可以删掉vector中的一个元素,百度耗时。最后D题没过,H题没时间调,可惜。
    Lfw和byf多次犯小错误,造成罚时很高,而且最后D题写慢,随机没能多交几次,H题没时间调,可惜。

    题解


    由于刚打完,先附上代码,后面会更新题解。

    Thanks, TuSimple!

    参考代码:

     1 #include<bits/stdc++.h>
     2 #define maxl 100010
     3 using namespace std;
     4 
     5 int n,m,ans;
     6 struct node
     7 {
     8     int h,p;
     9 }a[maxl];
    10 int b[maxl];
    11 int q[maxl];
    12 set <int> s1,s2;
    13 set <int> :: iterator it;
    14 
    15 inline void prework()
    16 {
    17     s1.clear();s2.clear();
    18     scanf("%d%d",&n,&m);
    19     for(int i=1;i<=n;i++)
    20         scanf("%d",&a[i].h);
    21     for(int i=1;i<=m;i++)
    22         scanf("%d",&b[i]);
    23         
    24     for(int i=1;i<=n;i++)
    25         scanf("%d",&a[i].p);
    26     for(int i=1;i<=m;i++)
    27     {
    28         scanf("%d",&q[i]);
    29         if(q[i]==0)
    30             s1.insert(b[i]);
    31         else
    32             s2.insert(b[i]);
    33     }
    34 }
    35 
    36 inline bool cmp(const node &x,const node &y)
    37 {
    38     return x.h<y.h;
    39 }
    40 
    41 inline void mainwork()
    42 {
    43     sort(a+1,a+1+n,cmp);
    44     ans=0;
    45     for(int i=1;i<=n;i++)
    46     if(a[i].p==0)
    47     {
    48         it=s2.begin();
    49         if(it!=s2.end())
    50             if((*it)<a[i].h)
    51             {
    52                 ans++;
    53                 s2.erase(it);
    54             }
    55     }
    56     else
    57     {
    58         it=s1.upper_bound(a[i].h);
    59         if(it!=s1.end())
    60             if((*it)>a[i].h)
    61             {
    62                 ans++;
    63                 s1.erase(it);
    64             }
    65     }
    66 }
    67 
    68 inline void print()
    69 {
    70     printf("%d
    ",ans);
    71 }
    72 
    73 int main()
    74 {
    75     int t;
    76     scanf("%d",&t);
    77     for(int i=1;i<=t;i++)
    78     {
    79         prework();
    80         mainwork();
    81         print();
    82     }
    83     return 0;
    84 }
    View Code

    B.Even Number Theory

    参考代码:

     1 import java.util.Scanner;
     2 import java.math.*;
     3 import java.text.*;
     4 
     5 public class Main {
     6     public static void main(String args[])
     7     {
     8         Scanner input=new Scanner(System.in);
     9         int t;
    10         BigInteger e,ans,zero=BigInteger.valueOf(0);
    11         BigInteger two=BigInteger.valueOf(2);
    12         t=input.nextInt();
    13         for(int i=1;i<=t;i++)
    14         {
    15             e=input.nextBigInteger();
    16             e=e.divide(two);ans=BigInteger.valueOf(0);
    17             while(!(e.equals(zero)))
    18             {
    19                 ans=ans.add(e);
    20                 e=e.divide(two);
    21 
    22             }
    23             System.out.println(ans);
    24         }
    25     }
    26 }
    View Code

    C. Robot Cleaner I

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef pair<int,int> pii;
     4 typedef long long LL;
     5 unordered_map<LL,bool> vis;
     6 char s[255];
     7 char lin[2005];
     8 int mp[2005][2005];
     9 int t4=3*3*3*3,t3=3*3*3,t2=3*3,t1=3;
    10 int calc(int i,int j)
    11 {
    12     return 1+t4*mp[i][j]+t3*mp[i-1][j]+t2*mp[i+1][j]+t1*mp[i][j-1]+mp[i][j+1];
    13 }
    14 bool check(int x,int y)
    15 {
    16     if(mp[x][y]!=1) return true;
    17     return false;
    18 }
    19 int zo2=2000*2000,zo1=2000;
    20 LL Hash(int x,int y,int cnt)
    21 {
    22     return 1LL*zo2*(x-1)+1LL*zo1*(y-1)+cnt-1;
    23 }
    24 int main()
    25 {
    26     int t;
    27     scanf("%d",&t);
    28     while(t--)
    29     {
    30         vis.clear();
    31         int n,m;
    32         scanf("%d%d",&n,&m);
    33         int a,b;
    34         LL k;
    35         scanf("%d%d%lld",&a,&b,&k);
    36         scanf("%s",s+1);
    37         int cnt=0;
    38         for(int i=1;i<=n;i++)
    39         {
    40             scanf("%s",lin+1);
    41             for(int j=1;j<=m;j++)
    42             {
    43                 mp[i][j]=lin[j]-'0';
    44                 if(mp[i][j]==2) cnt++;
    45             }
    46         }
    47         int ans=0;
    48         pii loc=pii(a,b);
    49         int mov;
    50         char op;
    51         int x,y;
    52         LL tim=0;
    53         while(++tim<=k)
    54         {
    55             mov=calc(loc.first,loc.second);
    56             op=s[mov];
    57             if(vis.count(Hash(loc.first,loc.second,cnt))) break;
    58             vis[Hash(loc.first,loc.second,cnt)]=1;
    59             x=loc.first,y=loc.second;
    60             if(op=='U') if(check(x-1,y)) loc=pii(x-1,y);
    61             if(op=='D') if(check(x+1,y)) loc=pii(x+1,y);
    62             if(op=='L') if(check(x,y-1)) loc=pii(x,y-1);
    63             if(op=='R') if(check(x,y+1)) loc=pii(x,y+1);
    64             if(op=='P') if(mp[x][y]==2)mp[x][y]=0,ans++,cnt--;
    65         }
    66         printf("%d
    ",ans);
    67     }
    68 }
    View Code

    D.Robot Cleaner II

    Unsolved.

    E.Potion

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 LL a[105],b[105];
     5 int main()
     6 {
     7     int t;
     8     scanf("%d",&t);
     9     while(t--)
    10     {
    11         int n;
    12         scanf("%d",&n);
    13         for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    14         for(int i=1;i<=n;i++) scanf("%lld",&b[i]);
    15         LL las=0;
    16         bool flag=1;
    17         for(int i=n;i>=1;i--)
    18         {
    19             if(las+b[i]>=a[i])
    20             {
    21                 las=las+b[i]-a[i];
    22             }
    23             else 
    24             {
    25                 flag=0;
    26                 break;
    27             }
    28         }
    29         if(flag)
    30         {
    31             puts("Yes");
    32         }
    33         else puts("No");
    34     }
    35 }
    View Code

    F.Strange Function

    Unsolved.

    G.Postman

    参考代码:

     1 #include<bits/stdc++.h>
     2 #define maxl 100010
     3 
     4 using namespace std;
     5 
     6 int n,k,suml,sumr;
     7 int lcnt[maxl],rcnt[maxl];
     8 long long ans;
     9 long long a[maxl],sum[maxl];
    10 long long lnum[maxl],rnum[maxl];
    11 
    12 inline void prework()
    13 {
    14     scanf("%d%d",&n,&k);
    15     for(int i=1;i<=n;i++)
    16         scanf("%lld",&a[i]);
    17     sort(a+1,a+1+n);
    18 }
    19 
    20 inline void mainwork()
    21 {
    22     int lind=0,rind=n+1;
    23     for(int i=1;i<=n;i++)
    24     if(a[i]<0)
    25         lind=i;
    26     suml=0;sumr=0;
    27     for(int i=1;i<=lind;i++)
    28     if(a[i]!=lnum[suml])
    29         lnum[++suml]=a[i],lcnt[suml]=1;
    30     else
    31         lcnt[suml]++;
    32     for(int i=1;i<=n;i++)
    33     if(a[i]>0)
    34     {
    35         rind=i;
    36         break;
    37     }
    38     for(int i=rind;i<=n;i++)
    39     if(a[i]!=rnum[sumr])
    40         rnum[++sumr]=a[i],rcnt[sumr]=1;
    41     else
    42         rcnt[sumr]++;
    43     int tmp=0;
    44     long long sum=0,last;
    45     for(int i=1;i<=n;i++)
    46     {
    47         if(a[i]>=0)
    48             break;
    49         if(tmp==0)
    50             last=-a[i];
    51         tmp++;
    52         if(tmp==k)
    53             sum+=2ll*last,tmp=0,last=0;
    54     }
    55     sum+=2ll*last;last=0;
    56     tmp=0;
    57     for(int i=n;i>=1;i--)
    58     {
    59         if(a[i]<=0)
    60             break;
    61         if(tmp==0) last=a[i];
    62         tmp++;
    63         if(tmp==k)
    64             sum+=2ll*last,tmp=0,last=0;
    65     }
    66     sum+=2ll*last;last=0;
    67     if(a[1]>=0 && a[n]>0)
    68         ans=sum-a[n];
    69     else
    70     if(a[n]<=0 && a[1]<0)
    71         ans=sum-(-a[1]);
    72     else
    73     if(a[1]<0 && a[n]>0)
    74         ans=min(sum-a[n],sum-(-a[1]));
    75     else
    76         ans=0;
    77 }
    78 
    79 inline void print()
    80 {
    81     printf("%lld
    ",ans);
    82 }
    83 
    84 int main()
    85 {
    86     int t;
    87     scanf("%d",&t);
    88     for(int i=1;i<=t;i++)
    89     {
    90         prework();
    91         mainwork();
    92         print();
    93     }
    94     return 0;
    95 }
    View Code

    H.Rescue the Princess

    参考代码:

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 const int N=2e5+10;
      4 struct Edge{
      5     int go,next,x;
      6 }e[2*N],e2[2*N];
      7 int head[N],tot,h[N],t2;
      8 void add(int x,int y){
      9     e[++tot].go=y;
     10     e[tot].x=x;
     11     e[tot].next=head[x];
     12     head[x]=tot;
     13 }
     14 void a2(int x,int y){
     15     e2[++t2].go=y;
     16     e2[t2].next=h[x];
     17     h[x]=t2;
     18 }
     19 int sta[N],be[N],top,dfn[N],low[N],ind,vis[N],cnt;
     20 void tarjan(int x,int f){
     21     dfn[x]=low[x]=++ind;
     22     sta[++top]=x;
     23     bool fir=1;
     24     for(int i=head[x];i;i=e[i].next){
     25         int v=e[i].go;
     26         if(v==f&&fir){
     27             fir=0;
     28             continue;
     29         }
     30         if(!dfn[v]){
     31             tarjan(v,x);
     32             low[x]=min(low[x],low[v]);
     33         }
     34         else if(!vis[v])low[x]=min(low[x],low[v]);
     35     }
     36     if(low[x]==dfn[x]){
     37         be[x]=++cnt;vis[x]=1;
     38         while(sta[top]!=x){
     39             int t=sta[top--];
     40             be[t]=cnt;
     41             vis[t]=1;
     42         }
     43         top--;
     44     }
     45 }
     46 int n,m,q;
     47 int ind2,df[2*N],p[2*N],st[2*N][20],dep[N];
     48 void dfs(int x,int f){
     49     df[x]=++ind2;
     50     dep[x]=dep[f]+1;
     51     p[ind2]=x;
     52     for(int i=h[x];i;i=e2[i].next){
     53         int v=e2[i].go;
     54         if(v==f)continue;
     55         dfs(v,x);
     56         p[++ind2]=x;
     57     }    
     58 }
     59 void ini(){
     60     for(int i=1;i<=ind2;i++)st[i][0]=df[p[i]];
     61     for(int i=1;i<=19;i++){
     62         for(int j=1;j+(1<<i)-1<=ind2;j++){
     63             st[j][i]=min(st[j][i-1],st[j+(1<<(i-1))][i-1]);
     64         }
     65     }
     66 }
     67 int lca(int x,int y){
     68     x=df[x],y=df[y];
     69     if(x>y)swap(x,y);
     70     int k=log2(y-x+1);
     71     int t=min(st[x][k],st[y-(1<<k)+1][k]);
     72     return p[t];
     73 }
     74 int a[N];
     75 int fin(int &x){
     76     if(x!=a[x])return x=fin(a[x]);
     77     return x;
     78 }
     79 void merg(int x,int y){
     80     fin(x);fin(y);
     81     a[x]=y;
     82 }
     83 bool ck(int x,int y,int z){
     84     fin(x);fin(y);fin(z);
     85     return x==y&&y==z;
     86 }
     87 inline void init(){
     88     tot=t2=ind=cnt=top=ind2=0;
     89     for(int i=1;i<=n;i++){
     90         head[i]=h[i]=0;
     91         vis[i]=be[i]=0;
     92         dfn[i]=low[i]=0;
     93         dep[i]=0;
     94         a[i]=i;
     95     }
     96 }
     97 int inline dis(int x,int y){
     98     int k=lca(x,y);
     99     return dep[x]+dep[y]-2*dep[k];
    100 }
    101 int main(){
    102     int T;
    103     scanf("%d",&T);
    104     while(T--){
    105         scanf("%d%d%d",&n,&m,&q);
    106         init();
    107         int ch=0;
    108         for(int i=1;i<=m;i++){
    109             int x,y;
    110             scanf("%d%d",&x,&y);
    111             if(x==y){
    112                 ch++;
    113                 continue;
    114             }
    115             add(x,y);add(y,x);
    116             merg(x,y);
    117         }
    118         for(int i=1;i<=n;i++){
    119             if(!dfn[i])tarjan(i,0);
    120         }
    121         for(int i=1;i<=2*m-2*ch;i++){
    122             int x=e[i].x,y=e[i].go;
    123             if(be[x]!=be[y]){
    124                 a2(be[x],be[y]);
    125             }
    126         }
    127         for(int i=1;i<=cnt;i++){
    128             if(!dep[i])dfs(i,0);
    129         }
    130         ini();
    131         for(int i=1;i<=q;i++){
    132             int u,v,w;
    133             scanf("%d%d%d",&u,&v,&w);
    134             if(!ck(u,v,w))puts("No");
    135             else {
    136                 u=be[u];v=be[v];w=be[w];
    137                 if(dis(u,v)+dis(u,w)==dis(v,w))puts("Yes");
    138                 else puts("No");
    139             }
    140         }
    141     }
    142     return 0;
    143 }
    View Code

    I.Defense Plan

    Unsolved.

    Extended Twin Composite Number

    参考代码:

     1 #include<bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int t;long long n;
     8     scanf("%d",&t);
     9     for(int i=1;i<=t;i++)
    10     {
    11         scanf("%lld",&n);
    12         if(n==1)
    13             puts("8 9");
    14         else
    15             printf("%lld %lld
    ",2*n,3*n);
    16     }
    17     return 0;
    18 }
    View Code

     

  • 相关阅读:
    bzoj5328: [Sdoi2018]物理实验
    HDU
    bzoj4820: [Sdoi2017]硬币游戏
    bzoj4600: [Sdoi2016]硬币游戏
    阿里云配置防火墙规则
    博客园 添加 Live 2D 模型
    R语言做逻辑回归
    R语言错误的提示(中英文翻译)
    用随机森林分类
    python 切换虚拟环境
  • 原文地址:https://www.cnblogs.com/csushl/p/10708269.html
Copyright © 2011-2022 走看看