J - Billboard Gym - 104397J

Xiaomei is watching a video about the popular song rankings from recent years. When she sees various comments on the screen like "too high", "too low", and "reasonable", her train of thought is disrupted. However, she also believes that the current ranking list is not very reasonable. Therefore, she wants to create a new list based on these comments.

Since there are too many comments, Xiaomei will collect and consolidate a more unified opinion for each song. There are three types of opinions:

  • 1 x1 x: The song ranked x is too high, and it should be lower.
  • -1 x-1 x: The song ranked x is too low, and it should be higher.
  • 0 x0 x: The song ranked x is reasonably ranked, and it should not be changed.

Each song has exactly one consolidated opinion. Xiaomei wants to know if it is possible to create a new list that satisfies all the opinions.

Input

The first line contains an integer T (1≤T≤300), representing the number of test cases.

The first line of each test case contains an integer n (1≤n≤105), representing the number of songs on the list.

Each of the next n lines contains two integers p and x (−1≤p≤1, 1≤x≤n), representing an opinion for the song ranked x. It is guaranteed that each song has exactly one opinion.

It is guaranteed that∑n≤2×105 over all test cases.

Output

For each test case, output n integers in a single line representing the new list of the song board if a satisfactory list can be created, where the i-th integer ai​ indicates that the new rank of the song originally ranked ai​ is i. If there are multiple solutions, output any. If there is no solution, output -1-1 in a single line.

Sample 1

官方题解就是这样说的

对于判断是否有解,我们可以考虑将需要排名上升的歌曲和需要排名向下的歌曲先拎出来。

然后对于排名需要向上的歌曲我们直接考虑贪心,即初始位置最高的对应现在空着的最高的位置,同理处理排名需要向下的歌曲。

得到答案后,我们可以代入之前的要求进行check,如果合法则有解,并且输出此解即可,否则一定无解。

代码是一个学长写的,基本思路和题解相同(还是学长强)

#include
using namespace std;
const int N=1e6+10;
int n,ans[N];
int a[N];
int now[N];
void solve()
{
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    a[i]=0;
    ans[i]=i;
  }
  priority_queue,greater>up,lower;
  for(int i=1;i<=n;i++)
  {
    int x,y;
    cin>>x>>y;
    a[y]=x;
    if(a[y]!=0)
    {
     ans[y]=0;
     if(a[y]==-1)
     up.push(y);
     else 
     lower.push(y);
    }
  }
  bool flag=0;
  for(int i=1;i<=n;i++)
  {
    if(!ans[i])
    {
      if(!up.empty())
      {
        ans[i]=up.top();
        up.pop();
      }
      else 
      {
        ans[i]=lower.top();
        lower.pop();
      }
      if(ans[i]==i)
      {
        flag=1;
        break;
      }
    }
  }
    if(flag)
    cout<<-1<>T;
  //T=1;
  while(T--)solve();
  return 0;
}
Inputcopy Outputcopy
 

2

2

1 1

-1 2

5

0 2

-1 3

-1 4

0 1

-1 5

2 1
-1

你可能感兴趣的:(数学建模)