Pre competition simulation of computer science popularization group ා1 (10-26)

Pre competition simulation of computer science popularization group ා1 (10-26)

A. mountain climbing

DD wants to climb the mountain now. She defines the fatigue degree from the first mountain to the first mountain as | a[i]-a[i+1] |. Now, given | n | mountain, DD is on the first mountain in the initial state. She wants to know the maximum single fatigue degree in the whole climbing process.

Input format

The first line gives an integer representation of n

The second line is n integers, and the second line is ai.

Output format

What is the maximum output single fatigue

Data range

For 30% data, 2 ≤ n ≤ 5000

For another 20% of the data, ensure that all ai's are the same

For data of 100%, 2 ≤ n ≤ 500000, 1 ≤ ai ≤ 1e9

The extra space at the end of each line does not affect the correctness of the answer.

sample input

6
1 100 2 200 3 300

sample output

297

Solution: only output the maximum value of single fatigue. So we set X, y to separate the input of a1, and then for each subsequent input, (x is the value of the current input, y is the value of the last input), compare their differences, and select the maximum value. The code is as follows:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define sf(n) scanf("%d",&n)
#define pf(n) printf("%d\n",n)
inline int mx(int a,int b){
    return a>b?a:b;
}
int n,x,sum=-1,y,t;
int main()
{
	sf(n);
	sf(x);
	y=x;
	rep(i,2,n){
		sf(x);
		if(y>x)t=y-x;
		else t=x-y;
		y=x;
		sum=mx(sum,t);//If the fatigue value is larger than the previous maximum value, update the fatigue value.
	}
	pf(sum);
	return 0;
}

B. paper

Now, given l,r, DD, I get r-l+1 pieces of paper, which say l,l+1,l+2...r-1,r, now I connect them in order, and I see that they become l(l+1)(l+2)...(r-1)r, for example, l=2,r=6, and then I get 23456. DD now wants to know if this number is a multiple of 9.

Input format

An integer TT in the first row indicates the number of data groups

Next T lines, two integers in each line represent l,r

Output format

For each query, if it is a multiple of 9, output Y, otherwise output N

Data range

For 30% data, 1 ≤ l ≤ r ≤ 10

For 50% data, 1 ≤ l ≤ r ≤ 106

For 100% data, 1 ≤ T ≤ 10000, 1 ≤ l ≤ r ≤ 1e12

The extra space at the end of each line does not affect the correctness of the answer.

sample input

3
2 4
2 5
2 7

sample output

Y
N
Y

Solution idea: let the number l(l+1)(l+2)...(r-1)r be a multiple of 9, and judge the definite time-out one by one. But we can use the multiple of 9 to simplify, and the relationship between l and R is continuous, which gives another operation space. For the multiple of 9, there is a property similar to the multiple of 3. If the sum of the digits of a number x is a multiple of 9, then x is a multiple of 9; if x=n*9, then for x + 1 + X + 2 + X + 3 +.... x + 8 = Y, (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) = 36 = 9 * 4, it is not difficult to deduce that the sum of eight digits between two multiples of 9 must also be a multiple of 9. Therefore, the problem becomes to find the most from l to R. Is the sum of the digits of the multiples of the decimal 9 and the digits of the multiples of the maximum 9 from R to (l to R) a multiple of 9? If yes, output Y; otherwise, output N. It should be noted that the situation with (r-l) and (r < 9) should be treated differently. The code is as follows:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define sf(n) scanf("%d",&n)
LL l,r,sum,num;
int main()
{
	int T;
	sf(T);
	while(T--){
		scanf("%lld%lld",&l,&r);
		sum=0;//Initializing the value of the digit sum
        if(l%9==0&&r%9==0)sum=9;//If l and R are multiples of 9, then l (L + 1)... (R-1) r must be multiples of 9.
        else if(r<=9||r-l<9){//Special cases, listed separately
            for(LL i=l;i<=r;i++){
                LL j=i;
                while(j>0){
                    sum+=j%10;
                    j/=10;
                }
            }
        }
        else {
            for(LL i=l;i<r;i++){//Figure out the sum of the digits between l and the minimum multiple of 9
                if(i%9==0)break;
                LL j=i;
                while(j>0){
                    sum+=j%10;
                    j/=10;
				}
            }
            for(LL i=r;r>l;i--){//Figure out the sum of the digits between r and the multiples of 9.
                if(i%9==0)break;
                LL j=i;
                while(j>0){
                    sum+=j%10;
                    j/=10;
				}
            }
        }
		if(sum%9==0)cout<<"Y\n";
		else cout<<"N\n";
	}
	return 0;
}

c. color

Now, given a grid graph of n*m, col[i][j] is a non-zero number if there is color in the grid. If there is no color at this point, col[i][j] is 0. Now DD wants to know how many colors he can encounter if he can only move on the adjacent grids with color.

Input format

In the first line, two integers represent n,m respectively.

Next n lines, m integers for each line, col[i][j]

Output format

How many colors can output DD touch at most

Data range

For 30% data, 1 ≤ n,m ≤ 50

For another 20%, 1 ≤ coli,j ≤ 1e6

For 100% data, 1 ≤ n,m ≤ 1000, 1 ≤ coli,j ≤ 1e9

The extra space at the end of each line does not affect the correctness of the answer.

sample input

5 5
1 2 3 4 0
0 0 0 0 7
1 2 2 1 0
0 0 0 3 4
6 0 0 5 0

sample output

5

Solution idea: for a direct search question, after inputting, traverse the map without finding a colored position, then traverse based on the position, and set mp[i][j] to 0 for each point in the process of traversal (assuming that two positions are connected, only one point can be walked, and the results of other points are the same); but what is required in the question is to be able to encounter at most How many kinds of colors, so you can't directly use the counter + +; if you carefully observe, you can find that the non-zero number in the map is a kind of color. If you can traverse to a colored point every time, take it out, and finally count the number again and compare the size, can't you get the most encountered color? The set in stl can play well. The code is as follows:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> ii;
#define inf 1e9
#define F first
#define S second
#define dbg(x) cout<<#x<<" value : "<<x<<"\n";
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define sf(n) scanf("%d",&n)
#define stf(n,m) scanf("%d%d",&n,&m)
#define pf(n) printf("%d\n",n)
#define slf(n) scanf("%lld",&n)
#define plf(n) printf("%lld\n",n)
priority_queue<int,vector<int>,greater<int> > q;
const int N=1e3+10;
const int mod=1e9+7;
int n,m,a[N][N],num;
set<int> w;
int dx[4][2]={1,0,-1,0,0,1,0,-1};//Moving array
inline bool txt(int x,int y){//Judge whether it is out of line
	return x>0&&x<=n&&y>0&&y<=m;
}
inline int mx(int x,int y){
	return x>y?x:y;
}
void dfs(int x,int y){
	a[x][y]=0;//Every time you visit a location, set it to no color
	for(int i=0;i<4;i++){
		int nx=x+dx[i][0];
		int ny=y+dx[i][1];
		if(txt(nx,ny)){
			if(a[nx][ny]!=0){//If the location has color
                w.insert(a[nx][ny]);//Add color number to set
				dfs(nx,ny);//Make a new move from this location
			}
		}
	}
}
void slove(){
	rep(i,1,n){
		rep(j,1,m){
			if(a[i][j]==0)continue;
			else {
				w.insert(a[i][j]);
				dfs(i,j);
				num=mx(w.size(),num);//Decide whether to update the value of num according to the type of color encountered
                w.clear();//Clear set after a traverse
			}
		}
	}
	cout<<num<<endl;
}
int main()
{
	stf(n,m);
	rep(i,1,n){
		rep(j,1,m){
			sf(a[i][j]);
		}
	}
	slove();
	return 0;
}

 

Posted by discomatt on Sat, 26 Oct 2019 05:02:54 -0700