签到题
GLucky 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
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;int main() {int t;scanf("%d",&t);while(t--) {int n;scanf("%d",&n);int x;if(n%7==0)x=n/7;elsex=n/7+1;while(1){int res=x*7;if(res%4!=0) {printf("%d\n",res);break;}elsex++;}}return 0;
}
FAbbreviation
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<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int main() {int t;scanf("%d",&t);while(t--){memset(str,'\0',sizeof(str));scanf("%s",str);printf("%c",str[0]);for(int i=1;i<strlen(str);i++){if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='y')continue;printf("%c",str[i]);}printf("\n");}return 0;
}
规律题
I Fibonacci in the Pocket
Time Limit: 1 Second Memory Limit: 65536 KB
DreamGrid has just found a Fibonacci sequence and two integers and in his right pocket, where indicates the -th element in the Fibonacci sequence.
Please tell DreamGrid if is even or is odd.
Recall that a Fibonacci sequence is an infinite sequence which satisfies , and for all .
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 and only line contains two integers and (). Their meanings are described above.
Output
For each test case output one line. If is even output "0" (without quotes); If is odd output "1" (without quotes).
Sample Input
6
1 2
1 3
1 4
1 5
123456 12345678987654321
123 20190427201904272019042720190427
Sample Output
0
0
1
0
0
1
Hint
The first few elements of the Fibonacci sequence are: , , , , , ...
题意:
给你斐波那契数列下标a和b,求奇偶。
分析:
我们发现斐波那契数列的1 1 2 3 5 8 13
原本数组 :奇奇偶 奇奇偶 奇奇偶 奇奇偶
前缀和奇偶性:奇偶偶 奇偶偶 奇偶偶 奇偶偶
所以只需要模3判断一下,奇-奇 偶-偶为偶数,其他为奇数。
package com;import java.math.BigInteger;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input=new Scanner(System.in);int t;t=input.nextInt();while((t--)>0) {BigInteger a,b;BigInteger zero=new BigInteger("0");BigInteger one=new BigInteger("1");BigInteger two=new BigInteger("2");BigInteger three=new BigInteger("3");a=input.nextBigInteger();b=input.nextBigInteger();if( (b.divide(three)).compareTo(a.divide(three))==0 ) {//a、b在一个区间a=a.mod(three);b=b.mod(three);if(a.compareTo(b)==0) {//a、b在一个位置if(a.compareTo(one)==0||a.compareTo(two)==0)//取模后为1、2System.out.println(1);else if(a.compareTo(zero)==0)//取模后为0System.out.println(0);} else {if((a.compareTo(one)==0)&&(b.compareTo(two)==0)) {//取模后为1、2System.out.println(0);}else if((a.compareTo(one)==0)&&(b.compareTo(zero)==0)) {//取模后为1、0System.out.println(0);}else if((a.compareTo(two)==0)&&(b.compareTo(zero)==0)) {//取模后为2、0System.out.println(1);}}}else {a=a.mod(three);b=b.mod(three);//System.out.println(a);//System.out.println(b);if(a.compareTo(one)==0&&b.compareTo(one)==0) {System.out.println(1);}else if(a.compareTo(one)==0&&b.compareTo(two)==0) {System.out.println(0);}else if(a.compareTo(one)==0&&b.compareTo(zero)==0) {System.out.println(0);}else if(a.compareTo(two)==0&&b.compareTo(one)==0) {System.out.println(0);}else if(a.compareTo(two)==0&&b.compareTo(two)==0) {System.out.println(1);}else if(a.compareTo(two)==0&&b.compareTo(zero)==0) {System.out.println(1);}else if(a.compareTo(zero)==0&&b.compareTo(one)==0) {System.out.println(1);}else if(a.compareTo(zero)==0&&b.compareTo(two)==0) {System.out.println(0);}else if(a.compareTo(zero)==0&&b.compareTo(zero)==0) {System.out.println(0);}}}}
}
学长做法
import java.math.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {public static BigInteger zero=new BigInteger("0");public static void main(String[] args) {Scanner sc = new Scanner(System.in);//result.toString();int T,cas=1;int n,m,cnt=0;BigInteger tmp=new BigInteger("0");BigInteger aa=new BigInteger("0");BigInteger a=new BigInteger("0");BigInteger b=new BigInteger("0");BigInteger zero=new BigInteger("0");BigInteger one=new BigInteger("1");BigInteger two=new BigInteger("2");BigInteger three=new BigInteger("3");n=sc.nextInt();while(n>0){n--;a=sc.nextBigInteger();b=sc.nextBigInteger();tmp=a.mod(three);aa=b.mod(three);if(tmp.equals(two)&&!aa.equals(one))System.out.println(one);else if(!tmp.equals(two)&&aa.equals(one))System.out.println(one);else System.out.println(zero);}}
}
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).
Submit Status
题意:删除一个数,使其峰最少
分析:分情况讨论,暴力列出了即可,不过有很多细节,队友的代码很详细。
不过好像数据很水
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;int a[N];
int pos[N];
int main()
{int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=1; i<=n; i++)scanf("%d",&a[i]);if(n==1 || n==2 || n==3)printf("0\n");else{int cnt=0;for(int i=2; i<=n-1; i++) //寻找峰if(a[i-1]<a[i]&&a[i]>a[i+1])++cnt;if(cnt==0)printf("0\n");else{int maxx=0;if(a[1]<a[2]&&a[2]>a[3])//2为峰顶时maxx=1;if(a[n-2]<a[n-1]&&a[n-1]>a[n])//n-1为峰顶时maxx=1;for(int i=2; i<=n-1; i++){if(a[i-1]<a[i]&&a[i]>a[i+1]) //i为峰顶时{if(a[i-1]!=a[i+1]){if(i>=3)if(a[i-1]>a[i+1] && a[i-2]>=a[i-1])maxx=max(maxx,1);if(i>=n-2)if(a[i-1]<a[i+1] && a[i+1]<=a[i+2] )maxx=max(maxx,1);}}else if(i>=3&&i<=n-2){if(a[i-2]<a[i-1] && a[i-1]>a[i] && a[i]<a[i+1] & a[i+1]>a[i+2] ) //双峰时{if(a[i-1]==a[i+1])maxx=2;elsemaxx=max(maxx,1);}}}printf("%d\n",cnt-maxx);}}}return 0;
}
思维题
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.
题意:
你个数自,操作:每一个数字只能移到第一个位置,问最少操作的次数
分析:
这题以前做过:https://blog.csdn.net/sdz20172133/article/details/86581609,但是当时的想法只能对于1~n的全排列,这个题就gg了,本来以为离散化就ok了,2 3 2 3样例卡死,最后,队友想了一个方法才过的;
正解:
首先我们发现越大的数越不要动,当前最大的数可以不动,把他们中间的数必须移动,而把这些越大的数到前面只会浪费次数。
我们开一个b数组对数组进行排序,对于最大的数我们是可以不动,紧接次大。。。。。
#include<bits/stdc++.h>
#define maxn 1000005
#define ll long long
using namespace std;
int a[maxn];
int b[maxn];
int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++)b[i]=a[i];sort(b+1,b+n+1);ll ans=0;int cur=n;for(int i=cur;i>=1;i--){if(a[i]==b[cur])cur=cur-1;}printf("%d\n",cur);}return 0;
}
J Welcome Party
Time Limit: 2 Seconds Memory Limit: 131072 KB
The 44th World Finals of the International Collegiate Programming Contest (ICPC 2020) will be held in Moscow, Russia. To celebrate this annual event for the best competitive programmers around the world, it is decided to host a welcome party for all participants of the World Finals, numbered from to for convenience.
The party will be held in a large hall. For security reasons, all participants must present their badge to the staff and pass a security check in order to be admitted into the hall. Due to the lack of equipment to perform the security check, it is decided to open only one entrance to the hall, and therefore only one person can enter the hall at a time.
Some participants are friends with each other. There are pairs of mutual friendship relations. Needless to say, parties are more fun with friends. When a participant enters the hall, if he or she finds that none of his or her friends is in the hall, then that participant will be unhappy, even if his or her friends will be in the hall later. So, one big problem for the organizer is the order according to which participants enter the hall, as this will determine the number of unhappy participants. You are asked to find an order that minimizes the number of unhappy participants. Because participants with smaller numbers are more important (for example the ICPC director may get the number 1), if there are multiple such orders, you need to find the lexicographically smallest one, so that important participants enter the hall first.
Please note that if participant and are friends, and if participant and are friends, it's NOT necessary that participant and are friends.
Input
There are multiple test cases. The first line of the input contains a positive integer , indicating the number of cases. For each test case:
The first line contains two integers and (), the number of participants and the number of friendship relations.
The following lines each contains two integers and (), indicating that the -th and the -th participant are friends. Each friendship pair is only described once in the input.
It is guaranteed that neither the sum of nor the sum of of all cases will exceed .
Output
For each case, print a single integer on the first line, indicating the minimum number of unhappy participants. On the second line, print a permutation of to separated by a space, indicating the lexicographically smallest ordering of participants entering the hall that achieves this minimum number.
Consider two orderings and , we say is lexicographically smaller than , if there exists an integer (), such that holds for all , and .
Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!
Sample Input
2
4 3
1 2
1 3
1 4
4 2
1 2
3 4
Sample Output
1
1 2 3 4
2
1 2 3 4
题意:
n个人,m给关系,n个进入会场,如果会场没有认识的人,则这个人会不高兴,注意,关系之间不可传递,朋友的朋友不一定是朋友。问不高兴的人数最少,并且输出字典序最小的入场方案。
分析:
不高兴的人数最少肯定为连通块的个数一个并查集就ok,关键是字典序输出。
对于整体来说,下标标号越小越好,对于每一个连通块我们选下标最小的人作为父亲节点(即为不高兴的人),让其作为整个连通块最先输出的点。首先,对其不高兴的人,选出最小编号的人进会场,然后让其的周围的朋友加入优先队列
#include <bits/stdc++.h>
using namespace std;
const int maxn=1000005;
int fa[maxn],flag[maxn];
int n,m;
const int N=1000005;
//结构体数组edge存边,edge[i]表示第i条边,
//head[i]存以i为起点的第一条边(在edge中的下标)
int head[N],tot;
struct node
{int next; //下一条边的存储下标int to; //这条边的终点int w; //权值
} edge[N*4];
void Add(int u, int v, int w) //起点u, 终点v, 权值w
///tot为边的计数,从0开始计,每次新加的边作为第一条边,最后倒序遍历
{edge[tot].to=v;edge[tot].w=w;edge[tot].next=head[u];head[u]=tot++;
}
void init()
{tot=0;memset(head,-1,sizeof(head));
}
priority_queue<int,vector<int>,greater<int> >pq;
int cnt=1;
void bfs()
{cnt=0;while(!pq.empty()){int u=pq.top();pq.pop();cnt++;if(cnt==n)printf("%d\n",u);elseprintf("%d ",u);for(int i=head[u]; i!=-1; i=edge[i].next) //遍历以u为起点的所有边,与输入顺序相反{int v=edge[i].to;if(flag[v]==0){pq.push(v);flag[v]=1;}}}}
int findd(int x)
{if(x==fa[x])return x;return fa[x]=findd(fa[x]);
}
void mergee(int x,int y)
{int x1=findd(x);int y1=findd(y);if(x1!=y1){if(y1>x1)fa[y1]=x1;elsefa[x1]=y1;}
}
int main()
{int T;scanf("%d",&T);while(T--){while(!pq.empty())pq.pop();scanf("%d%d",&n,&m);//init();tot=0;for(int i=1; i<=n; i++){fa[i]=i;flag[i]=0;head[i]=-1;}int u,v;for(int i=1; i<=m; ++i){scanf("%d%d",&u,&v);mergee(u,v);Add(u,v,0);Add(v,u,0);}int ans=0;for(int i=1; i<=n; i++){if(findd(i)==i){ans++;pq.push(i);flag[i]=1;}}printf("%d\n",ans);bfs();}return 0;
}
学姐代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int maxn=1000005;
int pre[maxn],ans,t[maxn];
int find(int x)
{int r=x;if(x==pre[x]) return x;elsereturn pre[x]=find(pre[x]);
}
void join(int x,int y)
{int fx=find(x);int fy=find(y);if(fx!=fy){ans--;//cout<<x<<" ! "<<y<<endl;if(fx<fy)pre[fy]=fx;else{pre[fx]=fy;}}return;
}
struct AA
{int v,next;
}pos[maxn*3];
int n,m,a,b,vis[maxn],f[maxn],num,k;
void add(int x,int y)
{pos[++num].v=y;pos[num].next=f[x];f[x]=num;pos[++num].v=x;pos[num].next=f[y];f[y]=num;
}
priority_queue<int,vector<int>, greater<int> >pq;
void dfs()
{while(!pq.empty()){int x=pq.top();k++;if(k==n)printf("%d\n",x);else printf("%d ",x);pq.pop();for(int i=f[x];i!=-1;i=pos[i].next){int v=pos[i].v;if(vis[v]) continue;else {vis[v]=1;pq.push(v);}}}return;
}
int main()
{int tt;scanf("%d",&tt);while(tt--){while(!pq.empty()) pq.pop();scanf("%d%d",&n,&m);k=0;ans=n;for(int i=1;i<=n;i++){pre[i]=i;vis[i]=0;f[i]=-1;}num=0;while(m--){scanf("%d%d",&a,&b);add(a,b);join(a,b);}printf("%d\n",ans);for(int i=1;i<=n;i++){if(vis[find(i)]==0){vis[find(i)]=1;pq.push(find(i));}else continue;}dfs();
// for(int i=1;i<n;i++)
// {
// printf("%d ",t[i]);
// }
// printf("%d\n",t[n]);}
}
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).
题意:
给字符串S和T,要求使S变为T,操作:对S的区间(l,r)反转,问有多少种方法?
分析:
关键对于两个字符串相等的情况,马拉车算法求回文子串的个数就ok
不相等的话,直接暴力找到s和t不相同的位置,然后判断反转后能不能相等,不相等直接就0,相等的话,就一此为基础扩展(只要(l,r)两边的字符相等就ok。
#include <bits/stdc++.h>
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
#define ll long long int
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int maxn=2000005;
char s[maxn],t[maxn];
char str[maxn];//原字符串
char tmp[maxn<<1];//转换后的字符串
int Len[maxn<<1];
//转换原始串
int init(char *st)
{int i,len=strlen(st);tmp[0]='@';//字符串开头增加一个特殊字符,防止越界for(i=1; i<=2*len; i+=2){tmp[i]='#';tmp[i+1]=st[i/2];}tmp[2*len+1]='#';tmp[2*len+2]='$';//字符串结尾加一个字符,防止越界tmp[2*len+3]=0;return 2*len+1;//返回转换字符串的长度
}
//Manacher算法计算过程
ll manacher(char *st,int len)
{int mx=0,ans=0,po=0;//mx即为当前计算回文串最右边字符的最大值ll num=0;for(int i=1; i<=len; i++){if(mx>i)Len[i]=min(mx-i,Len[2*po-i]);//在Len[j]和mx-i中取个小elseLen[i]=1;//如果i>=mx,要从头开始匹配while(st[i-Len[i]]==st[i+Len[i]])Len[i]++;if(Len[i]+i>mx)//若新计算的回文串右端点位置大于mx,要更新po和mx的值{mx=Len[i]+i;po=i;}int l=(i-1)/2-(Len[i]-1)/2;int r=(i-1)/2+(Len[i]-1)/2;if(Len[i]&1)r--;num+=((r-l+2)/2);ans=max(ans,Len[i]);}return num; //返回回文子串的个数return ans-1;//返回Len[i]中的最大值-1即为原串的最长回文子串额长度
}
int main()
{int T;scanf("%d",&T);int l,r;while(T--){ll ans=0;scanf("%s%s",s,t);int n=strlen(s);l=-1;for(int i=0; i<n; i++){if(s[i]!=t[i]){l=i;break;}}if(l!=-1){r=-1;for(int i=n-1; i>=0; i--){if(s[i]!=t[i]){r=i;break;}}int j=r,flag=0;for(int i=l; i<=r; i++,j--){if(s[i]!=t[j]){flag=1;break;}}if(flag){printf("0\n");}else{ans=1;for(int i=1; i<=l&&r+i<n; i++){if(s[l-i]==s[r+i])ans++;elsebreak;}printf("%lld\n",ans);}}else///s==t的时候{int len=init(s);cout<<manacher(tmp,len)<<endl;}}
}