Given a sequence of integers, a1, a2, . . . , an, we define its sign matrix S such that, for 1 ≤ i ≤ j ≤ n, Sij = “ + ” if ai + . . . + aj > 0; Sij = “ − ” if ai + . . . + aj < 0; and Sij = “0” otherwise. For example, if (a1, a2, a3, a4) = (−1, 5, −4, 2), then its sign matrix S is a 4 × 4 matrix: 1 2 3 4 1 − + 0 + 2 + + + 3 − − 4 + We say that the sequence (−1, 5, −4, 2) generates the sign matrix. A sign matrix is valid if it can be generated by a sequence of integers. Given a sequence of integers, it is easy to compute its sign matrix. This problem is about the opposite direction: Given a valid sign matrix, find a sequence of integers that generates the sign matrix. Note that two or more different sequences of integers can generate the same sign matrix. For example, the sequence (−2, 5, −3, 1) generates the same sign matrix as the sequence (−1, 5, −4, 2). Write a program that, given a valid sign matrix, can find a sequence of integers that generates the sign matrix. You may assume that every integer in a sequence is between −10 and 10, both inclusive. Input The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case consists of two lines. The first line contains an integer n (1 ≤ n ≤ 10), where n is the length of a sequence of integers. The second line contains a string of n(n + 1)/2 characters such that the first n characters correspond to the first row of the sign matrix, the next n − 1 characters to the second row, . . ., and the last character to the n-th row. Output For each test case, output exactly one line containing a sequence of n integers which generates the sign matrix. If more than one sequence generates the sign matrix, you may output any one of them. Every integer in the sequence must be between −10 and 10, both inclusive. Sample Input 3 4 -+0++++--+ 2 +++ 5 ++0+-+-+--+-+-- Sample Output -2 5 -3 1 3 4 1 2 -3 4 -5

拓扑排序:把sum(i,j)考虑成pre[j] - pre[i-1]的形式,然后可以建立图的关系,进行拓扑排序,从大到小,每一个拓扑序后面的点都比前面小,所以每当在拓扑排序里遇到该点(说明当前值比它大),都将遇到的点的sum 减一

注意i前缀序列是n+1项,【0,n】

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define MAXN 14
#define N 100
int in[MAXN], g[MAXN][MAXN];
int sum[MAXN];
char str[N];
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
memset(g, , sizeof(g));
memset(in, , sizeof(in));
memset(sum, , sizeof(sum));
scanf("%d", &n);
scanf("%s", str);
int pos = ;
for (int i = ; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
char c;
c = str[pos++];
if (c == '+')
{
g[j][i-] = ;
in[i - ]++;
}
else if(c == '-')
{
g[i - ][j] = ;
in[j]++;
}
}
}
queue<int> q;
while (!q.empty()) q.pop();
for (int i = ; i <= n; i++)
if (!in[i])
q.push(i);
while (!q.empty())
{
int tmp = q.front();
q.pop();
for (int i = ; i <= n; i++)//必须从0开始
{
if (g[tmp][i] == )
{
sum[i]--;
if (--in[i] == )
q.push(i);
}
}
}
for (int i = ; i <= n; i++)
{
printf("%d", sum[i] - sum[i - ]);
if (i != n)printf(" ");
else printf("\n");
}
}
}

D - Guess UVALive - 4255 拓扑排序的更多相关文章

  1. LA 4255 (拓扑排序 并查集) Guess

    设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...

  2. uvalive 4255 Guess(拓扑排序)

    算好题目,反正我没想到可以用图论做(虽然现在做的是图论专题= =) 首先是要把求每个位置上的值转化为求 “前缀和之差”,这是一个很有用的技巧 其次,由输入的(n+(n-1)+...+2+1)个符号,可 ...

  3. 【拓扑排序或差分约束】Guess UVALive - 4255

    题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...

  4. UVALive - 4255 - Guess (拓扑排序)

    Guess 题目传送:Guess 白书例题 注意拓扑排序时,,入度同一时候为0的前缀和须要赋值为同一个数(这个数能够随机取.由于前缀和是累加的,每个a的数值都仅仅和前缀和之差有关).,由于此时能够看成 ...

  5. UVALive 6264 Conservation --拓扑排序

    题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...

  6. UVALive 6467 Strahler Order 拓扑排序

    这题是今天下午BNU SUMMER TRAINING的C题 是队友给的解题思路,用拓扑排序然后就可以了 最后是3A 其中两次RE竟然是因为: scanf("%d",mm); ORZ ...

  7. uvalive 6393(uva 1572) Self-Assembly 拓扑排序

    题意: 给出一些正方形,这些正方形的每一条边都有一个标号.这些标号有两种形式:1.一个大写字母+一个加减号(如:A+, B-, A-......), 2.两个0(如:00):这些正方形能够任意翻转和旋 ...

  8. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

  9. 有向无环图的应用—AOV网 和 拓扑排序

    有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...

随机推荐

  1. BST二叉查找树转双向链表DoubleLinke

    问题:在不创建任何新的节点的情况下,实现将一颗BST变成有序的双向链表. 分析: 在结构上,如图的一颗BST,每个节点都有left right指针分别指指向左右儿子.结构上和双向链表节点是完全相同的. ...

  2. encodeURIComponent的用法

    实践出真知,项目中遇到坑,填满后总结:编码不一定需要解码 rsa加密字段(base64位后),通过url?filed=value传输后,总是有+等特殊字符,然后到后端时base64解不开,发现很多空格 ...

  3. [转]iOS WebKit browsers and auto-zooming form controls

    问题描述:https://github.com/jquery/jquery-mobile/issues/2581 本文转自:http://www.456bereastreet.com/archive/ ...

  4. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  5. AndroidStudio3.0 Canary 8注解报错Annotation processors must be explicitly declared now.

    体验最新版AndroidStudio3. Canary 8的时候,发现之前项目的butter knife报错,用到注解的应该都会报错 Error:Execution failed for task ' ...

  6. taskctl命令行类(sh、exe、python新增scp)插件升级扩展

    转载自: http://www.taskctl.com/forum/detail_129.html 上次写了一个帖子 TASKCTL中不使用代理,通过ssh免密连接执行远程脚本配置(SSH插件扩展)h ...

  7. 防止按钮button重复提交,点击后失效,10秒后恢复

    <script type="text/javascript"> $(function () {//页面完全加载完后执行 /*防止重复提交 10秒后恢复*/ var is ...

  8. (转)WKT转换工具terraformers

    http://blog.csdn.net/gisshixisheng/article/details/53150111 概述: 前面的文章中,提到了Arcgis中实现wkt转换为geometry,但是 ...

  9. R语言学习 - 热图美化

    实际应用中,异常值的出现会毁掉一张热图.这通常不是我们想要的.为了更好的可视化效果,需要对数据做些预处理,主要有对数转换,Z-score转换,抹去异常值,非线性颜色等方式. 对数转换 为了方便描述,假 ...

  10. C# HttpWebRequest Post Get 请求数据

    Post请求 1 //data 2 string cookieStr = "Cookie信息"; 3 string postData = string.Format("u ...