UVALive - 4255 Guess
来源:互联网 发布:油画制作软件 编辑:程序博客网 时间:2024/06/11 21:00
题意:对于一个序列,我们可以计算出一个符号矩阵,其中Sij为ai+...+aj的正负号,
现在给你一个矩阵的上三角,求一个满足的序列
思路:如果Sij>0的话,那么代表前缀和差Bj-Bi-1 >0 ,那么Bj > Bi-1,由此我们可以得到一系列的关系,利用toposort排序后,得到一个递增或者递减的序列,就可以求出来各个数了
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 20;int n,degree[MAXN];int map[MAXN][MAXN],s[MAXN],ans[MAXN];char text[1000];void init(){ memset(map,0,sizeof(map)); memset(degree,0,sizeof(degree));}void toposort(){ int count = 0,low = -10; while (count != n+1){ int tag[MAXN]; memset(tag,0,sizeof(tag)); for (int i = 0; i <= n; i++) if (degree[i] == 0){ s[i] = low; degree[i] = -1; tag[i] = 1; count++; } low++; for (int i = 0; i <= n; i++) if (tag[i]){ for (int j = 0; j <= n; j++) if (map[i][j]) degree[j]--; } }}int main(){ int t; scanf("%d",&t); while (t--){ scanf("%d%s",&n,text); init(); int cnt = 0; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++){ char a = text[cnt++]; if (a == '+'){ map[i-1][j] = 1; degree[j]++; } else if (a == '-'){ map[j][i-1] = 1; degree[i-1]++; } } toposort(); for (int i = 1; i <= n; i++) ans[i] = s[i] - s[i-1]; for (int i = 1; i < n; i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } return 0;}
1 0
- UvaLive-4255-Guess
- UVALive - 4255 Guess
- UVALive - 4255 Guess
- UVALive 4255 Guess
- UVALive 4255 Guess
- UVALive 4255 Guess【拓扑排序 】
- UVALive 4255-Guess-拓扑排序
- UValive 4255 Guess(拓扑排序)
- UVALive - 4255 - Guess (拓扑排序)
- UvaLive 4255 Guess(拓扑排序)
- UVALive 4255 Guess(前缀和+拓扑排序)
- UVALive 3664 Guess(贪心+精度)
- LA 4255 Guess
- LA 4255 Guess
- LA 4255(p309)----Guess
- Guess
- guess
- LA 4255 Guess 拓扑排序
- POJ 1195 树状数组 JAVA版
- 计算两个人的出生日期相差的天数
- 学习方向与规划
- 生产者消费者问题
- 一个空格用两个空格替代
- UVALive - 4255 Guess
- Popular Cows
- 【译】测试员,敢问路在何方?来自微软工程师
- mysql数据库优化总结
- sqlserver2008的使用问题
- 第五周:Swing JList 组件
- 嗯~就这么开始了
- C++ Gossip: 巢狀類別(Nested Classes)
- 实现pc端通过usb获取到android手机上的短信、通讯录等信息