Guess(前缀和+拓扑排序)

题目提交:https://vjudge.z180.cn/problem/UVA-1423

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:
在这里插入图片描述

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

题意:给你一个n*n的符号矩阵,表示从i到j的和,如果i到j所有值的和>0,符号就为+,<0就为-,其余就为0。求,给你一个符号矩阵,让你求满足该符号矩阵的n个数,数的范围为-10到10,可能会有多组n个数符合符号矩阵,输出其中一组即可。

题解:我是利用从1到n的前缀和来建立拓扑排序的图,即箭头指向谁,谁就小,即某个点入度为0就是前缀和值最大,具体详解在代码里。

前缀和的最大值可以定义成10,即cnt=10,也可以比10大,我尝试到cnt=110也可以。

AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=20;
int degree[maxn];//记录每个点的入度
queue<int> q;//把入度为0的入队列
vector<int> mp[maxn];//用来存图,方便输出拓扑排序
int sum[maxn];//用来记录1~n的前缀和
int n;
//初始化
void init()
{
	memset(degree,0,sizeof(degree));
	int i,j,k;
	for(i=0; i<=n; i++)
		mp[i].clear();
}
//入度为0的点就是前缀和值最大的点,因为它入度为0,即没有比它更大的点指向它
//所以它的前缀和值最大
void topsort()
{
	while(!q.empty())
		q.pop();
	int i,j,k;
	int cnt=10;
	//入度相等的两个点的前缀和一定相等
	//因为前缀和相等的两个点,在我们的图里并没有建立边
	for(i=0; i<=n; i++)
	{
		if(degree[i]==0)
		{
			q.push(i);
			sum[i]=cnt;
		}
	}
	while(!q.empty())
	{
		int now=q.front();
		q.pop();
		cnt--;
		for(i=0; i<mp[now].size(); i++)
		{
			int tn=mp[now][i];
			if(--degree[tn]==0)
			{
				q.push(tn);
				sum[tn]=cnt;
			}
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d",&n);
		getchar();
		int i,j,k;
		char ch;
		//利用前缀和来从大到小建立拓扑排序图
		for(i=1; i<=n; i++)
		{
			for(j=i; j<=n; j++)
			{
				scanf("%c",&ch);
				//前缀和为i到j,则,sum[j]-sum[i-1]<0;
				//即sum[i-1]>sum[j]
				//即建立(i-1)-->(j)
				if(ch=='-')
				{
					mp[i-1].push_back(j);
					degree[j]++;
				}
				//前缀和为i到j,则,sum[j]-sum[i-1]>0;
				//即sum[j]>sum[i-1]
				//即建立(j)-->(i-1)
				else if(ch=='+')
				{
					mp[j].push_back(i-1);
					degree[i-1]++;
				}
			}
		}
		topsort();
		//a[i]表示第i个点的值,a[i]=sum[i]-sum[i-1]
		for(i=1; i<=n; i++)
		{
			if(i!=1)
				printf(" ");
			printf("%d",sum[i]-sum[i-1]);
		}
		printf("\n");
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值