zoukankan      html  css  js  c++  java
  • NOIp2018集训test-9-21(am/pm)

    Am DAY1

    抄代码

    送分题

     1 //Achen
     2 #include<bits/stdc++.h>
     3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     5 #define Formylove return 0
     6 const int N=1007;
     7 using namespace std;
     8 typedef long long LL;
     9 typedef double db;
    10 char a[N],b[N];
    11 int T,n,m,to[500];
    12 
    13 template<typename T>void read(T &x) {
    14     T f=1; x=0; char ch=getchar();
    15     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    16     if(ch=='-') f=-1,ch=getchar();
    17     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    18 }
    19 
    20 void get(char a[],int &len) {
    21     len=0; char ch=getchar();
    22     if(ch!=';'&&(ch<'0'||ch>'9')&&(ch<'a'||ch>'z')&&ch!=' ') ch=getchar();
    23     for(;ch!='
    '&&ch!='	';ch=getchar()) a[len++]=ch;
    24 }
    25 
    26 #define ANS
    27 int main() {
    28 #ifdef ANS
    29     freopen("copycat.in","r",stdin);
    30     freopen("copycat.out","w",stdout);
    31 #endif
    32     read(T);
    33     while(T--) {
    34         memset(to,0,sizeof(to));
    35         get(a,n);
    36         get(b,m);
    37         if(n!=m) puts("0");
    38         else {
    39             int ans=1;
    40             For(i,0,n-1) {
    41                 if(a[i]>='a'&&a[i]<='z'&&b[i]>='a'&&b[i]<='z') {
    42                     if(!to[a[i]]) to[a[i]]=b[i];
    43                     else if(to[a[i]]!=b[i]) { ans=0; break; }
    44                 }
    45                 else if(a[i]!=b[i]) { ans=0; break; }
    46             }
    47             printf("%d
    ",ans);
    48         }
    49     }
    50     //cerr<<clock()<<endl;
    51     Formylove;
    52 }
    View Code

    做运动

    本来也是送分题但是我被自己蠢哭了。。

    二分明显随便做,但是显然这题卡二分,那么按边权从小到大枚举连边,并查集维护连通性,s和t联通后跑一遍dijkstra就出答案了,然而我蠢到用spfa去判断联通,我到底脑子里到底在想什么???

    事实是除了费用流和判负环spfa真的没有任何其他用途了,不要写spfa不要写spfa觉得要写spfa你一定是傻了!

     1 //Achen
     2 #include<bits/stdc++.h>
     3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     5 #define Formylove return 0
     6 const int N=1e6+7;
     7 using namespace std;
     8 typedef long long LL;
     9 typedef double db;
    10 int n,m,S,T;
    11 LL anst,ansc;
    12 
    13 template<typename T>void read(T &x) {
    14     T f=1; x=0; char ch=getchar();
    15     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    16     if(ch=='-') f=-1,ch=getchar();
    17     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    18 }
    19 
    20 int fa[N];
    21 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
    22 
    23 int ecnt,fir[N],nxt[N<<1],to[N<<1];
    24 LL val[N<<1],d[N],inf;
    25 void add(int u,int v,LL w) {
    26     nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
    27     nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
    28 }
    29 
    30 struct edge {
    31     int u,v,c;
    32     edge(int u,int v,int c):u(u),v(v),c(c){}
    33 };
    34 vector<edge>vc[10007];
    35 
    36 int vis[N];
    37 queue<int>que;
    38 void spfa() {
    39     d[S]=0;
    40     vis[S]=1;
    41     que.push(S);
    42     while(!que.empty()) {
    43         int x=que.front();
    44         que.pop(); vis[x]=0;
    45         for(int i=fir[x];i;i=nxt[i]) if(d[to[i]]>d[x]+val[i]) {
    46             d[to[i]]=d[x]+val[i];
    47             if(!vis[to[i]]) {
    48                 vis[to[i]]=1;
    49                 que.push(to[i]);
    50             }
    51         }
    52     }
    53 }
    54 
    55 #define ANS
    56 int main() {
    57 #ifdef ANS
    58     freopen("running.in","r",stdin);
    59     freopen("running.out","w",stdout);
    60 #endif
    61     read(n); read(m);
    62     For(i,1,m) {
    63         int u,v,c,t;
    64         read(u); read(v); read(t); read(c);
    65         vc[t].push_back(edge(u,v,c));
    66     }
    67     read(S); read(T);
    68     memset(d,127/3,sizeof(d));
    69     inf=d[0]; d[S]=0;
    70     For(i,1,n) fa[i]=i;
    71     For(t,0,10000) {
    72         int up=vc[t].size();
    73         For(i,0,up-1) {
    74             edge tp=vc[t][i];
    75             LL w=(LL)tp.c*t;
    76             add(tp.u,tp.v,w);
    77             int x=find(tp.u),y=find(tp.v);
    78             if(x!=y) fa[x]=y;
    79         }
    80         if(find(S)==find(T)) {
    81             spfa();
    82             anst=t; ansc=d[T];
    83             break;
    84         }
    85     }
    86     printf("%lld %lld
    ",anst,ansc);
    87     //cerr<<clock()<<endl;
    88     Formylove;
    89 }
    View Code

    大逃杀

    树dp随便做。注意一下更新顺序就好。

     1 //Achen
     2 #include<bits/stdc++.h>
     3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     5 #define Formylove return 0
     6 const int N=307;
     7 using namespace std;
     8 typedef long long LL;
     9 typedef double db;
    10 int n,T,w[N],t[N],ans,inf;
    11 
    12 template<typename T>void read(T &x) {
    13     T f=1; x=0; char ch=getchar();
    14     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    15     if(ch=='-') f=-1,ch=getchar();
    16     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    17 }
    18 
    19 int ecnt,fir[N],nxt[N<<1],to[N<<1],val[N<<1];
    20 void add(int u,int v,int w) {
    21     nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
    22     nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
    23 }
    24 
    25 void get_max(int &x,int y,int z) {
    26     if(y==inf||z==inf) return;
    27     if(x<y+z) x=y+z; ans=max(ans,x); 
    28 }
    29 
    30 int f[N][N][3];
    31 void dfs(int x,int fa) {
    32     f[x][t[x]][0]=w[x];
    33     f[x][t[x]][1]=w[x];
    34     f[x][t[x]][2]=w[x];
    35     for(int i=fir[x];i;i=nxt[i]) if(to[i]!=fa) {
    36         dfs(to[i],x);
    37         Rep(j,T,val[i]) {
    38             int f0=f[x][j][0],f1=f[x][j][1],f2=f[x][j][2];
    39             For(k,0,j-val[i]) {
    40                 if(j-k-2*val[i]>=0) {
    41                     get_max(f0,f[x][j-k-2*val[i]][0],f[to[i]][k][0]);
    42                     get_max(f1,f[x][j-k-2*val[i]][1],f[to[i]][k][0]);
    43                     get_max(f2,f[x][j-k-2*val[i]][2],f[to[i]][k][0]);
    44                     
    45                     get_max(f2,f[x][j-k-2*val[i]][0],f[to[i]][k][2]);
    46                 }
    47                 get_max(f1,f[x][j-k-val[i]][0],f[to[i]][k][1]);
    48                 get_max(f2,f[x][j-k-val[i]][1],f[to[i]][k][1]);
    49             }
    50             f[x][j][0]=f0,f[x][j][1]=f1,f[x][j][2]=f2;
    51         }
    52     }
    53 }
    54 
    55 #define ANS
    56 int main() {
    57 #ifdef ANS
    58     freopen("toyuq.in","r",stdin);
    59     freopen("toyuq.out","w",stdout);
    60 #endif
    61     read(n); read(T);
    62     For(i,1,n) read(w[i]);
    63     For(i,1,n) { read(t[i]); t[i]=min(t[i],T+1); }
    64     For(i,2,n) {
    65         int u,v,w;
    66         read(u); read(v); read(w);
    67         add(u,v,w);
    68     }
    69     memset(f,128,sizeof(f));
    70     inf=f[0][0][0];
    71     dfs(1,0);
    72     printf("%d
    ",ans);
    73     //cerr<<clock()<<endl;
    74     Formylove;
    75 }
    View Code

    pm DAY2

    非法输入

    细心一点模拟即可。考虑清楚所有情况,注意如-0等。

     1 //Achen
     2 #include<bits/stdc++.h>
     3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     5 #define Formylove return 0
     6 const int N=1e6+7;
     7 using namespace std;
     8 typedef long long LL;
     9 typedef double db;
    10 int T;
    11 LL bs;
    12 char s[N];
    13 
    14 template<typename T>void read(T &x) {
    15     T f=1; x=0; char ch=getchar();
    16     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    17     if(ch=='-') f=-1,ch=getchar();
    18     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    19 }
    20 
    21 void solve() {
    22     int cnt=0;
    23     LL a=0,b=0,fl1=1,fl2=1;
    24     char ch=getchar();
    25     while(ch!='
    '&&ch!='
    ') {
    26         if(!cnt) {
    27             if(ch=='-'||(ch>='0'&&ch<='9')) {
    28                 cnt++; 
    29                 if(ch=='-') fl1=-1;
    30                 else a=ch-'0';
    31                 for(int i=1;i;i++) {
    32                     ch=getchar();
    33                     if(ch==' '||ch=='
    '||ch=='
    ') break;    
    34                     if(ch<'0'||ch>'9') cnt=3;
    35                     else {
    36                         if((fl1==1&&i==1&&a==0)||(fl1==-1&&i==2&&a==0)) cnt=3;
    37                         a=a*10+ch-'0';
    38                         if((fl1==1&&a>=bs)||(fl1==-1&&a>bs)) cnt=3;
    39                     }
    40                 }
    41                 if(a==0&&fl1==-1) cnt=3;
    42                 if(fl1==-1) a=-a;
    43             }
    44             else cnt=3;
    45         }
    46         else if(cnt==1) {
    47             if(ch=='-'||(ch>='0'&&ch<='9')) {
    48                 cnt++; 
    49                 if(ch=='-') fl2=-1;
    50                 else b=ch-'0';
    51                 for(int i=1;i;i++) {
    52                     ch=getchar();
    53                     if(ch=='
    '||ch=='
    ') break;    
    54                     if(ch<'0'||ch>'9') cnt=3;
    55                     else {
    56                         if((fl2==1&&i==1&&b==0)||(fl2==-1&&i==2&&b==0)) cnt=3;
    57                         b=b*10+ch-'0';
    58                         if((fl2==1&&b>=bs)||(fl2==-1&&b>bs)) cnt=3;
    59                     }
    60                 }
    61                 if(b==0&&fl2==-1) cnt=3;
    62                 if(fl2==-1) b=-b;
    63             }
    64             else cnt=3;
    65         }
    66         if(ch=='
    '||ch=='
    ') break;    
    67         ch=getchar();
    68     }
    69     if(cnt!=2) puts("Input Error");
    70     else printf("%lld
    ",a+b);
    71 }
    72 
    73 #define ANS
    74 int main() {
    75 #ifdef ANS
    76     freopen("aplusb.in","r",stdin);
    77     freopen("aplusb.out","w",stdout);
    78 #endif
    79     read(T);
    80     bs=(1LL<<31);
    81     while(T--) {
    82         solve();
    83     }
    84     //cerr<<clock()<<endl;
    85     Formylove;
    86 }
    View Code

    手机信号

    两个标记有些地方没覆盖好,炸成10分。不给大样例的数据机构和大模拟都是耍流氓好吗。

    set可以做,但是我写得有点复杂,半天没搞出来,最后写了线段树。

    区间维护四个东西,区间最左最右的信号站位置,一个区间建信号站的标记和一个区间清空的标记。

    查pos的时候查1~pos中最靠右的和pos~n中最靠左的即可。

      1 //Achen
      2 #include<bits/stdc++.h>
      3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
      4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
      5 #define Formylove return 0
      6 const int N=2e5+7;
      7 using namespace std;
      8 typedef long long LL;
      9 typedef double db;
     10 int m,up=1e9+1;
     11 LL c,ans;
     12 char o[20];
     13 
     14 template<typename T>void read(T &x) {
     15     T f=1; x=0; char ch=getchar();
     16     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
     17     if(ch=='-') f=-1,ch=getchar();
     18     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
     19 }
     20 
     21 int rt,tot,ch[N*100][2],ll[N*100],rr[N*100],lz[N*100];
     22 #define lc ch[x][0]
     23 #define rc ch[x][1]
     24 #define mid ((l+r)>>1)
     25 int newnode() { ++tot; ll[tot]=up+1; rr[tot]=0; return tot;}
     26 
     27 void down(int x,int l,int r) {
     28     if(!lz[x]) return;
     29     if(lz[x]==-1) {
     30         if(lc) { ll[lc]=up+1; rr[lc]=0; lz[lc]=-1; }
     31         if(rc) { ll[rc]=up+1; rr[rc]=0; lz[rc]=-1; }
     32         lz[x]=0;
     33     }
     34     else {
     35         if(!lc) lc=newnode(); if(!rc) rc=newnode();
     36         int v=lz[x];
     37         if(ll[x]<=mid) {
     38             ll[lc]=rr[lc]=ll[x]; lz[lc]=lz[x];
     39             if(ll[lc]+lz[x]<=mid) rr[lc]=ll[lc]+(mid-ll[lc])/v*v;
     40         }
     41         else { ll[lc]=up+1; rr[lc]=0; lz[lc]=-1; }
     42         if(rr[x]>mid) {
     43             rr[rc]=ll[rc]=rr[x]; lz[rc]=lz[x];
     44             if(rr[rc]-v>mid) ll[rc]=rr[rc]-(rr[rc]-(mid+1))/v*v;
     45         }
     46         else { ll[rc]=up+1; rr[rc]=0; lz[rc]=-1; }
     47         lz[x]=0;
     48     }
     49 }
     50 
     51 void upd(int x) {
     52     ll[x]=min(ll[lc],ll[rc]);
     53     rr[x]=max(rr[lc],rr[rc]);
     54 }
     55 
     56 void clear(int x,int l,int r,int ql,int qr) {
     57     if(!x) return;
     58     if(l>=ql&&r<=qr) {
     59         ll[x]=up+1; rr[x]=0; 
     60         lz[x]=-1; return;
     61     }
     62     down(x,l,r);
     63     if(ql<=mid) clear(lc,l,mid,ql,qr);
     64     if(qr>mid) clear(rc,mid+1,r,ql,qr);
     65     upd(x);
     66 }
     67 
     68 void update(int &x,int l,int r,int ql,int qr,int v) {
     69     if(!x) x=newnode();
     70     if(l>=ql&&r<=qr) {
     71         int lv=(l-ql)%v==0?l:ql+((l-ql)/v+1)*v;
     72         int rv=max(lv,(r-ql)%v==0?r:ql+(r-ql)/v*v);
     73         if(lv<=r&&rv>=l) {
     74             ll[x]=lv; rr[x]=rv; lz[x]=v;
     75         }
     76         return;
     77     }
     78     down(x,l,r);
     79     if(ql<=mid) update(lc,l,mid,ql,qr,v);
     80     if(qr>mid) update(rc,mid+1,r,ql,qr,v);
     81     upd(x);
     82 }
     83 
     84 int qry(int x,int l,int r,int ql,int qr,int f) {
     85     if(!x) {
     86         if(f==1) return 0;
     87         if(f==0) return up+1;
     88     }
     89     if(l>=ql&&r<=qr) {
     90         return f==1?rr[x]:ll[x];
     91     }
     92     down(x,l,r);
     93     if(qr<=mid) return qry(lc,l,mid,ql,qr,f);
     94     if(ql>mid) return qry(rc,mid+1,r,ql,qr,f);
     95     return f==1?(max(qry(lc,l,mid,ql,qr,f),qry(rc,mid+1,r,ql,qr,f))):
     96                 (min(qry(lc,l,mid,ql,qr,f),qry(rc,mid+1,r,ql,qr,f)));
     97 }
     98 
     99 #define ANS
    100 int main() {
    101 #ifdef ANS
    102     freopen("cellphone.in","r",stdin);
    103     freopen("cellphone.out","w",stdout);
    104 #endif
    105     int tpp=0;
    106     read(m); read(c);
    107     ll[0]=up+1; rr[0]=0;
    108     For(cs,1,m) {
    109         if(cs==93) {
    110             int debug=1;
    111         }
    112         scanf("%s",o);
    113         int l,r,v,x;
    114         if(o[0]=='c') {
    115             read(l); read(r); read(v);
    116             l++; r++;
    117             update(rt,1,up,l,r,v);
    118         }
    119         else if(o[0]=='d') {
    120             read(l); read(r);
    121             l++; r++;
    122             clear(rt,1,up,l,r);
    123         }
    124         else if(o[0]=='q') {
    125             read(x); x++;
    126             ans=-1;
    127             int p1=qry(rt,1,up,1,x,1);
    128             int p2=qry(rt,1,up,x,up,0);
    129             if(p1!=0) ans=x-p1;
    130             if(p2!=up+1) {
    131                 if(ans==-1||ans>p2-x) 
    132                     ans=p2-x;
    133             }
    134             if(ans==-1) puts("0");
    135             else {
    136                 ans=max(0LL,c-ans*ans);
    137                 printf("%lld
    ",ans);
    138             }
    139         }
    140     }
    141     //cerr<<clock()<<endl;
    142     Formylove;
    143 }
    View Code

    量子纠缠

    总觉得这道题有些地方我没有搞清楚吧。。。但是又说不出来到底哪里有什么问题。

    不纠缠的时候字典树就可以解决,要纠缠就要合并字典树上的节点,考虑用并查集来合并节点,合并的时候并查集里父亲改了,递归下去合并儿子。

    代码的话就注意一下有标注的地方吧。

     1 //Achen
     2 #include<bits/stdc++.h>
     3 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     4 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     5 #define Formylove return 0
     6 const int N=8e6+7;
     7 using namespace std;
     8 typedef long long LL;
     9 typedef double db;
    10 int m;
    11 char a[N];
    12 
    13 template<typename T>void read(T &x) {
    14     T f=1; x=0; char ch=getchar();
    15     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    16     if(ch=='-') f=-1,ch=getchar();
    17     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    18 }
    19 
    20 int fa[N];
    21 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); }
    22 
    23 int rt,tot,ch[N][10],is[N];
    24 int &ins(int w) { 
    25     int x=rt,len=strlen(a);
    26     For(i,0,len-2) {
    27         int c=a[i]-'0';
    28         if(!ch[x][c]) {
    29             ch[x][c]=++tot;
    30             fa[tot]=tot; ///////////////////!!!!!!!!!!!!!!!!!!!!
    31         }
    32         x=find(ch[x][c]); ///////////////////!!!!!!!!!!!!!!!!!!!!
    33     }
    34     int c=a[len-1]-'0';
    35     if(!ch[x][c]) { ch[x][c]=++tot; fa[tot]=tot; ///////////////////!!!!!!!!!!!!!!!!!!!!}
    36     if(w) is[find(ch[x][c])]=w; ///////////////////!!!!!!!!!!!!!!!!!!!!
    37     return ch[x][c];
    38 }
    39 
    40 void qry() {
    41     int x=rt,len=strlen(a);
    42     For(i,0,len-1) {
    43         int c=a[i]-'0';
    44         x=find(ch[x][c]);
    45         if(!x) break;
    46     }
    47     printf("%d
    ",is[x]);
    48 }
    49 
    50 void merge(int &x,int &y) {
    51     if(!x&&!y) return;
    52     if(!x) x=y;
    53     else if(!y) y=x;
    54     else {
    55         int fx=find(x),fy=find(y);
    56         if(fx==fy) return ;
    57         if(is[fy]) is[fx]=1;
    58         fa[fy]=fx;
    59         For(i,0,9) {
    60             merge(ch[fx][i],ch[fy][i]);
    61             fx=find(fx); ///////////////////!!!!!!!!!!!!!!!!!!!!
    62         }
    63     }
    64 }
    65 
    66 #define ANS
    67 int main() {
    68 #ifdef ANS
    69     freopen("quantum.in","r",stdin);
    70     freopen("quantum.out","w",stdout);
    71 #endif
    72     read(m);
    73     For(cs,1,m) {
    74         int o;
    75         read(o); scanf("%s",a);
    76         if(o==1) ins(1);
    77         else if(o==2) qry();
    78         else {
    79             int &x=ins(0);
    80             scanf("%s",a);
    81             int &y=ins(0);
    82             merge(x,y);
    83         }
    84     }
    85     //cerr<<clock()<<endl;
    86     Formylove;
    87 }
    View Code
  • 相关阅读:
    memory consistency
    网页基础
    ECC
    RSA
    argparse模块
    009-MySQL循环while、repeat、loop使用
    001-mac搭建Python开发环境、Anaconda、zsh兼容
    013-在 Shell 脚本中调用另一个 Shell 脚本的三种方式
    012-Shell 提示确认(Y / N,YES / NO)
    014-docker-终端获取 docker 容器(container)的 ip 地址
  • 原文地址:https://www.cnblogs.com/Achenchen/p/9745305.html
Copyright © 2011-2022 走看看