地址: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-;
const double pi=acos(-1.0);
const int K=5e3+;
const int mod=1e9+; int n,ans,k,dp[K][<<];
char sa[]; int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
memset(dp,-,sizeof dp);
for(int i=,mx=(<<);i<mx;i++)
dp[][i]=;
for(int i=(<<),mx=(<<);i<mx;i++)
dp[][i]=;
for(int i=,x,y,dx,dy;i<=n;i++)
{
scanf("%s%d%s%s",sa,&x,sa,sa);
if(sa[]=='P')
{
scanf("%d%s%s%s",&x,sa,sa,sa);
if(sa[]=='b') dx=;
else dx=;
gets(sa);
for(int j=,mx=<<;j<mx;j++)
if(((j>>(x-i+))&)==dx&&~dp[i-][j])
dp[i][(j+mx)>>]=max(dp[i-][j]+,dp[i][(j+mx)>>]);
for(int j=,mx=(<<);j<mx;j++)
dp[i][j>>]=max(dp[i][j>>],dp[i-][j]);
}
else
{
scanf("%s%d%s%s%s",sa,&x,sa,sa,sa);
if(sa[]=='b') dx=;
else dx=;
scanf("%s%s%d%s%s%s",sa,sa,&y,sa,sa,sa);
if(sa[]=='b') dy=;
else dy=;
gets(sa);
for(int j=,mx=<<;j<mx;j++)
if(~dp[i-][j] && !(((j>>(x-i+))&)==dx&&((j>>(y-i+))&)!=dy))
dp[i][(j+mx)>>]=max(dp[i-][j]+,dp[i][(j+mx)>>]);
for(int j=,mx=(<<);j<mx;j++)
dp[i][j>>]=max(dp[i-][j],dp[i][j>>]);
}
}
ans=;
for(int i=,mx=(<<);i<mx;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
return ;
} /*
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.
*/

openJudge C17K:Lying Island的更多相关文章

  1. C17K:Lying Island

    链接 题意: 有n个人,每个人可能会说: 第x个人是好人/坏人 如果第x个人是好人/坏人,则第y个人是好人/坏人 思路: 状压dp,首先每个人所说的人只能是他前面10个人,所以对于第i个人记录下,他前 ...

  2. 【状压DP】OpenJ_POJ - C17K Lying Island

    https://vjudge.net/contest/171652#problem/K [题意] 小岛上有n个人,有些是好人(一定是真话),有些是坏人(可能是真话也可能是假话),现在要判断最多有多少好 ...

  3. PKU_campus_2017_K Lying Island

    思路: 题目链接http://poj.openjudge.cn/practice/C17K/ 状压dp.dp[i][j]表示第i - k人到第i人的状态为j的情况下前i人中最多有多少好人. 实现: # ...

  4. Poj 1328 / OpenJudge 1328 Radar Installation

    1.Link: http://poj.org/problem?id=1328 http://bailian.openjudge.cn/practice/1328/ 2.Content: Radar I ...

  5. OpenJudge/Poj 1753 Flip Game

    1.链接地址: http://bailian.openjudge.cn/practice/1753/ http://poj.org/problem?id=1753 2.题目: 总时间限制: 1000m ...

  6. uva 592 Island of Logic (收索)

      Island of Logic  The Island of Logic has three kinds of inhabitants: divine beings that always tel ...

  7. LightOJ 1418 Trees on My Island (Pick定理)

    题目链接:LightOJ 1418 Problem Description I have bought an island where I want to plant trees in rows an ...

  8. [LeetCode] Island Perimeter 岛屿周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  9. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

随机推荐

  1. ios开发之--PDF文件生成

    写项目的时候,碰到一个需求,就是在手机端根据指定的文件内容生成PDF文件,并可以保存到手机上,因为以前只是听说过,没有真正的去了解过这个需求,通过查阅资料,可以实现这个功能,话不多说,代码如下: -( ...

  2. JSON-Server 安装

    在后台还没给接口之前,使用JSON-Server搭建一台JSON服务器,将接口要返回的数据放在json文件里面.然后请求这些数据,这样我们可以先做一些东西,等后台接口好了之后直接替换就可以了,不必一直 ...

  3. 使用js里面的迭代器filter实现数组去重

    实现数组去重的方法很多,最原始的方法是一个值一个值的去遍历,写到空数组里面: let r=[],arr = ['a', 'b', 'c', 'a']; for(var i=0,len=arr.leng ...

  4. Python Pypi 修改 国内源(以豆瓣源为例)

    参考:http://pip.readthedocs.io/en/latest/user_guide/#config-file Pypi在国内豆瓣源的地址如下: http://pypi.douban.c ...

  5. egret.Capabilities 在pc和移动端输出值

    egret.log("Device:", egret.Capabilities.os, App.DeviceUtils.IsWeb, App.DeviceUtils.isMobil ...

  6. LeetCode 笔记系列 18 Maximal Rectangle [学以致用]

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  7. js 闭包与垃圾回收-待删

    关于闭包请看戳 串讲-解释篇:作用域,作用域链,执行环境,变量对象,活动对象,闭包,本篇写的不太好: 先摆定义: 函数对象,可以通过作用域链相互关联起来,函数体内部的变量都可以保存在函数作用域内,这种 ...

  8. Python全栈day28(上下文管理)

    我们知道在操作文件对象的时候可以这么写 with open('a.txt',''r) as f: 代码 上述叫做上下文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明 ...

  9. hibernate缓存,四种状态

    FlushMode.AUTO:Hibernate判断对象属性有没有改变,是默认的清理模式 FlushMode.COMMIT:在事务结束之前清理Session的缓存,其他任何时候都不清理缓存 Flush ...

  10. HDU 4417 Super Mario(线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...