## Codeforces Global Round 15 ABD

Codeforces Global Round 15 ABD

第一次上绿记录

终于打上1200了…
在这里插入图片描述

 AB都是一发过的,D 看了很久但是写不出来,E 也差不多,加油加油图片描述
AB都是一发过的,D 看了很久但是写不出来,E 也差不多,加油加油

A. Subsequence Permutation

A string s of length n, consisting of lowercase letters of the English alphabet, is given.

You must choose some number k between 0 and n. Then, you select k characters of s and permute them however you want. In this process, the positions of the other n−k characters remain unchanged. You have to perform this operation exactly once.

For example, if s=“andrea”, you can choose the k=4 characters “a_d_ea” and permute them into “d_e_aa” so that after the operation the string becomes “dneraa”.

Determine the minimum k so that it is possible to sort s alphabetically (that is, after the operation its characters appear in alphabetical order).

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤40) — the length of the string.

The second line of each test case contains the string s. It is guaranteed that s contains only lowercase letters of the English alphabet.

Output
For each test case, output the minimum k that allows you to obtain a string sorted alphabetically, through the operation described above.

解题思路

题目大意: 给定一个字符串,通过多少次移动,使得字符串满足字典序

翻译一下 对字符串进行排序,对比原始字符串与有序的字符串,逐字符比较,若不同则需要移动,进行计数

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	int t;
	int n;
	
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		char ar[50];
		char br[50];
		getchar();
		for (int i=0;i<n;i++)
		{
			scanf("%c",ar+i);
		}
		
		strcpy(br,ar);
		//puts(br);
		sort(br,br+n);
		//puts(br);
		int cnt=0;//进行计数
		for (int i=0;i<n;i++)
		{
			if(ar[i]!=br[i])
			{
				cnt++;
			}
		}
		cout<<cnt<<endl;
		
	}
	return 0;
}

B. Running for Gold

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The Olympic Games have just started and Federico is eager to watch the marathon race.

There will be n athletes, numbered from 1 to n, competing in the marathon, and all of them have taken part in 5 important marathons, numbered from 1 to 5, in the past. For each 1≤i≤n and 1≤j≤5, Federico remembers that athlete i ranked ri,j-th in marathon j (e.g., r2,4=3 means that athlete 2 was third in marathon 4).

Federico considers athlete x superior to athlete y if athlete x ranked better than athlete y in at least 3 past marathons, i.e., rx,j<ry,j for at least 3 distinct values of j.

Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes.

Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤50000) — the number of athletes.

Then n lines follow, each describing the ranking positions of one athlete.

The i-th of these lines contains the 5 integers ri,1,ri,2,ri,3,ri,4,ri,5 (1≤ri,j≤50000) — the ranking positions of athlete i in the past 5 marathons. It is guaranteed that, in each of the 5 past marathons, the n athletes have distinct ranking positions, i.e., for each 1≤j≤5, the n values r1,j,r2,j,…,rn,j are distinct.

It is guaranteed that the sum of n over all test cases does not exceed 50000.

Output
For each test case, print a single integer — the number of an athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes). If there are no such athletes, print −1. If there is more than such one athlete, print any of them.

解题思路

题目大意
每个选手参加五项比赛,如果选手A有任意三项的成绩优于选手B 则选手A 比B更为优秀
athlete x superior to athlete y if athlete x ranked better than athlete y in at least 3 past marathons, i.e., rx,j<ry,j for at least 3 distinct values of j.

题目要求,是否存在一位最强的选手,比每一位都更为优秀

Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete.

思路
先遍历所有人,找到一个"优秀的"选手,并记住他的位置
再次遍历,如果出现一个更优秀的选手,则不存在.通过第一次遍历,我们找到一位"优秀的"选手,如果又有一位,则说明之前那位选手不是最优秀的,即没有超越所有人
倘若,不存在更优秀的选手,则说明之前找到那位即为最优秀的运动员,输出他的序号.

代码

在这里插入代码```
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
	int t;
	int ar[50005][5]={0};
	
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int pos=0;
		
		scanf("%d",&n);
		for (int i=0;i<n;i++)
		{
			for (int j=0;j<5;j++)
			{
				scanf("%d",&ar[i][j]);
			}
		}
		
		
		int cnt=0;
		if(n==1)//如果只有一名运动员,那他肯定是最优秀的
		{
			cout<<1<<endl;
			continue;
		}
		
		else
		{
			for (int i=1;i<n;i++)
			{
				cnt=0;
				for (int j=0;j<5;j++)
				{
					if(ar[pos][j]>ar[i][j])
					{
						cnt++;
					}
				}
				if(cnt>2)//三场比赛以上才称为优秀
				{
					pos=i;
				}
			}
			
		}
		
		
		int mark=0;
		//再次循环
		for (int i=0;i<n;i++)
		{
			cnt=0;
			for (int j=0;j<5;j++)
			{
				if(ar[pos][j]>ar[i][j])
				{
					cnt++;
				}
			}
			
			if(cnt>2)
			{
				mark++;
			}
			
			
		}
		if(mark)
		{
			cout<<-1<<endl;
		}
		
		else
		{
			cout<<pos+1<<endl;
		}
		
		//int ar[n][5]={0};
		
		
	}
	return 0;
}


```片

D. Array Differentiation

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a sequence of n integers a1,a2,…,an.

Does there exist a sequence of n integers b1,b2,…,bn such that the following property holds?

For each 1≤i≤n, there exist two (not necessarily distinct) indices j and k (1≤j,k≤n) such that ai=bj−bk.
Input
The first line contains a single integer t (1≤t≤20) — the number of test cases. Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤10).

The second line of each test case contains the n integers a1,…,an (−105≤ai≤105).

Output
For each test case, output a line containing YES if a sequence b1,…,bn satisfying the required property exists, and NO otherwise.

解题思路

题目大意
给一个数组长度为n 的 数组 A, 判断是否存在长度也为n 的数组B,满足
A 中的每一个元素,为B中两个元素的组合
For each 1≤i≤n, there exist two (not necessarily distinct) indices j and k (1≤j,k≤n) such that ai=bj−bk.
注意 j 可以 等于 k ,同时对 j k 的顺序没有要求 **
思路
由于j k 的顺序不影响结果, 即A 中的
负数**, 可以通过改变顺序变化为正数 故将所有数取为正数
如果 A 中存在 **0 或者 元素组合的结果为 0 ** 则可以被表示

例如 数组A 1 2 -1 由于 1+(-1) =0
必定存在 0 1 2 的数组B ,
1 = 0 +1
-1 = 0 - 1
2 = 0 +2
满足题意
将 - 1 1 改为 x -x ,证明上面的表达式恒成立 **
x -x a(a!=x , a!=-x , a!=0, x!=0)

** x = 0 + x ; -x = 0 - x ; a = 0 + a

代码

声明 代码是看过cf 官方题解后写的,比赛的时候,没有写出来
[我不知道如何去处理进行过 加法减法运算或者不进行运算(被忽略) 的数字]



#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

void solve()
{
	int n;
	scanf("%d",&n);
	int ar[n]={0};
	int mark=1;
	
	for (int i=0;i<n;i++)
	{
		mark*=3;
		scanf("%d",&ar[i]);
		ar[i]=abs(ar[i]);
	}
	//mark 最后会变成 pow(3,n) 即指数形式
	
	
	for (int i=1;i<mark;i++)
	{
		int k=i;
		int sum=0;
		for (int j=0;j<n;j++)
		{
		//表示 运算过程的关键
		//如果 s=0 则不计算,忽略这个数字
		// s=1  则进行加法运算
		//s=2  改为-1  进行减法运算
		//特别巧妙
			int s=k%3;
			k/=3;
			if(s==2)s=-1;
			
			sum+=s*ar[j];
			
		}
		
		if(!sum)
		{
			cout<<"YES\n";
			return ;
		}
		
	}
	cout<<"NO\n";
	return ;
} 

int main()
{
	int t;
	scanf("%d",&t);
	
	while(t--)
	{
		solve();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值