Codeforces Round #510 (Div. 2)

A.

模拟

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
int n,m,a[105],sum,minn=10000,maxx,t;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum+=a[i];
        minn=min(minn,a[i]);
        maxx=max(maxx,a[i]);
    }
    for(int i=1;i<=n;i++)t+=maxx-a[i];
    if(t>=m)minn=maxx;
    else sum+=m,minn=sum/n+((sum%n)!=0);
    maxx+=m;
    printf("%d %d\n",minn,maxx);
}

B.

模拟

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
map<string,int>mp;
int n,a,r[10],ans;
string b;

int main(){
    scanf("%d",&n);
    mp["A"]=1,mp["B"]=2,mp["C"]=3,mp["ABC"]=7;
    mp["AB"]=4,mp["BC"]=5,mp["AC"]=6;
    for(int i=1;i<=7;i++)r[i]=1000050;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        sort(b.begin(),b.end());
        r[mp[b]]=min(r[mp[b]],a);
    }
    ans=min(min(r[1]+r[2]+r[3],min(r[4]+r[3],min(r[5]+r[1],r[6]+r[2]))),r[7]);
    ans=min(ans,min(r[4]+r[5],min(r[5]+r[6],r[4]+r[6])));
    if(ans>300000)ans=-1;
    printf("%d\n",ans);
}

C.

分情况讨论

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=200050;
int n,rec,t,tp,vis[N];
struct Node{int id,wei;}node[N],del[N];
bool operator<(Node a,Node b){
    if(a.wei!=b.wei)return a.wei<b.wei;
    return a.id<b.id;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&node[i].wei);
        node[i].id=i;
    }
    sort(node+1,node+1+n);
    for(int i=1;i<=n;i++){
        if(node[i].wei>=0){rec=i-1;break;}
        if(i==n)rec=i;
    } 
    if(rec&1)del[++tp]=node[rec];
    for(int i=rec+1;i<=n;i++){
        if(node[i].wei!=0)break;
        del[++tp]=node[i];
    }
    if(tp!=n){ 
        for(int i=1;i<tp;i++)
            printf("1 %d %d\n",del[i].id,del[i+1].id),vis[del[i].id]=1;
        if(tp)printf("2 %d\n",del[tp].id),vis[del[tp].id]=1;
        for(int i=1;i<=n;i++)if(!vis[node[i].id])node[++t]=node[i];
        for(int i=1;i<t;i++)printf("1 %d %d\n",node[i].id,node[i+1].id);
    }
    else{
        for(int i=1;i<n;i++)printf("1 %d %d\n",node[i].id,node[i+1].id);
    }
}

D.

权值线段树

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=400050;
int n,tp,a[N],tree[N<<2];
ll t,s[N],sum[N],ans;
void insert(int l,int r,int pos,int wei){
    if(l==r){tree[pos]++;return;}
    int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
    if(mid<wei)insert(mid+1,r,rson,wei);
    else insert(l,mid,lson,wei);
    tree[pos]=tree[lson]+tree[rson];
}
int query(int l,int r,int pos,int L,int R){
    if(l>=L&&r<=R)return tree[pos];
    int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
    if(mid<L)return query(mid+1,r,rson,L,R);
    else if(mid>=R)return query(l,mid,lson,L,R);
    else return query(l,mid,lson,L,R)+query(mid+1,r,rson,L,R);
}
int main(){
    scanf("%d%lld",&n,&t);t--;
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        sum[i]=sum[i-1]+a[i],s[++tp]=t+sum[i-1],s[++tp]=sum[i];
    sort(s+1,s+1+tp);
    for(int i=n;i;i--)
        insert(1,tp,1,lower_bound(s+1,s+1+tp,sum[i])-s), 
        ans+=query(1,tp,1,1,lower_bound(s+1,s+1+tp,t+sum[i-1])-s);
    printf("%lld\n",ans);
}

E.

DP(前缀和)

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=1000005,mod=998244353;
typedef long long ll;
int n,m,xx,tp,f[N],cnt[N],s[N],sx,sy;
int sumx[N],sumy[N],sumx2[N],sumy2[N];
struct Node{
    int x,y,wei;
    Node(){}
    Node(int X,int Y,int Z){x=X,y=Y,wei=Z;}
}node[N];
bool operator<(Node a,Node b){
    return a.wei<b.wei;
}
int power(int x,int y){
    int res=1;
    while(y){
        if(y&1)res=1ll*res*x%mod;
        x=1ll*x*x%mod,y>>=1;
    }return res;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&xx),node[++tp]=Node(i,j,xx);
    sort(node+1,node+1+tp),node[0].wei=-1;
    scanf("%d%d",&sx,&sy);
    for(int i=1;i<=tp;i++){
        if(node[i].wei==node[i-1].wei)cnt[i]=cnt[i-1];
        else cnt[i]=i-1;
        sumx[i]=(sumx[i-1]+node[i].x)%mod;
        sumy[i]=(sumy[i-1]+node[i].y)%mod;
        sumx2[i]=(sumx2[i-1]+1ll*node[i].x*node[i].x)%mod;
        sumy2[i]=(sumy2[i-1]+1ll*node[i].y*node[i].y)%mod;
    }
    for(int i=1;i<=tp;i++){
        f[i]=(-2ll*sumx[cnt[i]]%mod*node[i].x%mod-2ll*sumy[cnt[i]]%mod*node[i].y%mod)%mod;
        f[i]=((f[i]+sumx2[cnt[i]])%mod+sumy2[cnt[i]])%mod;
        f[i]=(f[i]+1ll*node[i].x*node[i].x%mod*cnt[i])%mod;
        f[i]=((f[i]+1ll*node[i].y*node[i].y%mod*cnt[i]%mod)%mod+mod)%mod;
        (f[i]+=s[cnt[i]])%=mod;
        f[i]=(1ll*f[i]*power(cnt[i],mod-2))%mod;
        s[i]=(s[i-1]+f[i])%mod;
        if(node[i].x==sx&&node[i].y==sy){printf("%d\n",f[i]);return 0;}
    }
}

F.

DFS+贪心

//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=2000500;
int n,k,xx,yy,first[N],nxt[N],v[N],tot,du[N],ans;
void add(int x,int y){v[tot]=y,nxt[tot]=first[x],first[x]=tot++;}
int dfs(int x,int fa){
    if(du[x]==1)return 0;
    vector<int>vec;
    for(int i=first[x];~i;i=nxt[i])if(v[i]!=fa)
        vec.push_back(dfs(v[i],x)+1);
    sort(vec.begin(),vec.end());
    for(int i=vec.size()-1;~i;i--){
        int temp=vec[i];
        if(i)temp+=vec[i-1];
        if(temp<=k)return vec[i];
        ans++;
    }return -1000000;
}
int main(){
    memset(first,-1,sizeof(first));
    scanf("%d%d",&n,&k);
    for(int i=1;i<n;i++){
        scanf("%d%d",&xx,&yy);
        add(xx,yy),add(yy,xx);
        du[xx]++,du[yy]++;
    }
    for(int i=1;i<=n;i++)if(du[i]>1){if(dfs(i,-1)>0)ans++;break;}
    printf("%d\n",ans);
}

 

posted @ 2018-09-20 20:56 SiriusRen 阅读(...) 评论(...) 编辑 收藏