Jiamusi Training Day1 Exam

Hey there I go again so long I did not think ah apparently did
not talk much to say on the topic directly

T1

Beautiful numbers

Title Description
Title Background : Duan2baka likes 25 and all multiples of
25. Duan2baka really likes 25. He thinks that there are only 25 and 25 multiples in the world that are beautiful. During the class that day, the teacher wrote on the blackboard. Duan2baka only thinks about 25. He wants to change this series of numbers into multiples of 25, but the teacher told him that he can only exchange two adjacent numbers on this series of numbers. To change this string of numbers into multiples of 25, at least how many times need to be exchanged?
Input
Input an integer n, which represents the series of numbers written by the teacher on the blackboard

Output The
output contains an integer that is the minimum number of exchanges required by Duan2baka to change this string of numbers into a multiple of 25. In particular, if the string of numbers cannot be exchanged into a multiple of 25, then output -1.

Sample input
[sample 1 input]
705
[sample 2 input]
5071
[sample 3 input]
5134689
sample output
[sample 1 output]
1
[sample 2 output]
4
[sample 3 output]
-1
prompt
The final result of the exchange cannot appear leading 0. For example, in example 1, 750 is a legal exchange result, and 075 is an illegal result.
For 20% of the data, n≤1000.
For 50% of the data, n≤1e10.
For 100% data, n≤1e18.

First, this question we should first know all ending in multiples of 25 are 00,255,075
so long to enumerate each case and then take the minimum just fine , however I did wrong honey zz 30 points

#include<cstdio>
#include<cstring>
int minn(int a, int b){
    
    return a<b?a:b;}
char ch[20],str[20];
int len,c0,c5,cn,ans=10000,now,num1,num2;
bool check;
int main()
{
    
    
	gets(ch);
	len=strlen(ch);
	strcpy(str,ch);
	for(int i=0;i<len;i++)
	{
    
    
		if(ch[i]=='0')c0++;
		if(ch[i]=='2'||ch[i]=='7')cn++;
		if(ch[i]=='5')c5++;
	}
	if(c0>=2)
	{
    
    
		for(int i=len-1;~i;i--)
		{
    
    
			if(ch[i]=='0'&&num1<2)
			{
    
    
				if(num1==0)now+=(len-1-i);
				if(num1==1)now+=(len-2-i);
				num1++;
			}
		}
		if(ans>now)ans=now;
		now=num1=0;
	}
	if(c0>=1&&c5>=1)
	{
    
    
		for(int i=len-1;~i;i--)
		{
    
    
			if(ch[i]=='0'&&!num1)
			{
    
    
				num1++;
				now+=(len-1-i);
				for(int j=i;j<len-1;j++)ch[j]=ch[j+1];
				ch[len-1]='0';
			}
			if(ch[i]=='5'&&!num2)
			{
    
    
				if(i==0&&ch[1]=='0')
				{
    
    
					for(int j=2;j<len-1;j++)if(ch[j]!='0')
					{
    
    
						now+=j-1;
						break;
					}
					now+=(len-2-i);
				}
				else if(!num1)now+=(len-1-i);
				else now+=(len-2-i);
				num2++;
			}
		}
		if(ans>now)ans=now;
		num1=num2=now=0;
	}
	if(c5>=1&&cn>=1)
	{
    
    
		strcpy(ch,str);
		for(int i=len;~i;i--)
		{
    
    	
			if((ch[i]=='2'||ch[i]=='7')&&!num1)
			{
    
    
				if(i==0&&ch[1]=='0')
				{
    
    
					for(int j=2;j<len-1;j++)if(ch[j]!='0')
					{
    
    
						now+=j-1;
						break;
					}
					now+=(len-2-i);
				}
				else if(!num2)now+=(len-1-i);
				else now+=(len-2-i);
				num1++;
			}
			if(ch[i]=='5'&&!num2)
			{
    
    
				if(i==0&&ch[1]=='0')
				{
    
    
					for(int j=2;j<len-1;j++)if(ch[j]!='0')
					{
    
    
						now+=j-1;
						break;
					}
				}
				now+=(len-1-i);
				num2++;
			}
		}
		if(ans>now)ans=now;
	}
	if(ans==10000)puts("-1");
	else printf("%d\n",ans);
}

T2

Beautiful array

Topic description
Topic background : msyzsfq wants to be the most powerful
oier before jmsyzsfq becomes the most powerful oier, you must first learn to deal with arrays, and stipulate that two arrays are equivalent if and only if the position of the smallest number in the two arrays is Similarly, jmsyzsfq thinks that two arrays of equal length are beautiful. At this time, Dilhao poses a problem for jmsyzsfq. Given two arrays a and b of length n, ask for a maximum q, from 1 to q Each array in the a array in the interval of is equivalent to the array at the corresponding position in the b array. Now jmsyzsfq can be stumped. Can you give the answer to this question?
Input
Input an integer n, which represents the length of the array. In the
next two lines, input n numbers in each line to represent two arrays of a and b. The input must ensure that the array a or array b is a permutation, that is to say in each array The location of the minimum is determined.
Output The
output contains an integer q. It is required to ensure that each array in the a array in the interval from 1 to q is equivalent to the array at the corresponding position in the b array, giving the largest q.
Sample input
[sample 1 input]
2
1 2
2 1
[sample 2 input]
5
5 3 4 2 1
4 2 3 1 5
Sample output
[sample 1 output]
1
[sample 2 output]
4
Tips
for 30% of the data, n≤10.
For 50% of the data, n≤1000.
For 100% data, n≤100,000.

Use the monotonic stack to maintain the minimum value and judge
PS as a whole every time a number comes in : Warm reminder that this question refers to all the arrays in 1-p instead of the 1-p array, although you read it wrong, it will be smoothly AC

#include<cstdio>
#include<algorithm>
using namespace std;
#define N 1000001
int n,t1,t2,num=1,a[N],b[N],q1[N],q2[N];
inline void read(int &x)
{
    
    
	int s=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
	x=s*w;
}
int main()
{
    
    
    read(n);
    for(int i=1;i<=n;i++)read(a[i]);
    for(int i=1;i<=n;i++)read(b[i]);
    while(num<=n)
    {
    
    
        while(a[q1[t1]]>a[num]) t1--;
        q1[++t1]=num;
        while(b[q2[t2]]>b[num]) t2--;
        q2[++t2]=num;
        if(q1[1]!=q2[1]) break;
        num++;
    }
    printf("%d",num-1);
}

T3

Beautiful number pairs

Title description
Title background : jmsyzsfq likes to play auto chess
as the most popular game mode now. Auto chess deeply attracted jmsyzsfq, so jmsyzsfq tried to design an auto chess game by himself. Each game of auto chess has There will be n players, and there will be m battles. There will be two players in each battle. In particular, each player has his own level. Jmsyzsfq hopes that the two players in each battle will be the same In order to ensure the fairness of the game, Dilhao told him that the player levels of these m matches are not equal. Jmsyzsfq decided to change the levels of some of the players to ensure the balance of the game. He would like to ask you how many player levels need to be modified at least. Can the game be balanced?

Input The
first line contains three integers n, m, representing the number of players and the number of battlefields. The
second line contains n integers, representing the
m lines beloweach player’s level. Each line contains2 integers x, y, representing the x number and Player y will fight to ensure that x is not equal to y.
Output The
output contains an integer for the minimum number of players who need to modify their level.
Sample input
[Sample 1 input]
3 2 3
1 2 3
1 2
2 3
[Sample 2 input]
3 2 2
1 1 2
1 2
2 1
Sample output
[Sample 1 output]
2
[Sample 2 output 】
0
Tips
For 30% of the data, n, m≤10.
For 50% of the data, n,m≤1000.
For 100% of the data, n, m, k ≤ 100,000, to ensure that the player level ≤ n.

And check the set to get all the opponents in the game into one set. The number of modifications in each set is the number of elements in the set minus the element that appears the most times in the set.
I wrote it a little troublesome...

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
inline void read(int &x)
{
    
    
	int s=0,w=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
	x=s*w;
}
int n,m,k,x,y,ans,cnt,tot,now,nowsum,a[100001],arr[100001],com[100001],f[100001],lev[100001];
int find(int x){
    
    return f[x]==x?x:f[x]=find(f[x]);}
void merge(int x, int y)
{
    
    
	x=find(x);y=find(y);
	if(x!=y)f[x]=y;
}
int main()
{
    
    
	read(n);read(m);read(k);
	for(int i=1;i<=n;i++)read(lev[i]);
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=1;i<=m;i++)
	{
    
    
		read(x);read(y);
		merge(x,y);
	}
	for(int i=1;i<=n;i++)arr[f[i]]++;
	for(int i=1;i<=n;i++)if(arr[i]>1)a[++cnt]=i;
	for(int i=1;i<=cnt;i++)
	{
    
    
		memset(com,0,sizeof(com));
		tot=now=nowsum=0;
		for(int j=1;j<=n;j++)if(f[j]==a[i])com[++tot]=lev[j];
		sort(com+1,com+1+tot);
		for(int j=1;j<=tot;j++)
		{
    
    
			if(com[j]!=com[j-1])
			{
    
    
				if(nowsum>now)now=nowsum;
				nowsum=1;
			}
			else nowsum++;
		}
		if(nowsum>now)now=nowsum;
		ans+=tot-now;
	}
	printf("%d\n",ans);
}

This is how Day1's exam questions are! There is no problem, but I seem to have only played 210 with my brain shortcomings...in
a while I will send out today’s exercise questions. If you
have any questions, you can write to the comment area below or add QQ407694747. Let’s discuss it together. Everyone,
please advise!

Guess you like

Origin blog.csdn.net/dhdhdhx/article/details/97406947