Codeforces Round 102 (Rated for Div. 2) A、B、C题

Insert picture description hereInsert picture description here

Idea: Find the smallest two values ​​in the sequence directly. If the sum of the two is less than or equal to the given d, then the other values ​​in the sequence must be replaced to be less than or equal to d. If the maximum value in the sequence is less than d, then directly Output yes, the others are no
C++ code

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
 
int main()
{
    
    
	int t;
	cin >> t;
	while(t--)
	{
    
    
		int n, d;
		cin >> n >> d;
		int a[105];
		for(int i = 0; i < n; i ++)
			cin >> a[i];
		sort(a,a+n);
		if(a[n-1] <= d)cout << "YES"<< endl;
		else if(a[0] + a[1] <= d) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

Insert picture description here
Idea: Find the smallest cyclic substring of a string. Note: You need to start from the beginning!

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
 
int fun(int a,int b)
{
    
    
	return b ? fun(b, a % b) : a;
}
 
int main()
{
    
    
	int t;
	cin >> t;
	while(t--)
	{
    
    
		string a,b;
		cin >> a >> b;
		string c,d;
		int len1 = a.size();
		int len2 = b.size();
		for(int i = 0; i < a.size(); i ++)  //找最小的整除(循环)子串
		{
    
    
			c+=a[i];
			int flag = 1;
			for(int j = i+1; j < a.size() && flag == 1; j += c.size() )
			{
    
    
				string s(a,j,c.size());
				if(c != s)
					flag = 0;
			}
			if(flag == 1)break;
		}
	//	cout << c;
		for(int i = 0; i < b.size(); i ++)  //与上一样
		{
    
    
			d+=b[i];
			int flag = 1;
			for(int j = i+1; j < b.size() && flag == 1; j += d.size() )
			{
    
    
				string s(b,j,d.size());
				if(d != s)
					flag = 0;
			}
			if(flag == 1)break;
		}
		//cout << c << "--" << d << endl;
		if(c==d)
		{
    
    
			int len3 = c.size();
			int a1 = len1/len3,a2=len2/len3;
			for(int i = 0; i < a1*a2/fun(a1,a2); i ++)
			{
    
    
				cout << c;
			}
			cout << endl;
		}
		else
		{
    
    
			cout << "-1" << endl;
		}
	}
	return 0;
}

Worship the boss code:
Insert picture description here

Here is the link to the original question of C question! ! !
Insert picture description here
Insert picture description here
Title translation:
You have a sequence a with n elements 1, 2, 3,..., k-1, k, k-1, k-2,..., k-1(n−k) (k≤n< 2k).
We call a pair of exponents i<j the reciprocal of a, such that a[i]>a[j].
Suppose you have a permutation p of size k, and you create a sequence b of size n in the following way: b[i]=p[a[i]].
Your goal is to find a permutation p such that the total number of inversions in b does not exceed the total number of inversions in a, and b is the largest in the dictionary.
Tip: If the sequence of k integers contains all integers from 1 to k exactly once, it is called permutation.
Another tip: If s is the prefix of t, or for the first i, si≠ti, si<ti remains the same (in the first position where these sequences are different, the number of s is smaller than t),
then the sequence s is smaller in the dictionary than another sequence t.
Input The
first line contains an integer t (1≤t≤1000)-the number of test cases.
The first and only line of each test case contains two integers n and k (k≤n<2k; 1≤k≤105)-the length of sequence a and its maximum value.
Ensure that the sum of k on the test case does not exceed 105.
Output
For each test case, print k integer-permutation p, which maximizes b lexicographically without increasing the total number of inversions.
It can prove the existence and uniqueness of p.

In the first test case, the sequence a=[1], and there is only one permutation p=[1].
In the second test case, the sequence a=[1,2]. There is no inversion in aa, so there is only one permutation p=[1,2], which does not increase the number of inversions.
In the third test case, a=[1,2,1], there are 11 inversions. If we use p=[2,1], then b=[p[a[1]], p[a[2]], p[a[3]]=[2,1,2], and 1 A reversal.
In the fourth test case, a=[1,2,3,2], since p=[1,3,2], then b=[1,3,2,3]. Both a and b have 1 inversion, and b is the maximum value of lexicography.

#include<bits/stdc++.h>

using namespace std;
const int N = 200;

int main(){
    
    
	int t;
	cin >> t;
	for(int j = 0;j < t;j++){
    
    
		int n,k;
		cin >> n >> k;
		for(int i = 1;i < 2*k-n;i++)   //第一个循环正序输出
			cout << i << " ";
		for(int i = 2*k-n; i <= k; ++i) //需要将数组倒序输出的地方
			cout << k-i+(2*k-n) << " ";
		cout << endl;
	}
    return 0;
}
 
 

Insert picture description here

Guess you like

Origin blog.csdn.net/diviner_s/article/details/112675140