zoukankan      html  css  js  c++  java
  • openJudge C17K:Lying Island

     地址:http://poj.openjudge.cn/practice/C17K/

    题目:

    C17K:Lying Island

    总时间限制: 
    2000ms
     
    内存限制: 
    262144kB
    描述

    There are N people living on Lying Island. Some of them are good guys, while others are bad.

    It is known that good guys are always telling truths, while bad guys may be telling either truths or lies.

    One day, an investigator came to Lying Island. He wondered how many good guys are there on the island. So he conducted an investigation.

    People on the island are numbered from 1 to N. When the investigator was interviewing person i, he showed the islander the information about at most K people numbered from max{i-K,1} to (i-1).

    Sometimes, the i-th islander would say, "Oh, person x is a good(bad) guy."

    But sometimes, the i-th islander would mince his words, "Eh, if person x is a good(bad) guy, person y is a good(bad) guy."

    Of course, x and y is less than i but no less than (i-K), because person i only saw the information of that at most K islanders.

    The investigator does not think he can infer exactly how many good guys on the island from these words, but he feels that he can figure out how many good guys at most on the island. Can you help him?

    输入
    The first line contains an integer T (1 <= T <= 50), indicating the number of test cases.

    For each test case:

    The first line contains two integers N and K (0 <= N <= 5000,1 <= K <= 10).

    Then (N-1) lines follow, each line contains a string, indicating the sentences said by person 2 to person N. Each sentence is in one of the following formats:
    1. Person i: Person x is a good guy.
    2. Person i: Person x is a bad guy.
    3. Person i: If person x is a good guy, person y is a good guy.
    4. Person i: If person x is a bad guy, person y is a bad guy.
    It is guaranteed that in each sentence, max{1,i-K} <= x,y < i and x ≠ y.
    输出
    For each test case, output one integer on a single line, indicating the number of good guys at most on the island.
    样例输入
    2
    7 2
    Person 2: Person 1 is a good guy.
    Person 3: Person 2 is a bad guy.
    Person 4: If person 3 is a good guy, person 2 is a good guy.
    Person 5: If person 4 is a good guy, person 3 is a bad guy.
    Person 6: If person 5 is a bad guy, person 4 is a good guy.
    Person 7: If person 6 is a bad guy, person 5 is a bad guy.
    7 4
    Person 2: Person 1 is a bad guy.
    Person 3: Person 2 is a bad guy.
    Person 4: If person 3 is a good guy, person 1 is a bad guy.
    Person 5: Person 2 is a bad guy.
    Person 6: If person 4 is a good guy, person 2 is a bad guy.
    Person 7: Person 6 is a bad guy.
    样例输出
    6
    4
    提示
    For the first test case, person 3 is a bad guy, and others are good.
    Person 1: Person 1 said nothing, he is a good guy.
    Person 2: Person 1 is indeed a good guy, and person 2 is saying the truth.
    Person 3: Person 2 is not a bad guy, and person 3 is lying.
    Person 4: Person 3 is not a good guy, so the prerequisite does not met. Person 4 is saying the truth.
    Person 5: Person 4 is indeed a good guy and person 3 is indeed a bad guy. Person 5 is saying the truth.
    Person 6: Person 5 is a good guy. Person 6 is saying the truth.
    Person 7: Person 6 is a good guy. Person 7 is saying the truth.
    思路:状压DP,把包括当前位的前十位状压进去就好了,然后转移。
    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef pair<int,int> PII;
    const double eps=1e-8;
    const double pi=acos(-1.0);
    const int K=5e3+7;
    const int mod=1e9+7;
    
    int n,ans,k,dp[K][1<<10];
    char sa[200];
    
    int main(void)
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&k);
            memset(dp,-1,sizeof dp);
            for(int i=0,mx=(1<<9);i<mx;i++)
                dp[1][i]=0;
            for(int i=(1<<9),mx=(1<<10);i<mx;i++)
                dp[1][i]=1;
            for(int i=2,x,y,dx,dy;i<=n;i++)
            {
                scanf("%s%d%s%s",sa,&x,sa,sa);
                if(sa[0]=='P')
                {
                    scanf("%d%s%s%s",&x,sa,sa,sa);
                    if(sa[0]=='b')  dx=0;
                    else    dx=1;
                    gets(sa);
                    for(int j=0,mx=1<<10;j<mx;j++)
                    if(((j>>(x-i+10))&1)==dx&&~dp[i-1][j])
                        dp[i][(j+mx)>>1]=max(dp[i-1][j]+1,dp[i][(j+mx)>>1]);
                    for(int j=0,mx=(1<<10);j<mx;j++)
                        dp[i][j>>1]=max(dp[i][j>>1],dp[i-1][j]);
                }
                else
                {
                    scanf("%s%d%s%s%s",sa,&x,sa,sa,sa);
                    if(sa[0]=='b')   dx=0;
                    else    dx=1;
                    scanf("%s%s%d%s%s%s",sa,sa,&y,sa,sa,sa);
                    if(sa[0]=='b')   dy=0;
                    else    dy=1;
                    gets(sa);
                    for(int j=0,mx=1<<10;j<mx;j++)
                    if(~dp[i-1][j] && !(((j>>(x-i+10))&1)==dx&&((j>>(y-i+10))&1)!=dy))
                        dp[i][(j+mx)>>1]=max(dp[i-1][j]+1,dp[i][(j+mx)>>1]);
                    for(int j=0,mx=(1<<10);j<mx;j++)
                        dp[i][j>>1]=max(dp[i-1][j],dp[i][j>>1]);
                }
            }
            ans=0;
            for(int i=0,mx=(1<<10);i<mx;i++)
                ans=max(ans,dp[n][i]);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    /*
    Person 2: Person 1 is a bad guy.
    Person 3: Person 2 is a bad guy.
    Person 4: Person 3 is a bad guy.
    Person 5: Person 4 is a bad guy.
    
    Person 2: Person 1 is a good guy.
    Person 3: Person 2 is a good guy.
    Person 4: Person 3 is a bad guy.
    Person 5: Person 4 is a good guy.
    */
  • 相关阅读:
    python 如何把在字符串里面的名字变成变量,进行复制
    2017-05-30 英语
    RYU 中如钩构建TCP数据包,设置ACK等标志
    Arch linux LXR 安装过程
    ubuntu如何配置lxr
    2017-05-27 英语
    Emacs学习笔记:移动
    9.特殊矩阵的压缩存储
    13.链路层设备
    9.CSMA_CD协议
  • 原文地址:https://www.cnblogs.com/weeping/p/7203505.html
Copyright © 2011-2022 走看看