Codeforces 876E:2-SAT 或者 瞎搞
来源:互联网 发布:如何提高唱歌水平 知乎 编辑:程序博客网 时间:2024/06/05 17:58
题意:给出N个字符串,每个字符用整数来代表,规定一个字符可以有大小写,设小写为1,大写为1‘,并且字典序表示为:1’<2'<3'<4'<1<2<3<4,现在要求你选出若干个字符变成大写(所有串中的这个字符都变成大写),使得N个字符串是按照字典序不降的。无解输出-1
题解:正解是2-SAT。。。。我要讲一个怪算法。。。
首先有一个不完整的思路是:正着扫一遍,要保证第i个字符串<第i+1个字符串,方法是,暴力搞掉最长公共前缀,然后拿到第一个不同的字符,必须让第i个串的那个字符小才行,如果不满足,就让那个字符变成大写的,如果变成大写了还不满足就是No。
但是这样有问题,后面有一些字符变成大写了之后,可能前边的字典序就保证不了了,那么显然 另外一个不完整的思路就是:反着再扫一次。
但是这样还是有问题,于是我们发现了,问题其实是这样子:如果某次扫描,把某个字符变成了大写,那么之前的字典序可能会被破坏掉,那么我们至少需要再来一次扫描检查一次。
于是骚的算法来了:正着扫一次+反着扫一次,如果把某个字符变成了大写,就继续疯狂扫,如果没有,就可以书出答案了。
我他妈不知道怎么就AC了。。。
过几天把2-SAT的板子补出来ORZ。。。
Code:
#include<bits/stdc++.h>using namespace std;const int maxm = 2e5;bool big[maxm];int m,n;int ch[maxm];int l[maxm];int pos[maxm];int cnt=0;bool small(int x_,int y_){if (big[x_]==big[y_]){return x_<=y_;}else{return big[x_];}}int main(){scanf("%d%d",&n,&m);for (int i=0;i<n;i++){int len;scanf("%d",&len);l[i]=len;pos[i] =cnt;for (int j=0;j<len;j++){scanf("%d",ch+cnt+j);}cnt+=len;}pos[n]=cnt;while (true){bool flag = false;for (int i=0;i<n-1;i++){int p1=pos[i];int p2 = pos[i+1];int l1=l[i];int l2 = l[i+1];int mm = min(l1,l2);int del =0;while (del<mm&&ch[p1+del]==ch[p2+del]){del++;}//cout<<i<<" "<<del<<endl;if (del==mm&&del==l1)continue;if (del==mm&&del==l2){cout<<"No"<<endl;return 0;}if (small(ch[p1+del],ch[p2+del])){continue;}else{if (!big[ch[p1+del]])flag = true;big[ch[p1+del]] =true;if (small(ch[p1+del],ch[p2+del])){continue;}else{cout<<"No"<<endl;return 0;}}}for (int i=n-1;i>0;i--){int p2 = pos[i];int p1 = pos[i-1];int l2 = l[i];int l1 = l[i-1];int mm = min(l1,l2);int del =0;while (del<mm&&ch[p1+del]==ch[p2+del])del++;//cout<<"! "<<i<<" "<<del<<endl; if (del==mm&&del==l1)continue;if (del==mm&&del==l2){cout<<"No"<<endl;return 0;}if (small(ch[p1+del],ch[p2+del])){continue;}else{if (!big[ch[p1+del]])flag = true;big[ch[p1+del]] = true;if (small(ch[p1+del],ch[p2+del])){continue;}else{cout<<"No"<<endl;return 0;}}}if (!flag)break;}printf("Yes\n");int cnt =0;for (int i=1;i<=m;i++){if (big[i])cnt++;}printf("%d\n",cnt);for (int i=1;i<=m;i++){if(big[i])printf("%d ",i);}return 0;}/*5 53 1 2 34 1 2 4 43 1 3 4 3 1 2 43 4 4 4*/
阅读全文
0 0
- Codeforces 876E:2-SAT 或者 瞎搞
- [2-SAT] Codeforces #876E. National Property
- 【2-SAT】Codeforces 876E National Property
- Codeforces 876 E National Property(2-SAT)
- Codeforces 876 E. National Property (2-SAT)
- Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~
- [2-SAT] Codeforces 668E #348 (VK Cup 2016 Round 2, Div. 1 Edition) E. Little Artem and 2-SAT
- Codeforces Round #348 (VK Cup 2016 Round 2) E F (2-sat. 待补)
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) E. National Property (2-SAT问题)
- CodeForces 228E The Road to Berland is Paved With Good Intentions 2Sat求解
- CodeForces 228E The Road to Berland is Paved With Good Intentions (2-Sat)
- Codeforces Round #441 E. National Property (2-sat tarjan后输出任意一组合法解)
- 【瞎搞】 VK Cup 2015 - Round 2 E. Correcting Mistakes
- Codeforces Round #424 E. Cards Sorting 线段树/数据结构瞎搞/模拟
- 【瞎搞】 Codeforces Round 276 DIV 2 C.Bits
- codeforces 468B 2-sat
- Codeforces Round #141 (Div. 2)E. The Road to Berland is Paved With Good Intentions(2-SAT模板题)
- CodeForces 723F【DFS瞎搞】
- 谈面向过程和面向对象
- 在一个 U 盘上安装多个 Linux 发行版
- APP开发1-欢迎界面转登录
- 【C语言】【unix c】信号量集(system v ipc)
- Codeforces Round #441 B.Divisiblity of Differences
- Codeforces 876E:2-SAT 或者 瞎搞
- 【C语言】【unix c】web服务器项目笔记
- P1103
- rhs和rhsa文件属性的文件删除方法
- 写给自己的JAVA工程师之路-链表
- ios-闭包的写法
- 2016CCPC东北地区大学生程序设计竞赛
- 【C语言】【unix c】web服务器项目代码
- Ubuntu下更改用户名和主机名