AtCoder Grand Contest 012 solution to a problem

Portal

\(A\)

Certainly every two behind the front accompany an optimal

typedef long long ll;
const int N=5e5+5;
int a[N],n;ll res;
int main(){
    scanf("%d",&n);
    fp(i,1,n*3)scanf("%d",&a[i]);
    sort(a+1,a+1+n*3);
    for(R int i=n+1;i<=n*3;i+=2)res+=a[i];
    printf("%lld\n",res);
    return 0;
}

\(B\)

First staining is certainly behind the front cover, then we do back to front, and each point record \ (d_i \) How much can extend from this point, only when \ (d_i \) increases when it is considered from the start updating it, because all the \ (d \) is very small, so each point is updated no more than \ (d_i \) , with the total complexity is \ (O (d_in) \)

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=1e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
struct node{int u,d,c;}E[N];
int d[N],c[N],q[N],n,m,Q;
void solve(int S,int dis,int col){
    int h=1,t=0,u;
    q[++t]=S;
    while(h<=t){
        u=q[h++];if(!c[u])c[u]=col;
        go(u)if(cmax(d[v],d[u]-1))q[++t]=v;
    }
}
int main(){
    scanf("%d%d",&n,&m);
    fp(i,1,n)d[i]=-1;
    for(R int i=1,u,v;i<=m;++i)scanf("%d%d",&u,&v),add(u,v),add(v,u);
    scanf("%d",&Q);
    fp(i,1,Q)scanf("%d%d%d",&E[i].u,&E[i].d,&E[i].c);
    fd(i,Q,1)if(cmax(d[E[i].u],E[i].d))solve(E[i].u,E[i].d,E[i].c);
    fp(i,1,n)printf("%d\n",c[i]);
    return 0;
}

\(C\)

Let the final sequence forming long as \ (p_1, p_2, p_3, ..., p_n, 1,2,3, ..., n \) form, then find the number of legitimate sequence is \ (p \ ) incremented sequence number \ (- 1 \)

Then we \ (the n-++ \) , then multiply by a similar idea to maintain. Found empty set sequence number is incremented by \ (1 \) , followed by the \ (n + 1 \) promoter sequence number \ (\ Times 2 \) , preceded by \ (n + 1 \) sequence number \ (1 + \)

With a double-ended queue maintains about enough

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
deque<int>st;ll n,k;int pos;
int main(){
    scanf("%lld",&n);++n;
    while((1ll<<pos)<=n)++pos;
    fd(i,pos-2,0){
        st.push_back(++k);
        if(n>>i&1)st.push_front(++k);
    }
    printf("%d\n",k<<1);
    fp(i,0,k-1)printf("%d ",st[i]);
    fp(i,1,k)printf("%d ",i);
    return 0;
}

\(D\)

First, we found that if we can exchange \ (ab \) and \ (ac \) case, you can not change other positions in exchange \ (bc \) , so if even one we all can swap sides, then a communication block all points are interchangeable position, for block direct communication with the interior of the operator will be able to

Then consider even sides, for each point \ (U \) , the minimum weight set point which corresponds to the color \ (Mn \) , there are two cases \ (U \) energy and \ (Mn \) Unicom a side is directly connected, with their one is to find a different color \ (V \) is then connected edge \ ((u, v) \ ) and \ ((V, Mn) \) . So if \ (u \ neq mn \) and can be directly connected to the side and even side, or find a different color and with their minimum weight point \ (v \) attempts to connect to it side. Eventually all found too difficult to connect both sides of the point and the minimum point in the same communication block weight, then calculate the line

//quming
#include<bits/stdc++.h>
#define R register
#define fi first
#define se second
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=1e9+7;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
    R int res=1;
    for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
    return res;
}
const int N=2e5+5;
typedef pair<int,int> pi;
int fac[N],ifac[N],fa[N],mn[N],cnt[N];pi c[N];
int n,x,y,pos,sz,res;
inline int find(R int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main(){
    scanf("%d%d%d",&n,&x,&y);
    fp(i,1,n)scanf("%d%d",&c[i].se,&c[i].fi);
    sort(c+1,c+1+n);
    pos=2;
    while(pos<=n&&c[pos].se==c[1].se)++pos;
    if(pos>n)return puts("1"),0;
    fd(i,n,1)mn[c[i].se]=i,fa[i]=i;
    fd(i,n,1){
        if(i!=mn[c[i].se]&&c[i].fi+c[mn[c[i].se]].fi<=x)
            fa[find(i)]=find(mn[c[i].se]);
        else if(c[i].se!=c[1].se&&c[i].fi+c[1].fi<=y)
            fa[find(i)]=find(1);
        else if(c[i].se!=c[pos].se&&c[i].fi+c[pos].fi<=y)
            fa[find(i)]=find(pos);
    }
    fac[0]=ifac[0]=1;fp(i,1,n)fac[i]=mul(fac[i-1],i);
    ifac[n]=ksm(fac[n],P-2);fd(i,n-1,1)ifac[i]=mul(ifac[i+1],i+1);
    fp(i,1,n)if(find(i)==find(1))++sz,++cnt[c[i].se];
    res=fac[sz];
    fp(i,1,n)res=mul(res,ifac[cnt[i]]);
    printf("%d\n",res);
    return 0;
}

\(F\)

Feeling Division son said than \ (yyb \) say ...... I understand a little better here say about the son of the Secretary of the wording of it ......

Convenience first default \ (a_i = i \)

First, it is clear \ (i \ leq b_i \ leq 2n-i \)

Secondly, we consider adding two new numbers, the median is either unchanged or become a precursor or successor

So there will be two properties

\ (1.i \ leq b_i \ leq 2n-i \)

\ (2 \) , there is no \ (I <J \) , so \ (b_j <b_i <b_ { j + 1} \) or \ (b_j> b_i> b_ { j + 1} \)

We assume that there can be considered a \ (S \) indicates that all legitimate (b_i \) \ set of values, first of all there must be \ (the n-B_n = \) , then backwards if we construct \ (b \) , and considering the \ (B_i \) , the first representative of the nature and we go \ (S \) was added in \ (I \) and \ (2N-I \) , if the selected second property described the \ (B_i \) , then the \ (S \) in all values \ (B_i \) and \ (b_ {i + 1} \) number (excluding the endpoint) between should be omitted (because of the nature of \ (2 \) is clearly in front of the numbers is not the existence of such numbers)

\ (ps: \) a second point above or may be understood that, if \ (b_ {i} \ neq b_ {i + 1} \) Description \ (b_ {i + 1} \) must be pre \ ( 2i + 1 \) the number of \ (B_i \) predecessor or successor, then the number of sections within their obviously prevent them being a successor or predecessor, so \ (a \) are not present

Then we come to think about, constructed in accordance with this rule above \ (b \) is strictly legitimate, that \ (S \) all the numbers there is only a necessity, how to prove their sufficiency

From \ (b_ {i + 1} \) to \ (B_i \) procedure, if \ (B_. 1} + {I> B_i \) , then we before deleting \ (2i + 1 \) number of ratio \ (b_ {i + 1} \) large minimum of two numbers, then we can get to the legal \ (B_i \) minimum, similarly if \ (b_ {i + 1} <b_i \) can take the maximum value and the \ (b_ {i} = b_ {i + 1} \) , then, just before we delete \ (2i + 1 \) number ratio \ (B_i \) large minimum number and the ratio \ (b_i \) smaller maximum number can be taken to the maximum and minimum values

Then the next step is the calculation scheme, denoted \ (f [i] [j ] [k] \) represents reverse consider \ (B \) , considered \ (I \) number, the number of legal value of \ (J \) , and take the first of these values (k \) \ a number of programs, transfer to enumerate what the next number to take legal value in the first few on the line

Remember there is a problem that is actually \ (\ a) situation may be equal, then a property can expand depends on the circumstances

Really still looking at the code easier to understand ......

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=1e9+7;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
    R int res=1;
    for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
    return res;
}
const int N=105;
int a[N],f[N][N][N],n,res;
int main(){
    scanf("%d",&n);
    fp(i,1,n+n-1)scanf("%d",&a[i]);
    sort(a+1,a+n+n);
    f[1][1][1]=1;
    fp(i,1,n-1)fp(j,1,n+n-1)fp(k,1,j)if(f[i][j][k]){
        R int tj=j,tk=k;
        if(a[n+i]!=a[n+i-1])++tj;
        if(a[n-i]!=a[n-i+1])++tj,++tk;
        fp(h,1,tj){
            if(h<tk)upd(f[i+1][tj-(tk-h-1)][h],f[i][j][k]);
            if(h>tk)upd(f[i+1][tj-(h-tk-1)][tk+1],f[i][j][k]);
            if(h==tk)upd(f[i+1][tj][h],f[i][j][k]);
        }
    }
    fp(i,1,n+n-1)fp(j,1,i)upd(res,f[n][i][j]);
    printf("%d\n",res);
    return 0;
}

Guess you like

Origin www.cnblogs.com/yuanquming/p/11524460.html