思路:

题目链接http://poj.openjudge.cn/practice/C17K/

状压dp。dp[i][j]表示第i - k人到第i人的状态为j的情况下前i人中最多有多少好人。

实现:

 #include <bits/stdc++.h>
using namespace std;
int dp[][];
struct node
{
int type, id1, id2;
bool f1, f2;
};
node a[];
bool same(bool a, bool b)
{
return (a && b) || (!a && !b);
}
int main()
{
int t, n, k;
cin >> t;
while (t--)
{
memset(dp, , sizeof dp);
cin >> n >> k;
string s;
getchar();
for (int i = ; i < n; i++)
{
getline(cin, s);
string tmp;
stringstream ss(s);
vector<string> v;
while (ss >> tmp) v.push_back(tmp);
if (v[][] == 'I')
{
a[i].type = ;
a[i].id1 = atoi(v[].c_str());
a[i].id2 = atoi(v[].c_str());
a[i].f1 = v[] == "good" ? true : false;
a[i].f2 = v[] == "good" ? true : false;
}
else
{
a[i].type = ;
a[i].id1 = atoi(v[].c_str());
a[i].f1 = v[] == "good" ? true : false;
}
} int msk = ( << k + ) - ;
dp[][] = ;
for (int i = ; i < n; i++)
{
memset(dp[i & ], , sizeof dp[i & ]);
for (int j = ; j < << k + ; j++)
{
if (i < k + && j >= << i) continue;
int tmp = j << & msk;
dp[i & ][tmp] = max(dp[i & ][tmp], dp[i - & ][j]);
if (a[i].type == )
{
int p1 = i - a[i].id1;
if (same(a[i].f1, j >> p1 & ))
{
dp[i & ][tmp | ] = max(dp[i & ][tmp | ], dp[i - & ][j] + );
}
}
else
{
int p1 = i - a[i].id1, p2 = i - a[i].id2;
bool g1 = j >> p1 & , g2 = j >> p2 & ;
if (!(same(a[i].f1, g1) && !same(a[i].f2, g2)))
dp[i & ][tmp | ] = max(dp[i & ][tmp | ], dp[i - & ][j] + );
}
}
}
int ans = ;
for (int i = ; i < << k + ; i++)
ans = max(ans, dp[n - & ][i]);
cout << ans << endl;
}
return ;
}

PKU_campus_2017_K Lying Island的更多相关文章

  1. openJudge C17K:Lying Island

    地址:http://poj.openjudge.cn/practice/C17K/ 题目: C17K:Lying Island 查看 提交 统计 提问 总时间限制:  2000ms 内存限制:  26 ...

  2. C17K:Lying Island

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

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

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

  4. uva 592 Island of Logic (收索)

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

  5. 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 ...

  6. [LeetCode] Island Perimeter 岛屿周长

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

  7. 463. Island Perimeter

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

  8. Leetcode-463 Island Perimeter

    #463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...

  9. LeetCode Island Perimeter

    原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...

随机推荐

  1. CentOS7.2安装Vim8和YouCompleteMe

    1.环境 本文使用VMWare虚拟机进行实验,客户机系统是CentOS 7.2最小安装(CentOS-7-x86_64-Minimal-1511.iso) 最终实现效果:安装vim8 + python ...

  2. hdu 2112 HDU Today 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloo ...

  3. classname.this 和 this的使用场景

    今天在写代码时,发现在写了一个内部类,而在内部类中需要调用外部类的实例方法,直接使用this调用发现调用的不是外部类而是内部类,于是查找资料原来需要使用外部类的classname.this这样的调用, ...

  4. Unable to instantiate receiver XXXXXX

    运行一个工程的时候时logcat中出现了“Unable to instantiate receiver XX..”. 检查后发现,由于是东拼西凑的代码,所以在Manifest文件里注册了Receive ...

  5. 理解Objective-C Runtime(四)Method Swizzling

    Objective-C对象收到消息之后,究竟会调用何种方法需要在运行期间才能解析出来.那你也许会问:与给定的选择子名称相应的方法是不是也可以在runtime改变呢?没错,就是这样.若能善用此特性,则可 ...

  6. 【hdu 4374】One Hundred Layer

    [题目链接] 点击打开链接 [算法] 不难看出,这题可以用动态规划来解决 f[i][j]表示第i行第j列能够取得的最大分数 则如果向右走,状态转移方程为f[i][j]=max{f[i-1][k]+a[ ...

  7. Spring中Bean获取IOC容器服务的方法

    Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...

  8. Javascript位置 body之前、后执行顺序(探讨)!

    转载来自:http://blog.csdn.net/dxnn520/article/details/7927461 简介:当页面加载的时候,嵌入html标记的js代码和位于<body>&l ...

  9. eclipse整合tomcat

    首先确保jdk已经安装好 步骤1 获得服务器运行环境配置,Window/Preferences/Server/Runtime Environmen l步骤2 添加服务器 步骤3 选择服务器在硬盘的地址 ...

  10. 007--linux常用命令nginx和nfs服务

    作业: 集群搭建 1.部署nginx反向代理三个web服务,调度算法使用加权轮询: 2.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: 一.nginx服务 1. 先 ...