CF组赛补题-Game Master

nn players are playing a game.

There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map.

You are the game master and want to organize a tournament. There will be a total of n−1n−1 battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament.

In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the number of players.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109, ai≠ajai≠aj for i≠ji≠j), where aiai is the strength of the ii-th player on the first map.

The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤1091≤bi≤109, bi≠bjbi≠bj for i≠ji≠j), where bibi is the strength of the ii-th player on the second map.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print a string of length nn. ii-th character should be "1" if the ii-th player can win the tournament, or "0" otherwise.

题目类型:百思不解题:)

解题知识:

        1)unique()函数

         unique函数的功能是元素去重。即”删除”序列中所有相邻的重复元素(只保留一个),在使用unique函数之前,一般都会将目标序列进行排序

常用形式:

1

iterator unique(iterator it_1,iterator it_2);

其中这两个参数表示对容器中[it_1,it_2)范围的元素进行去重

(注:区间是前闭后开,即不包含it_2所指的元素)

返回值是一个迭代器,它指向的是去重后容器中不重复序列的最后一个元素的下一个元素

unique函数的去重过程实际上就是不停的把后面不重复的元素移到前面来(也可以说是用不重复的元素占领重复元素的位置)

2)C++ lower_bound()函数

        lower_bound() 函数用于在指定区域内查找不小于目标值的第一个元素。也就是说,使用该函数在指定范围内查找某个目标值时,最终查找到的不一定是和目标值相等的元素,还可能是比目标值大的元素。

//在 [first, last) 区域内查找不小于 val 的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                             const T& val);
//在 [first, last) 区域内查找第一个不符合 comp 规则的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                             const T& val, Compare comp);

该函数还会返回一个正向迭代器。当查找成功时,迭代器指向找到的元素;

                                                      反之,如果查找失败,迭代器的指向和 last 迭代器相同。

该函数仅适用于已排好序的序列。

所谓“已排好序”,指的是 [first, last) 区域内所有令 element

解题思路:1)先去重。(记得排序)这也是 我不明白的地方,明明题目说(No two players have the same strength on the same map)

                  2)根据去重后的序列A, 对算出i 在地图A的手下败将有几个,存入p[i].a中

                        根据去重后的序列B, 对算出i 在地图B的手下败将有几个,存入p[i].b中

                3)根据地图A的能力排序,则p[i]关于a非降序。

                      累加能打败的人数,当ans1 == ans2 时,说明

            for(int i =1; i

错误代码:

CF组赛补题-Game Master_第1张图片

#define inf 0x3f3f3f3f
#include 
using namespace std;
const int N = 1e5+10;

int main()
{
	int t ;
	string ans;
	cin>>t;
	while(t -- )
	{
		int n;
		cin>>n;
		int win[n];
		vector v1, v2;
		for(int i = 0; i>temp;
			v1.push_back(temp);
		}
		for(int i = 0; i>temp;
			v2.push_back(temp);
		}
		if(n == 1)
		{
			cout<<1<

AC代码:

#define inf 0x3f3f3f3f
#include 
using namespace std;
const int N = 2e5+10;
typedef long long ll;
struct node
{
	int a, b, inx, w;
	bool operator<(const node&x)const{
		return a < x.a;	
	}
}p[N];
int a[N], b[N];
int A[N], B[N];
bool cmp(node &a, node &b){
	return a.inx < b.inx;
}
int main()
{
	int t ;
	cin>>t;
	while(t -- )
	{
		int n;
		cin>>n;
		for(int i =1; i<= n; i++) p[i].w = 0; 
 		for(int i = 1; i<=n; i++) cin>>a[i],A[i] = a[i];
		for(int i = 1; i<=n; i++) cin>>b[i], B[i] = b[i];
		sort(A+1, A+n+1);
		sort(B+1, B+1+n);
		int index1 = unique(A+1, A+n+1)-A-1;
		int index2 = unique(B+1, B+n+1)-B-1;
		/*用来去重,获取a, b存当前下标为i,能力为a[i]的人能打败的人数*/
		for(int i =1; i<=n;i++)
		{
			p[i].inx = i;
			//lower_bound() 函数用于在指定区域内查找不小于目标值的第一个元素。
			p[i].a = lower_bound(A+1, A+1+index1, a[i])-A; 
			p[i].b = lower_bound(B+1, B+1+index2, b[i])-B;
		}
			sort(p+1, p+1+n);//按地图a能力排序 
			int ans1 = 0, ans2 = 0;
			int f = 1;
			
			for(int i =1; i

你可能感兴趣的:(p2p,蓝桥杯,linq)