码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #260 (Div. 1)

时间:2014-09-02 00:09:03      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   art   

大三目标把codeforces打成黄色以上!

从div1下手,每次做前三道题。

A

一道简单的DP。

bubuko.com,布布扣
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
#define LL long long
#define MAXN 100005
using namespace std;
LL vis[100005];
LL dp[1000005];
int main()
{
    int n;
    scanf("%d",&n);
    int maxi=0;
    for(int i=0; i<n; ++i)
    {
        int a;
        scanf("%d",&a);
        vis[a]+=a;
        maxi=max(maxi,a);
    }
    for(int i=1; i<=maxi; ++i)
        if(i-2>=0)
            dp[i]=max(dp[i-1],dp[i-2]+vis[i]);
        else
            dp[i]=vis[i];
    printf("%I64d\n",dp[maxi]);
    return 0;
}
View Code

B

Tire+dp+博弈

win[i]表示当前在i结点时先手是否有胜利可能,lost[i]表示当前在i结点时先手是否有失败可能。

如此可计算出先手是否有胜利和失败可能,然后再进行简单博弈即可。

bubuko.com,布布扣
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int n,K;
struct Tire
{
    int ch[100005][26];
    bool win[100005],lost[100005];
    int sz;
    int newnode()
    {
        memset(ch[sz],0,sizeof(ch[sz]));
        win[sz]=lost[sz]=0;
        return sz++;
    }
    void init()
    {
        sz=0;
        newnode();
    }
    int getX(char c)
    {
        return c-a;
    }
    void insert(char *word)
    {
        int now=0;
        for(int i=0; word[i]; ++i)
        {
            int x=getX(word[i]);
            if(ch[now][x]==0)
                ch[now][x]=newnode();
            now=ch[now][x];
        }
    }
    bool getWin(int now)
    {
        win[now]=false;
        bool noChild=true;
        for(int i=0; i<26; ++i)
        {
            if(ch[now][i])
            {
                noChild=false;
                win[now]|=(!getWin(ch[now][i]));
            }
        }
        if(noChild) return win[now]=false;
        return win[now];
    }
    bool getLost(int now)
    {
        lost[now]=false;
        bool noChild=true;
        for(int i=0; i<26; ++i)
        {
            if(ch[now][i])
            {
                noChild=false;
                lost[now]|=(!getLost(ch[now][i]));
            }
        }
        if(noChild) return lost[now]=true;
        return lost[now];
    }
};
char word[100005];
Tire tree;
int main()
{
    scanf("%d%d",&n,&K);
    tree.init();
    for(int i=1; i<=n; ++i)
    {
        scanf("%s",word);
        tree.insert(word);
    }
    bool firstWin=false,firstLost=false;
    firstWin=tree.getWin(0);
    firstLost=tree.getLost(0);
    if(!firstWin) puts("Second");
    else
    {
        if(firstLost) puts("First");
        else
        {
            if(K&1) puts("First");
            else puts("Second");
        }
    }
    return 0;
}
View Code

 

Codeforces Round #260 (Div. 1)

标签:style   blog   http   color   os   io   ar   for   art   

原文地址:http://www.cnblogs.com/kkkwjx/p/3950115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!