CodeForces 754C Vladik and chat (DP+暴力)
题意:给定n个人的m个对话,问能不能找一个方式使得满足,上下楼层人名不同,并且自己不提及自己。
析:首先预处理每一层能有多少个user可选,dp[i][j] 表示第 i 层是不是可以选第 j 个user。最后再输出即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1LL << 60;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<string, int> mp;
int cnt;
vector<string> v;
bool vis[maxn][maxn];
int dp[maxn][maxn], a[maxn];
string name[maxn]; inline int getId(const string &s){
if(!mp.count(s)) name[mp[s] = cnt++] = s;
return mp[s];
} string getF(const string &t){
string s = "";
for(int j = 0; t[j] != ':'; ++j) s.push_back(t[j]);
return s;
} void calc1(bool *is, string s){
for(int i = 0; i < s.size(); ++i)
if(ispunct(s[i])) s[i] = ' ';
// cout << s << endl;
stringstream ss(s);
while(ss >> s) if(mp.count(s)) is[mp[s]] = true;
} int main(){
ios::sync_with_stdio(false);
int T; cin >> T;
while(T--){
cin >> n;
mp.clear();
cnt = 1;
string s;
memset(vis, 0, sizeof vis);
for(int i = 0; i < n; ++i){
cin >> s;
getId(s);
}
cin >> m;
cin.get();
v.clear();
v.push_back(" ");
for(int i = 1; i <= m; ++i){
getline(cin, s);
v.push_back(s);
calc1(vis[i], s);
a[i] = v[i][0] == '?' ? 0 : mp[getF(s)];
} memset(dp, 0, sizeof dp);
int tmp = 1;
dp[0][0] = 1;
for(int i = 1; i <= m; ++i){
int tt = 0;
if(a[i]){
dp[i][a[i]] = tmp > dp[i-1][a[i]];
tt = dp[i][a[i]];
}
else for(int j = 1; j <= n; ++j){
dp[i][j] = (!vis[i][j] && tmp > dp[i-1][j]);
tt += dp[i][j];
}
tmp = tt;
}
tmp = 0;
for(int i = 1; i <= n; ++i) if(dp[m][i]){ tmp = i; break; }
if(!tmp) cout << "Impossible" << endl;
else{
for(int i = m; i > 0; --i)
for(int j = 0; j <= n; ++j)
if(j != tmp && dp[i-1][j]){
if(!a[i]) a[i] = -tmp;
tmp = j;
break;
}
for(int i = 1; i <= m; ++i)
if(a[i] > 0) cout << v[i] << endl;
else cout << name[-a[i]] + v[i].substr(1) << endl;
}
}
return 0;
}
CodeForces 754C Vladik and chat (DP+暴力)的更多相关文章
- CodeForces 754C Vladik and chat ——(xjbg)
虽然是xjbg的题目,但是并不很好做. 题意不难理解.读入有点麻烦.做法是先正着推每段对话的?可能是谁说的,然后反过来选择即可.正推时,其中vis数组表示谁已经被用过了,cnt表示该组当前可以选择几个 ...
- CodeForces 446A DZY Loves Sequences (DP+暴力)
题意:给定一个序列,让你找出一个最长的序列,使得最多改其中的一个数,使其变成严格上升序列. 析:f[i] 表示以 i 结尾的最长上升长度,g[i] 表示以 i 为开始的最长上升长度,这两个很容易就求得 ...
- Codeforces Round #390 (Div. 2) C. Vladik and chat(dp)
http://codeforces.com/contest/754/problem/C C. Vladik and chat time limit per test 2 seconds memory ...
- codeforces 811E Vladik and Entertaining Flags(线段树+并查集)
codeforces 811E Vladik and Entertaining Flags 题面 \(n*m(1<=n<=10, 1<=m<=1e5)\)的棋盘,每个格子有一个 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- Codeforces 811C Vladik and Memorable Trip (区间异或最大值) (线性DP)
<题目链接> 题目大意: 给你n个数,现在让你选一些区间出来,对于每个区间中的每一种数,全部都只能出现在这个区间. 每个区间的价值为该区间不同的数的异或值之和,现在问你这n个数最大的价值是 ...
- Codeforces 914 C 数位DP+暴力打表+思维
题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...
- 【dp】codeforces C. Vladik and Memorable Trip
http://codeforces.com/contest/811/problem/C [题意] 给定一个自然数序列,在这个序列中找出几个不相交段,使得每个段的异或值之和相加最大. 段的异或值这样定义 ...
- Educational Codeforces Round 19 E. Array Queries(暴力)(DP)
传送门 题意 给出n个数,q个询问,每个询问有两个数p,k,询问p+k+a[p]操作几次后超过n 分析 分块处理,在k<sqrt(n)时,用dp,大于sqrt(n)用暴力 trick 代码 #i ...
随机推荐
- BGP多线单IP技术实现形式以及其他双线对比
自从电信与网通分离之后,北方网通与南方电信网络的互联瓶颈问题一直没有得到很好的解决,这个问题也严重困扰广大的ICP服务商.ICP也只能根据自己网站主流用户群是在南方还是在北方,服务重点是在南方还是北方 ...
- mac 终端下ssh 登录远程服务器不发输入中文
转:https://segmentfault.com/q/1010000000150673 用Mac的iterm2 ssh连上去,终端显示中文乱码,也不能输入中文,然而本地终端可以显示和输入. 解决方 ...
- eclipse 使用问题
eclipse 启动失败,错误信息为org.eclipse.swt.SWTException: Failed to execute runnable 方法三:删除了workspace\.metadat ...
- namenode无法启动
查看日志错误信息关键语句: There appears to be a gap in the edit log. We expected txid 44353, but got txid 原因: n ...
- Linux 相关的error处理
1 dpkg: error: duplicate file trigger interest for filename Notice the first and last lines of /var ...
- linux中找不到/etc/sysconfig/iptables
解决办法初始化iptables. #iptables -F #service iptables save #service iptables restart
- hdu1024
#include <cstdio>#include <iostream>const int MAX = 1000005; using namespace std; int nu ...
- express学习点滴- methodOverride
app.use(express.methodOverride());在app.js中可以看到这个中间件,去翻文档,几乎没有提到.一番寻找,整理如下. 作用是使用REST风格的http请求时,进行改写后 ...
- [妙味DOM]第六课:鼠标滚轮和COOKIE
知识点总结: 鼠标滚轮事件 存在兼容性问题: IE/chorme : onmousewheel FF : DOMMouseScroll,必需用在addEventListener下,例如: if (ob ...
- php 类型
var_dump 打印出类型和值 整型inter用 decimal 表示为 decimal : [1-9][0-9]* | 0 (int) ( (0.1+0.7) * 10 ); 强制类 ...