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. shiro vue 前后端分离中模拟登录遇到的坑

    系统采用jeeplus框架(ssm+redis+shiro+mongodb+redis),默认是了JSP未做前后端分离,由于业务需要已经多终端使用的需求(H5.小程序等),需要实现前后端分离.但是由于 ...

  2. redis的bitmap

    BitMap是什么 就是通过一个bit位来表示某个元素对应的值或者状态,其中的key就是对应元素本身.我们知道8个bit可以组成一个Byte,所以bitmap本身会极大的节省储存空间. Redis中的 ...

  3. 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

    题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...

  4. 牛客练习赛17-A-长方体

    题目描述 给出共享长方体一个顶点的三个面的面积,求它十二条边的边长和. 输入描述: 一行三个整数a, b, c表示面积(1 <= a, b, c <= 10000). 输出描述: 一行一个 ...

  5. Android内存管理(14)*使用开源库LeakCanary检查内存泄漏

    1.简介 它是一个非常简单好用的内存泄漏检测工具库.可以轻松检测Activity,Fragment的内存泄漏.如果有内存泄漏,它会产生一个通知. 2.资料 官网: https://github.com ...

  6. Git命令add和commit的区别

    要想弄明白git add和git commit的区别,首先我们需要知道三个概念:工作区(Working Directory).版本库(Repository).暂存区(Stage or index). ...

  7. C#随机取部分数据

    1.使用Random伪随机生成器 但是这样会由于转换为数组类型导致性能下降,千万要避免这种用法. 2.使用Take返回重头开始指定数量的连续元素 每次进来这个方法的时候,都使用Guid进行一次排序,然 ...

  8. linux环境下为php7装phpredis扩展

    phpredis在php7.php5下都有不同的版本,装岔了可能会编译报错,所以在安装之前请先看下自己的php是啥版本. 我的redis装的是redis3.2.3版本. 用phpinfo()查看安装的 ...

  9. 自定义 Java Annotation ,读取注解值

    1. 首先是自定义注解: package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import java.lang ...

  10. swift- mutating

    struct Stack<Element> { var items = [Element]() func push(_ item:Element){ self.items.append(i ...