PKU_campus_2017_K Lying Island
思路:
题目链接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的更多相关文章
- openJudge C17K:Lying Island
地址:http://poj.openjudge.cn/practice/C17K/ 题目: C17K:Lying Island 查看 提交 统计 提问 总时间限制: 2000ms 内存限制: 26 ...
- C17K:Lying Island
链接 题意: 有n个人,每个人可能会说: 第x个人是好人/坏人 如果第x个人是好人/坏人,则第y个人是好人/坏人 思路: 状压dp,首先每个人所说的人只能是他前面10个人,所以对于第i个人记录下,他前 ...
- 【状压DP】OpenJ_POJ - C17K Lying Island
https://vjudge.net/contest/171652#problem/K [题意] 小岛上有n个人,有些是好人(一定是真话),有些是坏人(可能是真话也可能是假话),现在要判断最多有多少好 ...
- uva 592 Island of Logic (收索)
Island of Logic The Island of Logic has three kinds of inhabitants: divine beings that always tel ...
- 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 ...
- [LeetCode] Island Perimeter 岛屿周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- 463. Island Perimeter
https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...
- Leetcode-463 Island Perimeter
#463. Island Perimeter You are given a map in form of a two-dimensional integer grid where 1 represe ...
- LeetCode Island Perimeter
原题链接在这里:https://leetcode.com/problems/island-perimeter/ 题目: You are given a map in form of a two-dim ...
随机推荐
- 数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)
数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...
- linq to xml There are multiple root elements.
添加xml结点的时候 var temp2 = temp1.Element("staticContent"); if (temp2 != null) { string str = & ...
- 【Maven】pom.xml(2)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- 聊聊Java SPI机制
一.Java SPI机制 SPI(Service Provider Interface)是JDK内置的服务发现机制,用在不同模块间通过接口调用服务,避免对具体服务服务接口具体实现类的耦合.比如JDBC ...
- java io流中怎么在一个文本中追加字符串
1/ FileOutputStream(File file, boolean append)2/FileWriter(File file, boolean append) 不管哪一种IO都有 appe ...
- 任务46:Identity MVC:登录逻辑实现
任务46:Identity MVC:登录逻辑实现 实现登陆的方法 退出方法 _layout里面增加如下的代码: Login.cshtml 运行代码测试: 这个地方的单词之前拼错了.这里进行修正 输入账 ...
- Epoll简介以及例子
第一部分:Epoll简介 问题 : Select,Poll和Epoll的区别 答案 : Epoll和Select的区别 1. 遍历方式的区别.select判断是否有事件发生是遍历的,而epoll是事 ...
- 基于 Algebird 谈一谈代数数据类型在数据聚合中的应用
此文已由作者肖乃同授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 代数数据类型是指满足一定数学特性的数据类型, 这些特性使得计算能够很方便的并行化,在Scalding和 S ...
- VS代码中常用 正则表达式
1. #define ABC 1 修改为 enum 样式: #define (.+?)\s+(.+?)$ $1 = $2 ,