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_ ...
随机推荐
- C#操作config文件
以下是app.config或web.config的定义,定义了一个参数,键为Isinit,值为false <?xml version="1.0"?> <confi ...
- pl/sql tutorial
http://plsql-tutorial.com/plsql-procedures.htm What is PL/SQL? PL/SQL stands for Procedural Language ...
- Chp10: Scalability and Memory Limits
The Step-by-Step Approach break down a tricky problem and to solve problems using what you do know. ...
- ActiveMQ 学习笔记
http://somebody-hjh.iteye.com/blog/726050 一.概述 Message,即消息.人与人之间通过消息传递信息.言语.眼神.肢体动作都可被视为消息体.当然还有我们经常 ...
- AndroidManifest.xml介绍一
下面是AndroidManifest.xml的简单介绍,直接上图! 一.manifest结点的属性介绍 二.application结点属性介绍 三.activity.intent-filter.use ...
- Delphi 发展历史
自然人的软件著作权,保护期为自然人终生及其died后50年:软件是合作开发的,截止于最后died的自然人died后第50年的12月31日.法人或者其他组织的软件著作权,保护期为软件首次发表之后50年, ...
- Dictionary<Key,Value>的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#四种文件流的区别(转)
1.FileStream类的读写操作 FileStream类可以对任意类型的文件进行读取操作,而且我们也可以按照需要指定每一次读取字节长度,以此减少内存的消耗,提高读取效率. 代码实例: //创建文件 ...
- 模拟在table中移动鼠标,高亮显示鼠标所在行
在项目中有这样一个需求,在table中移动鼠标时,鼠标所在行高亮显示,其他行正常显示,为此做了一个模拟. 具体代码如下: <!DOCTYPE html> <html xmlns=&q ...
- NSDate & NSDateFormatter
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasep ...