UVALive 4255 Guess
这题竟然是图论···orz
题意:给出一个整数序列a1,a2,……,可以得到如下矩阵
1 2 3 4
1 - + 0 +
2 + + +
3 - -
4 +
“-”表示从ai到aj的和是负数,“+”表示和是正数,“0”表示和是0。现给出矩阵,求序列,每个整数的绝对值不超过10。
解法:转化为前缀和问题,矩阵可以表示前缀和的关系,用b表示前缀和,例如“-”表示b[j]-b[i-1]<0,即b[j]<b[i-1]。通过矩阵得到所有前缀和之间的关系,通过拓扑排序解出所有前缀和(任意一组解)。记录每个前缀和比它大的前缀和的个数,和比它小的前缀和都有哪些,找到比自己大的前缀和的个数为0的赋值为10(前缀和最大值),将比它小的前缀和的比它们大的前缀和数减一,如此循环,下次赋值为9。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define ll long long
using namespace std;
char sign[15][15];
int bigger[15];//比自己大的前缀和个数
vector<int> v[15];//比自己小的前缀和们
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(sign,'\0',sizeof(sign));
memset(bigger,0,sizeof(bigger));
for(int i=0; i<15; i++)
v[i].clear();
int n;
int b[15]= {0};
scanf("%d",&n);
char str[200];
scanf("%s",str);
int k=0;
for(int i=1; i<=n; i++)
for(int j=i; j<=n; j++)
{
sign[i][j]=str[k++];
if(sign[i][j]=='-')
{
bigger[j]++;
v[i-1].push_back(j);
}
else if(sign[i][j]=='+')
{
bigger[i-1]++;
v[j].push_back(i-1);
}
}
int sum=10;
while(1)
{
queue<int> q;
int flag=1;
for(int i=0; i<=n; i++)
{
if(bigger[i]==0)
{
bigger[i]--;
flag=0;
b[i]=sum;
q.push(i);
}
}
if(flag)
break;
else
{
while(!q.empty())
{
int i=q.front();
q.pop();
for(int j=0; j<v[i].size(); j++)
bigger[v[i][j]]--;
}
sum--;
}
}
for(int i=1; i<=n; i++)
{
if(i!=1)
cout<<" ";
cout<<b[i]-b[i-1];
}
cout<<endl;
}
return 0;
}
代码好丑···
UVALive 4255 Guess的更多相关文章
- uvalive 4255 Guess(拓扑排序)
算好题目,反正我没想到可以用图论做(虽然现在做的是图论专题= =) 首先是要把求每个位置上的值转化为求 “前缀和之差”,这是一个很有用的技巧 其次,由输入的(n+(n-1)+...+2+1)个符号,可 ...
- 【拓扑排序或差分约束】Guess UVALive - 4255
题目链接:https://cn.vjudge.net/contest/209473#problem/B 题目大意:对于n个数字,给出sum[j]-sum[i](sum表示前缀和)的符号(正负零),求一 ...
- UVALive - 4255 - Guess (拓扑排序)
Guess 题目传送:Guess 白书例题 注意拓扑排序时,,入度同一时候为0的前缀和须要赋值为同一个数(这个数能够随机取.由于前缀和是累加的,每个a的数值都仅仅和前缀和之差有关).,由于此时能够看成 ...
- D - Guess UVALive - 4255 拓扑排序
Given a sequence of integers, a1, a2, . . . , an, we define its sign matrix S such that, for 1 ≤ i ≤ ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
随机推荐
- Centos安装桌面环境
刚开始装系统的时候,没有选Gnome或者KDE,现在想装个玩玩. 简单的安装可以参考这个:http://huruxing159.iteye.com/blog/744750 centos安装是是使用li ...
- lintcode:Unique Characters 判断字符串是否没有重复字符
题目: 判断字符串是否没有重复字符 实现一个算法确定字符串中的字符是否均唯一出现 样例 给出"abc",返回 true 给出"aab",返回 false 挑战 ...
- python 类成员函数
http://cowboy.1988.blog.163.com/blog/static/75105798201091141521583/ 这篇文章总结的非常好 主要注意的地方是 1,在类内调用成员函数 ...
- eclipse 中忽略jsp, xml文件中的报错信息
有的时候, 在eclipse中, jsp, xml 文件时运行的好好的, 可是就是在eclipse中报错, 虽然不影响功能, 但看起来很烦, 去掉这些错误警告的方法是: Windows-Prefere ...
- IO多路复用的几种实现机制的分析
http://blog.csdn.net/zhang_shuai_2011/article/details/7675797 select,poll,epoll都是IO多路复用的机制.所谓I/O多路复用 ...
- Java解压上传zip或rar文件,并解压遍历文件中的html的路径
1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...
- bash: ./device/nexell/tools/build.sh: 权限不够
/bin/bash: build/tools/diff_package_overlays.py: 鏉冮檺涓嶅 i686-linux-gcc: error trying to exec 'cc1': ...
- IOS键盘收起
1.点击Return按扭时收起键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignF ...
- C#实现GDI+基本图的缩放、拖拽、移动
C#实现GDI+基本图的缩放.拖拽.移动示例代码如下: using System;using System.Collections.Generic;using System.ComponentMode ...
- HDU 4686 Arc of Dream(矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 思路: #include <iostream>#include <cs ...