Jiamusi Training Day1 Examination

Keywords: Programming less

Hey hey, did I miss me for so long? Obviously not.
Say little and go straight to the point.

T1

Beautiful numbers

Topic Description
Topic Background: Duan 2baka likes 25 and multiples all 25
Duan 2baka really likes 25. He thinks that only 25 and 25 multiples in the world are beautiful. This day in class, the teacher wrote a series of numbers on the blackboard. Duan 2baka only thought about 25. He wanted to turn the series of numbers into 25 multiples, but the teacher told him that he could only exchange the adjacent numbers at a time. Two numbers, Duan2baka wants to know, if you change these numbers into multiples of 25, how many times do you need to swap them at least?
input
Enter an integer n to represent the number the teacher wrote on the blackboard.

output
The output contains the minimum number of times that an integer Duan2baka needs to swap to turn the number into a multiple of 25. In particular, if the number cannot be swapped to a multiple of 25, the 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
Tips
The final result of exchange can not appear leading 0. For example, example 1, 750 is the result of legal exchange, while 075 is the result of illegal exchange.
For 20% of the data, n is less than 1000.
For 50% of the data, n < 1e10.
For 100% data, n < 1e18.

First of all, we should know that the end of all 25 multiples is 00, 25, 50, 75.
So just enumerate each case and then take the minimum. However, I missed 30 points in zz for honey juice.

#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
Background: msyzsfq wants to be the most powerful oier
Before jmsyzsfq becomes the strongest oier, it is necessary to learn how to deal with arrays, specifying that two arrays are equal if and only if the position of the smallest number in the two arrays is the same. jmsyzsfq considers that two arrays of equal length are beautiful. Dilhao gives jmsyzsfq a difficult problem when two arrays of equal length are given a and b. Ask for a maximum q, so that every array in a array in the interval from 1 to q is equivalent to the array of corresponding positions in b array. Now jmsyzsfq can be stuck. Can you give the answer to this question?
input
Enter an integer n to represent the length of the array.
Next two lines, each line enters n digits, representing two arrays a and b. The input guarantees that the array a or b is an arrangement, that is to say, the position of the minimum value in each array is determined.
output
The output contains an integer q, which requires that every array of a in the interval from 1 to Q be equal to the array of corresponding positions in the b array, giving the maximum 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 is less than 1000.
For 100% of the data, n is less than 100,000.

Maintain the minimum value with monotone stack and judge the total number of entries
PS: Warm Tip This topic is about all the arrays in 1-p instead of 1-p, although you will see the wrong AC smoothly.

#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

Graceful pairs

Topic Description
Topic Background: jmsyzsfq likes to play go-go very much
As the hottest game mode now, self-paced chess attracts jmsyzsfq deeply. So jmsyzsfq tries to design a self-paced chess game by himself. There are n players in each game of self-paced chess game, and there will be m matches. There will be two players in each game. In particular, each player belongs to himself. Level, jmsyzsfq hopes that the two players in each game will have the same level to ensure fair play, but Dilhao tells him that the level of players in this m-game is not equal. jmsyzsfq decides to change the level of some of the players to ensure the balance of the game. He wants to ask you how many levels you need to change at least. Can the game be balanced?

input
The first line consists of three integers n,m, representing the number of players and the number of battlefields.
The second line contains n integers, representing the level of each player.
The following m lines, with two integers x and Y in each line, indicate that players of X and y will fight to ensure that x is not equal to y.
output
The output contains an integer of the minimum number of players that need to be modified.
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 are less than 100,000, guaranteeing player level is less than n.

The number of modifications in each set is the number of elements in the set minus the number of elements that appear most frequently in the set.
I'm writing a bit of trouble.

#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);
}

That's the exam question for Day1! It's no problem, but I just hit 210 as if I had a brain defect.
I'll send out today's exercises in a moment.
If you have any questions, please write them in the comments section below or add QQ407694747 for discussion.
All kinds of gods and gentlemen, please give more advice!

Posted by AbydosGater on Fri, 26 Jul 2019 04:39:35 -0700