设这个序列的前缀和为Si(0 <= i <= n),S0 = 0

每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下。

还要注意一下前缀和相等的情况,所以用一个并查集来查询。

 #include <cstdio>
#include <cstring> const int maxn = ;
int n;
int G[maxn][maxn];
char s[];
int sum[maxn], a[maxn]; int topo[maxn], c[maxn], t; void dfs(int u)
{
c[u] = ;
for(int v = ; v <= n; v++) if(G[u][v] && !c[v]) dfs(v);
topo[--t] = u;
} void toposort()
{
t = n + ;
memset(c, , sizeof(c));
for(int u = ; u <= n; u++) if(!c[u]) dfs(u);
} int p[maxn];
int find(int x)
{ return x == p[x] ? x : p[x] = find(p[x]); } void link(int x, int y)
{
int px = find(x), py = find(y);
if(px != py) p[px] = py;
} int main()
{
//freopen("in.txt", "r", stdin); int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
scanf("%s", s); for(int i = ; i <= n; i++) p[i] = i; memset(G, , sizeof(G));
int p = ;
for(int i = ; i <= n; i++)
for(int j = i; j <= n; j++)
{
if(s[p] == '+') G[i-][j]++;
if(s[p] == '-') G[j][i-]++;
if(s[p] == '') link(i-, j);
p++;
}
toposort();
for(p = ; p <= n; p++) if(topo[p] == ) break;
s[] = ; for(int i = p + ; i <= n; i++)
{
int s1 = topo[i], s2 = topo[i-];
if(find(s1) == find(s2)) sum[s1] = sum[s2]; //前缀和相等
else sum[s1] = sum[s2] + ;
}
for(int i = p - ; i >= ; i--)
{
int s1 = topo[i], s2 = topo[i+];
if(find(s1) == find(s2)) sum[s1] = sum[s2];
else sum[s1] = sum[s2] - ;
}
for(int i = ; i <= n; i++)
{
if(i > ) printf(" ");
printf("%d", sum[i] - sum[i - ]);
}
puts("");
} return ;
}

代码君

LA 4255 (拓扑排序 并查集) Guess的更多相关文章

  1. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  3. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  4. Rank of Tetris(hdu1811拓扑排序+并查集)

    题意:关于Rating的信息.这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rati ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. LA4255/UVa1423 Guess 拓扑排序 并查集

    评分稍微有一点过分..不过这个题目确确实实很厉害,对思维训练也非常有帮助. 按照套路,我们把矩阵中的子段和化为前缀和相减的形式.题目就变成了给定一些前缀和之间的大小关系,让你构造一组可行的数据.这个东 ...

  7. hdu 1811 Rank of Tetris - 拓扑排序 - 并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  8. hdu 1811(缩点+拓扑排序+并查集)

    Rank of Tetris Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. hdu1811 Rank of Tetris 拓扑排序+并查集

    这道题是拓扑排序和并查集的综合运用. 由于排行榜是一种从高到低的排序.所以在拓扑排序的时候,如果有一次加入的入度为零的点数大于1,就有变得不确定了(UNCERTAIN). 由于只有一棵树,当树的数量大 ...

随机推荐

  1. CSS 类名的单词连字符:下划线还是连接符?

    本文的部分内容整理自我对此问题的解答: 命名 CSS 的类或 ID 时单词间如何连接? - 知乎 问题 CSS 类或 ID 命名时单词间连接通常有这几种写法: 驼峰式: solutionTitle.s ...

  2. webvector将html转为svg或者png图片的工具

    有些js较多,html组织不好的页面转换起来很不理想,cnblog转换的还不错 http://cssbox.sourceforge.net/webvector/

  3. self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror

    D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...

  4. eclipse编译项目jar,git管理代码

    2015-3-10 settings.xml文件: <localRepository>C:\Users\xxx\.m2\repository</localRepository> ...

  5. iptables使用multiport 添加多个不连续端口 不指定

    iptables使用multiport 添加多个不连续端口   碟舞飞扬 , 01:26 , Linux技术 , 评论(0) , 引用(0) , 阅读(12214) , Via 本站原创 大 | 中  ...

  6. 小圣求职记B:总集篇

    1. 搜狐sohu 搜狐在正式招聘前邀请了部分应聘者到武汉研发中心开座谈会(因此简历尽量早投,机会多些),有研发的也有产品的,40人左右,座谈会期间介绍了搜狐汽车.北京研发中心.武汉研发中心和搜狐媒体 ...

  7. HDU 1789 Doing Homework again (贪心)

    Doing Homework again http://acm.hdu.edu.cn/showproblem.php?pid=1789 Problem Description Ignatius has ...

  8. java基础知识回顾之接口

    /* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该 ...

  9. hdu 4345 Permutation 记忆化搜索

    思路:实际上求的是和小于等于n的质数的种类数!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorit ...

  10. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...