Codeforces Round #894 (Div.3)

insert image description here


foreword

I played a game of Codeforces Div3 last night. Alas, I played very badly. I only ACped two thinking questions.

insert image description here


A. Gift Carpet

topic:

Link: A. Gift Carpet
Recently, Tema and Vika celebrated a family day. Their friend Arina gave them a rug that can be expressed as n⋅m in
the lowercase Latin alphabet.

Vika hasn't seen the gift yet, but Tema knows what kind of rug she likes. Vika would love the rug if she could read her name. She reads column by column from left to right and selects one or more or zero letters from the current column.

Formally, if four distinct columns can be selected in order from left to right, e.g. the first column contains "v", the second column contains "i", the third column contains "k", and the fourth column contains "A", girls will love the rug.

Help Tema find out in advance whether Vika will like Elena's gift.

enter:

Each test consists of multiple test cases. The first line of input contains a single integer t
(1≤t≤100) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers n,m
(1≤n, m≤20) — the dimensions of the carpet.

The next n lines contain m each lowercase latin letter, describing the given rug.

output:

For each set of input data, output "YES" if Vika likes carpets, and "NO" otherwise.
You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" would be accepted as positive answers.
insert image description here

Ideas:

Control the for loop exchange of rows and columns, and then look for them one by one, find k++ and then jump to the next column.
Finally, if k==4is output yes, otherwise it is output no.

tip: Oh, keep going! ! !

code:

#include<iostream>
using namespace std;
int main()
{
    
    
	int t;
	cin>>t;
	while(t--)
	{
    
    
		int n,m;
		cin>>n>>m;
		char s[22][22];
		for(int i=0;i<n;i++)
		{
    
    
			for(int j=0;j<m;j++)
			{
    
    
				cin>>s[i][j];	
			}
		}
		int k=0;
		for(int i=k;i<m;i++)
		{
    
    
			for(int j=0;j<n;j++)
			{
    
    
				if(s[j][i]=='v')
				{
    
    
					if(k==0) 
					{
    
    
						k++;
						break;
					}
				}
				if(s[j][i]=='i')
				{
    
    
					if(k==1)
					{
    
    
						k++;
						break;
					}
				}
				if(s[j][i]=='k')
				{
    
    
					if(k==2)
					{
    
    
						k++;
						break;
					}
				}
				if(s[j][i]=='a')
				{
    
    
					if(k==3)
					{
    
    
						k++;
						break;
					}
				}
			}
		}
		if(k==4) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

B. Sequence Game

topic:

链接:B. Sequence Game
Tema and Vika are playing the following game.

First, Vika comes up with a sequence of positive integers a
of length m
and writes it down on a piece of paper. Then she takes a new piece of paper and writes down the sequence b
according to the following rule:

First, she writes down a1
Then, she writes down only those ai (2≤i≤m) such that ai−1≤ai
Let the length of this sequence be denoted as n

For example, from the sequence a=[4,3,2,6,3,3]
Vika will obtain the sequence b=[4,6,3]

She then gives the piece of paper with the sequence b
to Tema. He, in turn, tries to guess the sequence a

Tema considers winning in such a game highly unlikely, but still wants to find at least one sequence a
that could have been originally chosen by Vika. Help him and output any such sequence.

Note that the length of the sequence you output should not exceed the input sequence length by more than two times.

roughly means:

It is to input an array b with a size of n and guess the numbers in the array a.
The basis for the guess is:

  • First write out a[1];
  • Then if a[i-1]<a[i], store it in the b array

The question is also to let you start from b—>a, and use b to launch the array of a

enter:

Each test consists of multiple test cases. The first line of input data contains a single integer t (1≤t≤104) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the sequence b

The second line of each test case contains n
integers b1,b2,b3,…,bn (1≤bi≤10^9) — the elements of the sequence.

The sum of the values of n over all test cases does not exceed 2⋅10^5

output:

For each test case, output two lines. In the first line, output a single integer m
— the length of the sequence (n≤m≤2⋅n). In the second line, output m integers a1,a2,a3,…,am (1≤ai≤109) — the assumed sequence that Vika could have written on the first piece of paper.

If there are multiple suitable sequences, you can output any of them.

insert image description here

Ideas:

emm can barely be said to be a double-pointer algorithm. It is to open a result array to store the content to be added, and then set a m=0 pointer to add data.

The core point is when encountering descending order: directly add a number 1 to solve all problems, that's it...

code:

#include<iostream>
using namespace std;
 
int n,a[200005],m,b[400005];

int main()
{
    
    
	int t;
	scanf("%d\n",&t);
	while(t--)
	{
    
    
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i];
		m=0;
		b[++m]=a[1];
		for(int i=2;i<=n;i++)
		{
    
    
			if(a[i]<a[i-1]) b[++m]=1;
			b[++m]=a[i];
		}
		printf("%d\n",m);
		for(int i=1;i<=m;i++) printf("%d ",b[i]);
		puts("");
	}
	return 0;
}

C. Flower City Fence

Link: C. Flower City Fence

topic:

Anya lives in the Flower City. By order of the city mayor, she has to build a fence for herself.
The fence consists of nplanks, each with a height of aimeters. According to the order, the heights of the planks must not increase. In other words, it is true that ai≥aj for all i<j.

Anya became curious whether her fence is symmetrical with respect to the diagonal. In other words, will she get the same fence if she lays all the planks horizontally in the same order.

For example, for n=5, a=[5,4,3,2,1], the fence is symmetrical. Because if all the planks are laid horizontally, the fence will be [5,4,3,2,1], as shown in the diagram.

insert image description here
But for n=3, a=[4,2,1], the fence is not symmetrical. Because if all the planks are laid horizontally, the fence will be [3,2,1,1], as shown in the diagram.

insert image description here
Help Anya and determine whether her fence is symmetrical.

enter:

The first line of the input contains an integer t (1≤t≤104) — the number of test cases.

The description of the test cases follows.

The first line of a test case contains a single integer n (1≤n≤2⋅105) — the length of the fence.

The second line of a test case contains n integers a1≥a2≥a3≥⋯≥an (1≤ai≤109) — the heights of the planks.

The sum of the values of n for all test cases does not exceed 2⋅105.

output:

For each test case, output “YES” if the fence is symmetrical, otherwise output “NO”.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes” and “YES” will be accepted as a positive answer.

insert image description here

Ideas:

The main idea of ​​the topic is to give you an array to abstract the array into a cuboid and combine it together, and then cut it from the diagonal to see if it is symmetrical about the cutting line. If it is symmetrical, output YES, otherwise output NO

Algorithm label: Thinking questions

In fact, this is the way to solve the idea a[a[i]]. In fact, a[a[i]]it perfectly solves the problem of symmetry. It is really good. Think about it carefully, right? ? ?
It is indeed the case.

code:

#include<stdio.h>

int a[200005];

void solve()
{
    
    
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	
	for(int i=1;i<=n;++i)
	{
    
    
		if(a[i]>n || a[a[i]]<i) 
		{
    
    
			puts("No");
			return;
		}
	}
	puts("Yes");
	return;
}
int main()
{
    
    
	int t;
	scanf("%d",&t);
	
	while(t--) solve();
	
	return 0;
}

D. Ice Cream Balls

topic:

链接:D. Ice Cream Balls
Tema decided to improve his ice cream making skills. He has already learned how to make ice cream in a cone using exactly two balls.

Before his ice cream obsession, Tema was interested in mathematics. Therefore, he is curious about the minimum number of balls he needs to have in order to make exactly ndifferent types of ice cream.

There are plenty possible ice cream flavours: 1,2,3,…. Tema can make two-balls ice cream with any flavours (probably the same).

Two ice creams are considered different if their sets of ball flavours are different. For example, {1,2}={2,1}, but {1,1}≠{1,2}.

For example, having the following ice cream balls: {1,1,2}, Tema can make only two types of ice cream: {1,1} and {1,2}

Note, that Tema do not need to make all the ice cream cones at the same time. This means that he making ice cream cones independently. Also in order to make a following cone {x,x} for some x, Tema needs at least 2 balls of type x

Help Tema answer this question. It can be shown that answer always exist.

enter:

Each test consists of multiple test cases. The first line of input contains a single integer t (1≤t≤104) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer n (1≤n≤1018) — the number of ice cream types that Tema wants to make.

output:

For each test case, output a single integer — the minimum number of balls Tema needs to buy.
insert image description here

Ideas:

It can be abstracted into a binary tree, that is, an ice cream must have a combination of two numbers, and then I was confused hahaha, looking at the code of the boss, the boss should have summed up the rules and then written them out using mathematical knowledge. Anyway, I am too good at it sorry guys

code:

#include<iostream>
#include<cmath>

using namespace std;

int a[200005];

void solve()
{
    
    
	long long n;
	cin>>n;
	
	long long p=(1+sqrt(n*8+1))/2;
	cout<<p+n-p*(p-1)/2<<endl;
	
	return;
}

int main()
{
    
    
	long long t;
	cin>>t;
	
	while(t--) solve(); 
	
	return 0;
}

E. Kolya and Movie Theatre

Link: E. Kolya and Movie Theater

topic:

Recently, Kolya found out that a new movie theatre is going to be opened in his city soon, which will show a new movie every day for n days. So, on the day with the number 1≤i≤n, the movie theatre will show the premiere of the i-th movie. Also, Kolya found out the schedule of the movies and assigned the entertainment value to each movie, denoted by ai

However, the longer Kolya stays without visiting a movie theatre, the larger the decrease in entertainment value of the next movie. That decrease is equivalent to d⋅cnt, where d is a predetermined value and cnt is the number of days since the last visit to the movie theatre. It is also known that Kolya managed to visit another movietheatre a day before the new one opened — the day with the number 0. So if we visit the movie theatre the first time on the day with the number i, then cnt — the number of days since the last visit to the movie theatre will be equal to i

For example, if d=2 and a=[3,2,5,4,6], then by visiting movies with indices 1 and 3, cnt value for the day 1 will be equal to 1−0=1 and cnt value for the day 3 will be 3−1=2, so the total entertainment value of the movies will be a1−d⋅1+a3−d⋅2=3−2⋅1+5−2⋅2=2.

Unfortunately, Kolya only has time to visit at most m movies. Help him create a plan to visit the cinema in such a way that the total entertainment value of all the movies he visits is maximized.

enter:

Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers n, m, and d (1≤n≤2⋅105, 1≤m≤n, 1≤d≤109).

The second line of each set of input data contains n integers a1,a2,…,an (−109≤ai≤109) — the entertainment values of the movies.

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

output:

For each test case, output a single integer — the maximum total entertainment value that Kolya can get.

insert image description here

Ideas:

Algorithm Label: Greedy Algorithm

Use the usage of priority queue (priority_queue) to write code, knowledge points of priority queue: usage of priority_queue<int, vector<int>, greater<int>> and priority<int> of priority queue in C++

Mainly use the priority queue to store elements greater than 0, then every m data except the first data can be stored in the record array, and finally traverse to find the most valuable

code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
 
int n,m,d,a[200005],b[200005];
 
void solve()
{
    
    
	priority_queue<int,vector<int>,greater<int> >q;
	cin>>n>>m>>d;
	for(int i=1;i<=n;i++) cin>>a[i];
	int s=0,ans=0;
	for(int i=1;i<=n;i++)
	{
    
    
		if(a[i]>0)
		{
    
    
			s+=a[i];
			q.push(a[i]);
			while(q.size()>m) s-=q.top(),q.pop();
		}
		b[i]=s;
	}
	for(int i=1;i<=n;i++) ans=max(ans,b[i]-d*i);
	cout<<ans<<endl;
}
 
signed main()
{
    
    
	int t;
	scanf("%lld\n",&t);
	while(t--) solve();
	return 0;
}

Summarize

The second time I played CF Div3 compared with the freshman winter vacation, I wrote one more thinking question, which is a bit of progress. I woke up this morning after exercising and started to fill in the questions. I can reach the "peach" Take it off for learning, although it was very difficult to play, but the feeling after growing up is really good. Slowly improve and play slowly, there will always be a day when you will become stronger, come on Natsume Asahi.

insert image description here

Guess you like

Origin blog.csdn.net/congfen214/article/details/132486836