codeforces round #321 (div2)

codeforces#321(div2) 

A题:水题。

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

typedef long long ll;
const int maxn=1000100;

ll n,a[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>n){
        REP(i,1,n) scanf("%I64d",&a[i]);
        int ans=1,now=1;
        REP(i,2,n){
            if(a[i]>=a[i-1]) now++;
            else now=1;
            ans=max(ans,now);
        }
        printf("%d\n",ans);
    }
    return 0;
}
View Code

B题:排序+贪心,水题。

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

typedef long long ll;
const int maxn=1000100;

int n,d;
struct Node
{
    ll m,s;
    friend bool operator<(Node A,Node B)
    {
        if(A.m<B.m) return 1;
        if(A.m==B.m) return A.s<B.s;
        return 0;
    }
};Node a[maxn];

int main()
{
    freopen("in.txt","r",stdin);
    while(cin>>n>>d){
        REP(i,1,n){
            scanf("%I64d%I64d",&a[i].m,&a[i].s);
        }
        ll ans=0,now=0;
        sort(a+1,a+n+1);
        int l=1,r=1;
        REP(r,1,n){
            if(a[r].m-a[l].m<d) now+=a[r].s;
            else{
                now+=a[r].s;
                while(a[r].m-a[l].m>=d) now-=a[l++].s;
            }
            //cout<<l<<" "<<r<<" "<<now<<endl;
            ans=max(ans,now);
        }
        cout<<ans<<endl;
    }
    return 0;
}
View Code

C题:dfs,水题。

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PB push_back
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=1000100;

int n,m;
int has[maxn];
vector<int> G[maxn];
int u,v;
int deg[maxn];

int dfs(int pre,int u,int cat)
{
    if(has[u]) cat+=has[u];
    else cat=0;
    if(cat>m) return 0;
    if(u!=1&&deg[u]==1) return 1;
    int res=0;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==pre) continue;
        res+=dfs(u,v,cat);
    }
    return res;
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>n>>m){
        REP(i,0,n) G[i].clear();
        REP(i,1,n) scanf("%d",&has[i]);
        MS0(deg);
        REP(i,1,n-1){
            scanf("%d%d",&u,&v);
            G[u].PB(v);
            G[v].PB(u);
            deg[u]++;deg[v]++;
        }
        printf("%d\n",dfs(0,1,0));
    }
    return 0;
}
View Code

D题:状压dp,TSP。

比赛的时候写的递推居然没过!!!!!!!!!赛后改成记忆化搜索直接1A了。。。。看来以后TSP就写记忆化搜索的形式比较保险一点。

#include<bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PB push_back
#define MS0(a) memset(a,0,sizeof(a))

using namespace std;

typedef long long ll;
const int maxn=1100;
const int INF=(1<<29);

int n,m,k;
ll c[maxn][maxn];
ll a[maxn];
ll x,y,z;
ll dp[20][1<<20];
ll cnt[1<<20];

int Cnt(int x)
{
    int res=0;
    for(int i=0;(1<<i)<=x;i++){
        if(x&(1<<i)) res++;
    }
    return res;
}

void Init()
{
    for(int i=0;i<(1<<20);i++){
        cnt[i]=Cnt(i);
    }
}

ll dfs(int i,int s)
{
    ll &res=dp[i][s];
    if(~res) return res;
    if((s&(1<<i))==0) return res=-INF;
    if(cnt[s]==1) return res=a[i];
    for(int j=0;j<n;j++){
        if(i==j) continue;
        if(s&(1<<j)) res=max(res,dfs(j,s^(1<<i))+c[j][i]+a[i]);
    }
    return res;
}

int main()
{
    freopen("in.txt","r",stdin);
    Init();
    while(cin>>n>>m>>k){
        REP(i,0,n-1) scanf("%I64d",&a[i]);
        MS0(c);
        while(k--){
            scanf("%I64d%I64d%I64d",&x,&y,&z);
            x--;y--;
            c[x][y]=z;
        }
        memset(dp,-1,sizeof(dp));
        ll ans=0;
        for(int i=0;i<n;i++){
            for(int s=0;s<(1<<n);s++){
                if(cnt[s]==m) ans=max(dfs(i,s),ans);
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
View Code

 虽然D题意外地没过,但还是涨分了,要是D题过了就直接涨到紫了,虽然没涨到紫,不过今天除了D题TSP没过时没能及时选择改成记忆化搜索的形式这点不好以外,其它的比赛策略手速基本没什么问题。感觉自己距离紫名越来越近了,不过还是要学更多的算法和数据结构,AC自动机,后缀自动机,splay,LCT,博弈论,数学,数论,计算几何,图论,各种dp,等等,还有很多很多要学。

对了,还有这场的E题,线段树.

E题:线段树+字符串hash

这题得另外写一个题解

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

using namespace std;

typedef unsigned long long ull;
const int maxn=100100;
const int INF=(1<<29);

const ull x=13LL;
const ull p=1e9+7;
int n,m,k;
int q;
int op,L,R,c;
char s[maxn];
ull xp[maxn];
ull xs[maxn];

struct SegTree
{
    int l,r;
    int tag;
    ull h;
    void debug(int rt)
    {
        printf("rt=%2d l=%2d r=%2d tag=%2d h=%2llu\n",rt,l,r,tag,h);
    }
};SegTree T[maxn<<2];

void push_down(int rt,int l,int r,int m)
{
    if(T[rt].tag!=-1){
        T[rt<<1].tag=T[rt<<1|1].tag=T[rt].tag;
        T[rt<<1].h=((T[rt].tag%p)*(xs[m-l]%p))%p;
        T[rt<<1|1].h=((T[rt].tag%p)*(xs[r-m-1]%p))%p;
        T[rt].tag=-1;
    }
}

void push_up(int rt,int l,int r,int m)
{
    T[rt].h=(T[rt<<1].h+((T[rt<<1|1].h%p)*(xp[m-l+1]%p)%p))%p;
}

void build(int l,int r,int rt)
{
    T[rt].l=l;T[rt].r=r;
    T[rt].tag=-1;
    if(l==r){
        T[rt].h=s[l-1]-'0';
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt,l,r,m);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        T[rt].tag=c;
        T[rt].h=(c*xs[r-l])%p;
        return;
    }
    int m=(l+r)>>1;
    push_down(rt,l,r,m);
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    push_up(rt,l,r,m);
}

ull query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R) return T[rt].h;
    int m=(l+r)>>1;
    push_down(rt,l,r,m);
    ull res=0;
    if(L<=m){
        if(R<=m) res=query(L,R,lson);
        else res=(query(L,R,lson)+((query(L,R,rson)%p)*xp[m-max(l,L)+1]))%p;
    }
    else res=query(L,R,rson);
    return res%p;
}

bool judge(int l1,int r1,int l2,int r2)
{
    ull h1=query(l1,r1,1,n,1);
    ull h2=query(l2,r2,1,n,1);
    return h1==h2;
}

void Init()
{
    xp[0]=1;
    for(ull i=1;i<maxn;i++) xp[i]=(xp[i-1]*x)%p;
    xs[0]=1;
    for(ull i=1;i<maxn;i++) xs[i]=(xs[i-1]+xp[i])%p;
}

int main()
{
    freopen("in.txt","r",stdin);
    Init();
    while(~scanf("%d%d%d",&n,&m,&k)){
        scanf("%s",s);
        q=m+k;
        build(1,n,1);
        while(q--){
            scanf("%d%d%d%d",&op,&L,&R,&c);
            if(op==1) update(L,R,c,1,n,1);
            else{
                if(R-L+1==c) puts("YES");
                else puts(judge(L,R-c,L+c,R)?"YES":"NO");
            }
        }
    }
    return 0;
}
View Code

 

posted @ 2015-09-23 03:44  __560  阅读(209)  评论(0编辑  收藏  举报