JG-OJ Record-Seventh online competition: 20-21(2) 0th online competition (IQ rehabilitation training)

* 1: Sign-in question-5
* 2: Interval union
* 3: Special 01 string
* 4: Common factor-2
* 5: WSAD
* 6: Contribution value of the array

1: Sign-in question-5

Description
Jiageng College has carried out a two-week online teaching activity, where attendance is a very important indicator.

Every time n students should come to participate in an online course, they have their own unique student ID to sign in.

But there is always a classmate who overslept, that is, he did not sign in, and was punished (reward) for the photo shoot. Can you help the teacher find out who is so lucky?

Enter
only one set of cases.

The first line is a positive integer n, indicating that n students should participate in this course. (2 <= n <= 1e5)

The second line is n different positive integers xi, which respectively represent the unique student numbers of these n students. (1 <= xi <= 1e9)

The third line is n-1 positive integers that are different from each other, which represents the student ID of n-1 students who have signed in given by the teacher. (Guaranteed to be the student ID of the classmate who should come for this class)

Output
Output a positive integer, indicating that the student ID of the student has not been checked in, and then wrap the line.

Sample input
4

2 3 4 8

8 3 4

Sample output
2


The student number given by the HINT teacher and the student number that should be signed in may not be in order.

Source
20-21 (2) 0th online match

Solution:
1. (The simplest) (from Dadao Tutu) The sum of the first input-the sum of the second input.
2. (From IST) The key-value student number of the map container is not repeated> set> s. erase> output
(too difficult to elaborate) (I can’t-confidently)
3. Sort two one-dimensional arrays, The first one that does not match the number is the one that did not arrive (code below).

#include<iostream>
#include<algorithm>
//algorithm-sort
using namespace std;
int main()
{
    
    
	int n;
	cin>>n;
	int *a=new int[n]();
	int *b=new int[n-1]();
	for(int i=0;i<n;i++)
	{
    
    
		cin>>a[i];
	}
	sort(a,a+n-1);
	for(int i=0;i<n-1;i++)
	{
    
    
		cin>>b[i];
	}
	sort(b,b+n-2);
	bool sc=0;
	for(int i=0;i<n-1;i++)
	{
    
    
		if(sc==0)
		{
    
    
			if(a[i]!=b[i])
			{
    
    
				cout<<a[i]<<endl;
				sc=1;
				break;
			}
		}
	}
	if(sc==0) cout<<a[n-1]<<endl;
	delete []a;
	delete []b;
	return 0;
}

2: Interval union

Description
There are n intervals: a1, a2,..., an.

求 a1 ∪ a2 ∪ a3 ∪ …… ∪ an。

Enter
only one set of cases.

The positive integer n in the first line represents the number of intervals. (1 <= n <= 1e5)

Then there are n rows, each row contains two positive integers L and R, which represent the left and right end points of an interval, namely [L, R]. (0 <= L <= R <= 1e10)

Output
According to the size of the left end point, output each interval after the union from small to large.

The left end and right end of each interval are separated by a space, and a new line is required after each output.

Sample input
6

1 2

1 4

8 10

8 9

5 6

6 7

Sample output
1 4

5 7

8 10

HINT
[1, 2] interval and [1, 4] interval take the union and become [1, 4]

[5, 6] interval and [6, 7] interval take the union and become [5, 7]

The [8, 9] interval and the [8, 10] interval are merged into [8, 10]

Source
20-21 (2) 0th online match

Solution:
1. The structure stores each input L and R (left and right), and then sorts them. See compare for the sorting rules. Then record the first L and R as l and r, and output when L is greater than the recorded r. Stored l, r, store new L, R, if the following L is not greater than the recorded r, merge the collection, take the smallest left end of the two collections (stored l, r and this time L, R) as l and The largest right end is r. (Code below)

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long int ll;
struct qj
{
    
    
	ll L;
	ll R;
};
qj s[100005];
bool compare(qj a,qj b)
{
    
    
	if(a.L==b.L)
	{
    
    
		return a.R<b.R;
	}
    return a.L<b.L;
}
int main()
{
    
    
	ll n;
	cin>>n;
	for(ll i=1;i<=n;i++)
	{
    
    
		cin>>s[i].L>>s[i].R;
	}
	//cout<<"opening"<<endl; 
	sort(s,s+n+1,compare);
	ll l=0,r=0;
	for(ll i=1;i<=n;i++)
	{
    
    
		if(i==1)
		{
    
    
			l=s[i].L;
			r=s[i].R;
		}
		else
		{
    
    
			if(s[i].L>r)
			{
    
    
				cout<<l<<" "<<r<<endl;
				l=s[i].L;
				r=s[i].R;
			}
			else
			{
    
    
				if(s[i].R>r)
				{
    
    
					r=s[i].R;
				}
			}
		}
	}
	cout<<l<<" "<<r<<endl;
	return 0;
}

3: Special 01 string

Description
We define a string to be a special 01 string:

1. The empty string is a special 01 string.
2. Only composed of characters 0 and 1, and not all two adjacent characters are 1.

Now please construct a special 01 string of length n. How many can you construct?

Input The
first line is a positive integer T representing the number of test cases. (1 <= T <= 1e5)

Each set of cases is an integer n, which represents the length of the constructed string. (0 <= n <= 1e6)

Output For
each set of cases, output how many you can construct, and then wrap.

Since the answer may be large, you only need to output the result of modulo 998244353.

Sample input
1
3

Sample output
5


The special 01 string with a HINT length of 3 can be 100 010 001 101 000.

Source
20-21 (2) 0th online match

Solution:
1. Find the law and store it in an array (code below).

#include<iostream>
#include<cstring>
#define mod 998244353
using namespace std;
int ans[1000005];
int main()
{
    
    
	memset(ans,0,sizeof(ans));
	ans[0]=1;ans[1]=2;ans[2]=3;ans[3]=5;
	for(int i=4;i<=1000005;i++)
	{
    
    
		ans[i]=(ans[i-1]+ans[i-2])%mod;
	}
	int T;
	cin>>T;
	for(int f=1;f<=T;f++)
	{
    
    
		int n;
		cin>>n;
		cout<<ans[n]<<endl;
	}
	return 0; 
}

4: Common factor -2

Description
There are n numbers, find their common prime factors.
Enter
only one set of cases.

The first line is a positive integer n representing the number of digits. (1 <= n <= 1e5)

Then there are n positive integers, for each positive integer x there is 1 <= x <= 1e5.

Output Output
the common prime factors of these n numbers in order from small to large, separated by a space between every two numbers, and there is no space after the last number.

If they do not have a common prime factor, output No.

Finally wrap.

Sample input
3

6 12 18

Sample output
2 3

HINT
Source
20-21 (2) 0th online match

Solution:
1. The sieve method sifts out the prime numbers below 1e5, and after sorting the given numbers, it loops to judge whether each prime number is a common factor (code below).
2. (No need for sieve method) (from CST boss) Sort from small to large-take a[0] to find a prime number less than or equal to it-get the prime number to judge whether it is a common factor-yes-output.

#include<iostream>
#include<cstring>
using namespace std;
const int N=100005;
int p[N];
int prime[N];
int cnt=0;
void xxs()
{
    
    
	for (int i = 2; i <= N; i++)
	{
    
    
	if(p[i] == 0)
	{
    
    
		p[i]=i;
		prime[cnt++]=i;
	}
	for (int j = 0;j<cnt;j++)
	{
    
    
		if (prime[j]>p[i]||prime[j]*i>N)
		{
    
    
			break;
		}
		p[prime[j]*i]=prime[j];
	}
	}
}
int main()
{
    
    
	bool sc=0;
	int sz[100005];
	memset(sz,0,sizeof(sz));
	xxs();
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>sz[i];
	}
	for(int i=0;i<cnt;i++)
	{
    
    
		for(int j=1;j<=n;j++)
		{
    
    
			if(sz[j]%prime[i]!=0) break;
			else if(j==n)
			{
    
    
				if(sc==0)
				{
    
    
					cout<<prime[i];
					sc=1;
				}
				else cout<<" "<<prime[i];
			}
		}
	}
	if(sc==0) cout<<"No";
	cout<<endl;
	return 0;
} 

5: WSAD

Description
Luo Shao is playing a game. Use WSAD to control the characters to move up, down, left, and right. We can think of the map as a two-dimensional plane. Luo Shao initially stands at the origin. Now he has operated it n times. Please output his position.

Input The
first line is a positive integer n representing the number of test cases. (1 <= n <= 100)

Each set of cases is a string containing only the characters WSAD, t representing Luo Shao's operation. (1 <= length(t) <= 100)

Output For
each group of cases, output the horizontal and vertical coordinates of Luo Shao's position after the operation, separated by a space, and then wrap.

Sample input
2

WWW

SSD

Sample output
0 3

1 -2

HINT
Source
20-21 (2) 0th online match

Solution:
1. Circulate to judge each character of the string (code below).
2. (From the CST boss) Use count to calculate the number of WASD, and calculate directly.

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    
    
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		int x=0,y=0;
		string s;
		cin>>s;
		int lg=s.size();
		for(int j=0;j<=lg;j++)
		{
    
    
			if(s[j]=='W') y++;
			if(s[j]=='S') y--;
			if(s[j]=='A') x--;
			if(s[j]=='D') x++;
		}
		cout<<x<<" "<<y<<endl;
	}
	return 0;
}

6: Contribution value of the array

Describe Luo Shao often brushing questions, and that day he saw another very interesting question.

Given an array of length n, define the contribution of the array as the position where each number first appears in the array * the sum of the number of times each number appears (the same number is counted only once).

For example: 1 2 2 3 1, the first occurrence of the number 1 is 1, and a total of 2 occurrences, so provide the contribution value of 1 * 2 = 2, the number 2 is 2 * 2 = 4, the number 3 is 4 * 1 = 4, so the contribution value of this array is 2 + 4 + 4 = 10.

But obviously this is not difficult, so there is a condition attached, you can change the position of the number in the array arbitrarily, and ask what is the maximum contribution value of the array after the change?

Input The
first line is a positive integer T representing the number of test cases. (1 <= T <= 10)

Each group of cases contains a positive integer n, which represents the length of the array. (1 <= n <= 100000)

Then there are n integers ai. (|ai| <= 10000)

Output For
each group of cases, output the maximum array contribution value that can be obtained after changing the number position, and then wrap the line.

Sample input
2
4
1 2 1 2
4
1 1 2 2

Sample output
8
8

HINT
Note: The array can also remain unchanged.

For example 1: 1 2 1 2

The contribution value of the array before the change is 1 * 2 + 2 * 2 = 6

You can change it to 1 1 2 2 or 2 2 1 1 to get the contribution value 8 of the larger array.

Source
20-21 (2) 0th online match

Solution:
None (too difficult QWQ)

Guess you like

Origin blog.csdn.net/Fei_WuYan/article/details/114450933