acwing game 24

Posted by gonsman on Thu, 10 Feb 2022 16:50:50 +0100

acwing game 24

This week's competition has three topics: simulation, enumeration and simulation

acwing 4070. XOR

Link: 4070. XOR - AcWing question bank

Title Description

Problem solving ideas

Very simple simulation, record the maximum value and the last input when entering, and don't even open the array

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int n,maxx,a;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a);
		maxx=max(maxx,a);
	}
	a^=maxx;//a here is the last element
	printf("%d",a);
	return 0;
}

acwing 4071. Chess

Link: 4071. Chess - AcWing question bank

Title Description

Problem solving ideas

There are cars and horses on the chessboard a. ask how to put horses b

List all the squares that can't put horses, and subtract them from the total number of squares

There are three situations in the grid where horses cannot be put: the whole row and column where the car is located and the grid where horse a itself is located, the grid where horse a can jump, and the grid where horse b can eat the car after it is put down

The first case is easy to deal with, and it is not difficult to think of two or three cases: take the horse a and the car as the starting point, the horse takes the day as one step, and the grid that can be reached in the next step is the grid that cannot be put

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int fx[8]={2,1,-1,-2,-2,-1,1,2};
int fy[8]={1,2,2,1,-1,-2,-2,-1};
int ans=16;//The grid that the car can walk to is 15 grid, plus the grid of horse 1 itself
int c1,c2;//c is the coordinates of the car
int m1,m2;//m is the coordinate of horse a
bool vis[10][10];
int main(){
	char a[2],b[2];
	scanf("%s",&a);
	scanf("%s",&b);
	c1=a[0]-'a';
	c2=a[1]-'0'-1;
	m1=b[0]-'a';
	m2=b[1]-'0'-1;
	for(int i=0;i<8;i++) vis[c1][i]=1;//The row and column where the vehicle is located cannot be placed
	for(int i=0;i<8;i++) vis[i][c2]=1;
	vis[m1][m2]=1;//The lattice of the horse itself
	for(int i=0;i<8;i++){
        //Calculate the grid that horse a can reach
		int tox=m1+fx[i];
		int toy=m2+fy[i];//The horse takes the day as a step
		if(tox>=0&&tox<8&&toy>=0&&toy<8&&!vis[tox][toy]) {ans++;vis[tox][toy]=1;}//The coordinates reached must be on the chessboard and the point has not been calculated
        //Calculate the grid where you can eat the car
		tox=c1+fx[i];
		toy=c2+fy[i];
		if(tox>=0&&tox<8&&toy>=0&&toy<8&&!vis[tox][toy]) {ans++;vis[tox][toy]=1;}
	}
	ans=64-ans;//Total number of cells minus the number of cells that cannot be placed
	printf("%d",ans);
	return 0;
}

acwing 4072.

Link: 4072. Exercise book - AcWing question bank

Title Description

Problem solving ideas

The total number of knowledge points in the topic is only 3, and each exercise book has only 1-2 knowledge points. Each inquiry will contain only one knowledge point

So a good idea is to use three vector arrays to record the exercise books containing three knowledge points of a, b and c, sort them from small to large according to the price, output one after asking and pop out one

Then the question comes: an exercise book has two knowledge points a and b. if the pop of array A is lost, what about b?

Then let's use a vis array to record which book is bought. Use struct to record the id and cost of each exercise book. The vector array is sorted according to the cost size. The smallest cost is output each time. If the array is empty and can't be bought, it will output - 1

The main calculation is sorting, so the time complexity is O(nlogn)

matters needing attention

The title ensures that the price of each exercise book is different, so there is no better problem to buy one at the same price

It should be noted that the knowledge points contained in each exercise book are input twice and may be the same. Remember not to repeatedly push them into the exercise book array

There is also an optimization, vector pop_ The back operation is faster than erase, so it's best to sort from high to low according to the price of the exercise book and read from the end of the array, so that the code is easy to write and the time is fast

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
const int N=2e5+10;
struct node{
	int id;
	int cost;
};
vector<node> a,b,c;//An array of exercise books containing three knowledge points a, B and C
int n,m;
int p[N],a1[N],b1;//p is the price of each exercise book, a1 is the first knowledge point contained in each exercise book, and b1 is the second knowledge point
bool vis[N];//Record whether the exercise book has been bought
bool cmp(node x,node y){
	if(x.cost>y.cost) return true;//Sort by size
	return false;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&p[i]);//cost
	for(int i=1;i<=n;i++){
		scanf("%d",&a1[i]);//Read in the first point of knowledge
		node temp;
		temp.id=i;
		temp.cost=p[i];
        //Enter the array according to the contained knowledge points
		if(a1[i]==1){
			a.push_back(temp);
		}
		else if(a1[i]==2){
			b.push_back(temp);
		}
		else{
			c.push_back(temp);
		}
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&b1);
		if(b1==a1[i]) continue;//If the second knowledge point is the same as the first, continue directly
		node temp;
		temp.id=i;
		temp.cost=p[i];
		if(b1==1){
			a.push_back(temp);
		}
		else if(b1==2){
			b.push_back(temp);
		}
		else{
			c.push_back(temp);
		}
	}
	sort(a.begin(),a.end(),cmp);
	sort(b.begin(),b.end(),cmp);
	sort(c.begin(),c.end(),cmp);
	scanf("%d",&m);
	for(int i=1;i<=m;i++){
		int op;//Ask for knowledge
		scanf("%d",&op);
		bool flag=0;
		if(op==1){
			for(int j=a.size()-1;j>=0;j--){
				node temp=a[j];
				a.pop_back();
				if(!vis[temp.id]){
					printf("%d ",temp.cost);
					vis[temp.id]=1;
					flag=1;
					break;
				}
			}
		}
		else if(op==2){
			for(int j=b.size()-1;j>=0;j--){
				node temp=b[j];
				b.pop_back();
				if(!vis[temp.id]){
					printf("%d ",temp.cost);
					vis[temp.id]=1;
					flag=1;
					break;
				}
			}
		}
		else{
			for(int j=c.size()-1;j>=0;j--){
				node temp=c[j];
				c.pop_back();
				if(!vis[temp.id]){
					printf("%d ",temp.cost);
					vis[temp.id]=1;
					flag=1;
					break;
				}
			}
		}
		if(flag==0) printf("-1 ");//There is no exercise book to buy, output - 1
	}
	return 0;
}

j–){
node temp=c[j];
c.pop_back();
if(!vis[temp.id]){
printf("%d ",temp.cost);
vis[temp.id]=1;
flag=1;
break;
}
}
}
if(flag==0) printf("- 1"); / / no exercise book to buy, output - 1
}
return 0;
}

Topics: C C++ Algorithm Simulation