The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple (Mirror)(EFGHIJk)

E

Sequence in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB


DreamGrid has just found an integer sequence in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.

What's the minimum number of operations needed to make the sequence non-decreasing?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer ( ), indicating the length of the sequence.

The second line contains integers ( ), indicating the given sequence.

It's guaranteed that the sum of of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
4
1 3 2 4
5
2 3 3 5 5

Sample Output

2
0

Hint

For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.

For the second sample test case, as the sequence is already sorted, no operation is needed.

一层for循环,将递增的放到一边,阻止递增放到另一边。阻止递增肯定都要向前移动的,ans+=阻止递增的个数。找出阻止递增数中最大的。在递增的数里二分或者暴力找到那些。小于这个数也都要向前移动。ans+=num2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2,mod=1000000007 ;
const ll inf=1e18;
const int N=1e5+10;
int a[N],n,vis[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
		int num=0,top=a[1],mn=0;
		for(int i=2;i<=n;i++)
		{
			if(a[i]>=top) top=a[i];
			else
			{
				vis[i]=1;
				num++;
				mn=max(mn,a[i]);
			}
		}
		int id=1;
		int num2=0;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			if(a[i]<mn) 
			{
				num2++;
				continue;
			}
			else
			{
				id=i;
				break;
			}
		}
		printf("%d\n",num+num2); 
	}
}

F题

Abbreviation


Time Limit: 1 Second      Memory Limit: 65536 KB


In the Test of English as a Foreign Language (TOEFL), the listening part is very important but also very hard for most students since it is usually quite hard for them to remember the whole passage. To help themselves memorize the content, students can write down some necessary details. However, it is not easy to write down the complete word because of its length. That's why we decide to use the abbreviation to express the whole word.

It is very easy to get the abbreviation, all we have to do is to keep the consonant letters and erase the vowel. In the English alphabet, we regard 'a', 'e', 'i', 'y', 'o', 'u' as the vowels and the other letters as the consonants. For example, "subconscious" will be expressed as "sbcnscs".

However, there is one exception: if the vowel appears as the first letter, they should be kept instead of thrown away. For example, "oipotato" should be expressed as "optt".

Since you have learned how to use the abbreviation method, it's time for some exercises. We now present you words in total, it's your task to express them in their abbreviation form.

Input

There are multiple test cases. The first line of the input contains an integer (about 100), indicating the number of test cases. For each test case:

The only line contains a string ( ) consisting of lowercase English letters, indicating the word which needs to be abbreviated.

Output

For each test case output one line containing one string, which is the correct abbreviation of the given word.

Sample Input

5
subconscious
oipotato
word
symbol
apple

Sample Output

sbcnscs
optt
wrd
smbl
appl

签到题

#include<bits/stdc++.h>
using namespace std;
const int N=200;
char s[N];
char a[10]={'a', 'e', 'i', 'y', 'o', 'u' };
int vis[30];
int main()
{
	int t;
	cin>>t;
	for(int i=0;i<6;i++) vis[a[i]-'a']=1;/*
	vis[a-'a']=1;
	vis[e-'a']=1;
	vis[i-'a']=1;
	vis[y-'a']=1;
	vis[o-'a']=1;
	vis[u-'a']=1;*/
	while(t--)
	{
		cin>>s+1;
		int len=strlen(s+1);
		for(int i=1;i<=len;i++)
		{
			if(i==1)
			{
				printf("%c",s[i]);continue;
			}
			if(vis[s[i]-'a']) continue;
			printf("%c",s[i]);
		}
		puts("");
	}
}

Lucky 7 in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao loves number 7 but hates number 4, so he refers to an integer as a "lucky integer" if is divisible by 7 but not divisible by 4. For example, 7, 14 and 21 are lucky integers, but 1, 4 and 28 are not.

Today BaoBao has just found an integer in his left pocket. As BaoBao dislikes large integers, he decides to find a lucky integer such that and is as small as possible. Please help BaoBao calculate the value of .

Input

There are multiple test cases. The first line of the input is an integer (about 100), indicating the number of test cases. For each test case:

The first and only line contains an integer ( ), indicating the integer in BaoBao's left pocket.

Output

For each test case output one line containing one integer, indicating the value of .

Sample Input

4
1
7
20
28

Sample Output

7
7
21
35

构造大于n,最小的能被7整除,不能被4整除的数

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		if(n%7==0)
		{
			if(n%4==0) printf("%d\n",n+7);
			else printf("%d\n",n);
			continue;
		}
		int ans=(n/7+1)*7;
		if(ans%4==0) ans+=7;
		printf("%d\n",ans);
	}
}

Singing Everywhere


Time Limit: 1 Second      Memory Limit: 65536 KB


Baobao loves singing very much and he really enjoys a game called Singing Everywhere, which allows players to sing and scores the players according to their performance.

Consider the song performed by Baobao as an integer sequence , where indicates the -th note in the song. We say a note is a "voice crack" if , and . The more voice cracks BaoBao sings, the lower score he gets.

To get a higher score, BaoBao decides to delete at most one note in the song. What's the minimum number of times BaoBao sings a voice crack after this operation?

Input

There are multiple test cases. The first line of the input contains an integer (about 100), indicating the number of test cases. For each test case:

The first line contains one integer ( ), indicating the length of the song.

The second line contains integers ( ), indicating the song performed by BaoBao.

It's guaranteed that at most 5 test cases have .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

3
6
1 1 4 5 1 4
7
1 9 1 9 8 1 0
10
2 1 4 7 4 8 3 6 4 7

Sample Output

1
0
2

Hint

For the first sample test case, BaoBao does not need to delete a note. Because if he deletes no note, he will sing 1 voice crack (the 4th note), and no matter which note he deletes, he will also always sing 1 voice crack.

For the second sample test case, BaoBao can delete the 3rd note, and no voice cracks will be performed. Yay!

For the third sample test case, BaoBao can delete the 4th note, so that only 2 voice cracks will be performed (4 8 3 and 3 6 4).

题意:若a[i-1]<a[i]>a[i+1]则a[i]属于破音,问去掉一个数后最少的破音数是多少

一层for循环枚举去掉的点,该点只能影响相邻的左边的和右边的点。更新一下破音数就可以了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2,mod=1000000007 ;
const ll inf=1e18;
const int N=1e5+10;
ll a[N];
int n,vis[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%d",&n);
		int n1=0;
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
		a[0]=inf;
		a[n+1]=inf;
		for(int i=2;i<n;i++)
			if(a[i]>a[i-1]&&a[i]>a[i+1] ) vis[i]=1,n1++;
			
		int ans=n1,n2=n1;
		for(int i=1;i<=n;i++)
		{
			n1=n2;
			if(i==1)
			{
				if(vis[2]) n1--;
			}
			else if(i==n)
			{
				if(vis[n-1]) n1--;
			}
			else
			{
				if(vis[i]) n1--;
				if(vis[i-1])
				{
					if(a[i-1]>a[i-2]&&a[i-1]>a[i+1])n1++,n1--;
					else n1--;
				}
				else 
				{
					if(a[i-1]>a[i-2]&&a[i-1]>a[i+1])n1++;
				}
				if(vis[i+1])
				{
					if(a[i+1]>a[i+2]&&a[i+1]>a[i-1])n1++,n1--;
					else n1--;
				}
				else 
				{
					if(a[i+1]>a[i+2]&&a[i+1]>a[i-1])n1++;
				}
			}
			ans=min(ans,n1);
		}
		cout<<ans<<endl;
	}
}

题意:给两个值,a、b,代表着第a位的斐波拉契数,问从下标a加到b的数是偶数还是奇数。通过简单的操作,发现斐波那契数的规则是1,1,0,1,1,0,1,1,0,1,1,0.1代表奇数,0代表偶数。

只需要将a不断的%3,b也%3.得到n1,n2。 

假设你n1的分布位为 1 1 0,n2的分布为1 1 0.中间的110全部为偶数,全部省略

那么

只有这四种情况,和才为奇数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=2,mod=1000000007 ;
const int N=1e6+10;
char a[N],b[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%s%s",a+1,b+1);
		int l1=strlen(a+1);
		int l2=strlen(b+1);
		int n1=0,n2=0;
		for(int i=1;i<=l1;i++)
		{
			n1+=a[i]-0;
			n1%=3;
		}
		for(int i=1;i<=l2;i++)
		{
			n2+=b[i]-'0';
			n2%=3;
		}
		if((n1==0&&n2==1)||(n1==1&&n2==1)||(n1==2&&n2==0)||(n1==2&&n2==2))
		printf("1\n");
		else printf("0\n");
	}
}

158 - The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple (Mirror) - E

Sequence in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB


DreamGrid has just found an integer sequence in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.

What's the minimum number of operations needed to make the sequence non-decreasing?

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer ( ), indicating the length of the sequence.

The second line contains integers ( ), indicating the given sequence.

It's guaranteed that the sum of of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
4
1 3 2 4
5
2 3 3 5 5

Sample Output

2
0

Hint

For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.

For the second sample test case, as the sequence is already sorted, no operation is needed.

题意:给出几个人给几个人之前的关系,每两个人有一个关系就是互为朋友。若一个参加party的时候没有认识的朋友在里面这个人就会不开心,问如何安排进场人编号,使得不开心的人最少。

注意要字典序最小

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+30;
int u,v,t,n,m,vis[N],vi[N]; 
bool flag=0;
int number[N];
queue<int>que;
vector<int>G[N];
int bfs(int id)
{
	priority_queue<int,vector<int>,greater<int> >q;
	q.push(id);
	int ans=1e9+10;
	vis[id]=1;
	while(!q.empty())
	{
		int u=q.top();q.pop();
		ans=min(ans,u);
		for(int i=0;i<G[u].size();i++)
		{
			int v=G[u][i];
			if(vis[v]) continue;
			vis[v]=1;
			q.push(v);
		}
	}
	return ans;
}
void bfs_1(int *a,int len)
{
	priority_queue<int,vector<int>,greater<int> >q;
	for(int i=1;i<=len;i++)
	{
		q.push(a[i]);
		vis[a[i]]=1;
	}
	while(!q.empty())
	{
		int u=q.top();q.pop();
		que.push(u);
		for(int i=0;i<G[u].size();i++)
		{
			int v=G[u][i];
			if(vis[v]) continue;
			vis[v]=1;
			q.push(v);
		}
	}
}
int main()
{
	cin>>t;
	while(t--)
	{
		scanf("%d%d",&n,&m); 
		for(int i=1;i<=n;i++) G[i].clear(),vis[i]=0;
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&u,&v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		int j=0;
		flag=0;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			vi[++j]=bfs(i);
		}
		for(int i=0;i<=n+1;i++) vis[i]=0;
		bfs_1(vi,j);
		printf("%d\n",j);
		while(que.size()-1)
		{
			printf("%d ",que.front());
			que.pop();
		}
		printf("%d",que.front());
		que.pop();
		puts("");
	}
}

k

Strings in the Pocket


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao has just found two strings and in his left pocket, where indicates the -th character in string , and indicates the -th character in string .

As BaoBao is bored, he decides to select a substring of and reverse it. Formally speaking, he can select two integers and such that and change the string to .

In how many ways can BaoBao change to using the above operation exactly once? Let be an operation which reverses the substring , and be an operation which reverses the substring . These two operations are considered different, if or .

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains a string ( ), while the second line contains another string ( ). Both strings are composed of lower-cased English letters.

It's guaranteed that the sum of of all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

2
abcbcdcbd
abcdcbcbd
abc
abc

Sample Output

3
3

Hint

For the first sample test case, BaoBao can do one of the following three operations: (2, 8), (3, 7) or (4, 6).

For the second sample test case, BaoBao can do one of the following three operations: (1, 1), (2, 2) or (3, 3).

来自https://www.cnblogs.com/whdsunny/p/10785922.html题解

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+10;
char s[N],s1[N],str[N*2];
int len1,len2,p[N*2];
void init()
{
	//memset(p,0,sizeof(p));
	str[0]='$';
	str[1]='#';
	for(int i=0;i<len1;i++)
	{
		str[2*i+2]=s[i];
		str[2*i+3]='#';
	}
	len2=len1*2+2;
	str[len2]='*';
	for(int i=0;i<=len2+5;i++) p[i]=0;
}
void manacher()
{
	int id=0,mx=0;
	for(int i=1;i<len2;i++)
	{
		if(mx>i) p[i]=min(p[2*id-i],mx-i);
		else p[i]=1;
		for(;str[i+p[i]]==str[i-p[i]];p[i]++)
		if(p[i]+i>mx){mx=p[i]+i;id=i;}
	}
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%s%s",s,s1);
		len1=strlen(s);
		int tot1=-1,tot2=len1;
		for(int i=0;i<len1;i++)
		if(s[i]!=s1[i]){
			tot1=i;break;
		}
		for(int i=len1-1;i>=0;i--)
		if(s[i]!=s1[i]){
			tot2=i;break;
		}
		if(tot1==-1)
		{
			init();
			manacher();
			ll ans=0;
			for(int i=0;i<=len2;i++)
		 	ans+=p[i]/2;
		 	printf("%lld\n",ans);
		}
		else
		{
			bool flag=0;
			for(int i=tot1;i<=tot2;i++)
			if((s[i]!=s1[tot2-(i-tot1)])||(s1[i]!=s[tot2-(i-tot1)])) 
			//if(s[i]!=s1[tot2-(i-tot1)])
			{
				flag=1;
				break;
			}
			if(flag) printf("0\n");
			else
			{
				ll ans=0;
				while(tot1>=0&&tot2<len1&&(s[tot1]==s1[tot2])&&(s1[tot1]==s[tot2]))
				tot1--,tot2++,ans++;
				printf("%d\n",ans);
			}
		}
	}
} 

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/89606101