7.20 7.23习题
You are given an array aa consisting of nn integers. Each aiai is one of the six following numbers: 4,8,15,16,23,424,8,15,16,23,42 .
Your task is to remove the minimum number of elements to make this array good.
An array of length kk is called good if kk is divisible by 66 and it is possible to split it into k6k6 subsequences 4,8,15,16,23,424,8,15,16,23,42 .
Examples of good arrays:
- [4,8,15,16,23,42][4,8,15,16,23,42] (the whole array is a required sequence);
- [4,8,4,15,16,8,23,15,16,42,23,42][4,8,4,15,16,8,23,15,16,42,23,42] (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements);
- [][] (the empty array is good).
Examples of bad arrays:
- [4,8,15,16,42,23][4,8,15,16,42,23] (the order of elements should be exactly 4,8,15,16,23,424,8,15,16,23,42 );
- [4,8,15,16,23,42,4][4,8,15,16,23,42,4] (the length of the array is not divisible by 66 );
- [4,8,15,16,23,42,4,8,15,16,23,23][4,8,15,16,23,42,4,8,15,16,23,23] (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence).
Input
The first line of the input contains one integer nn (1≤n≤5⋅1051≤n≤5⋅105 ) — the number of elements in aa .
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (each aiai is one of the following numbers: 4,8,15,16,23,424,8,15,16,23,42 ), where aiai is the ii -th element of aa .
Output
Print one integer — the minimum number of elements you have to remove to obtain a good array.
Examples
5 4 8 15 16 23
5
12 4 8 4 15 16 8 23 15 16 42 23 42
0
15 4 8 4 8 15 16 8 16 23 15 16 4 42 23 42
3
这道题也算是考察思维的题,题目很简单,就是让你判断所给的数有多少组由4,8,15,15,23,42,,将多余的删去,起初想的很复杂
,后来转换思维,用一个很小的技巧,就可以了,代码一看就懂
#include<iostream> using namespace std; int p[50]; int main() { int n,a,ans=0,x,q; cin>>n; for(int i=1;i<=n;i++) { cin>>a; if(a==4) p[4]++; if(a==8) { p[8]++; p[8]=min(p[4],p[8]); } if(a==15) { p[15]++; p[15]=min(p[15],p[8]); } if(a==16) { p[16]++; p[16]=min(p[15],p[16]); } if(a==23) { p[23]++; p[23]=min(p[16],p[23]); } if(a==42) { p[42]++; p[42]=min(p[23],p[42]); } } cout<<n-6*p[42]<<endl; return 0; }
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m .
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
1 0
1
2 1
1 2
2
3 2
1 2
2 3
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).
这道题是并查集的考察,若两种化学物质根节点相同,则他们的伤害性是同一层次的
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int fu[100]; int init(int n) { for(int i=0;i<=n;i++) fu[i]=i; } int find(int x) { if(x!=fu[x]) fu[x]=find(fu[x]); return fu[x]; } int main() { int n,m,a,b; long long ans; cin>>n>>m; init(n); while(m--) { cin>>a>>b; int fa=find(a); int fb=find(b); fu[fa]=fb; } ans=n; for(int i=1;i<=n;i++) { if(fu[i]==i) ans--; } ans=pow(2,ans); cout<<ans<<endl; return 0; }
Programming Competition
我们想要用10个题目,大声喊出年青人的声音。我们希望和大家一起用技术创造一个更好的2050。
第一道题目,我们来玩一个数字游戏。
给出一个数字 nn,我们想知道 nn 是不是若干个 2050 依次拼接起来的。
Input第一行一个正整数 T (T≤10)T (T≤10) 表示数据组数。
对于每组数据,一行一个正整数 n (1≤n≤10100000)n (1≤n≤10100000)。Output对于每组数据,Yes 表示 nn 是若干个 2050 依次拼接起来的,No 表示不是。Sample Input
2 2050 205020
Sample Output
Yes
No
题解:2050四个数,我们用字符串输入,若长度不是4的倍数,则输出no,否则,记录位置,用flag标记
#include<iostream> using namespace std; #include<cstring> #include<algorithm> int main() { int n; cin>>n; string s; while(n--) { cin>>s; if((s.length()%4)!=0) { cout<<"No"<<endl; continue; } else { int flag=0; for(int i=0;i<=s.length()-4;i+=4) { if(s[i]!='2'||s[i+1]!='0'||s[i+2]!='5'||s[i+3]!='0') { flag=1; break; } } if(flag) cout<<"No"<<endl; else cout<<"Yes"<<endl; } } return 0; }
There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will discuss about an interesting game here. Each player will be given N positive integer. (S)He can make a big integer by appending those integers after one another. Such as if there are 4 integers as 123, 124, 56, 90 then the following integers can be made — 1231245690, 1241235690, 5612312490, 9012312456, 9056124123, etc. In fact 24 such integers can be made. But one thing is sure that 9056124123 is the largest possible integer which can be made. You may think that it’s very easy to find out the answer but will it be easy for a child who has just got the idea of number?
Input Each input starts with a positive integer N (≤ 50). In next lines there are N positive integers. Input is terminated by N = 0, which should not be processed.
Output
For each input set, you have to print the largest possible integer which can be made by appending all the N integers.
Sample Input
4 123 124 56 90 5 123 124 56 90 9 5 9 9 9 9 9 0
Sample Output
9056124123 99056124123 99999
这道题让人很懵,压根没想到用字符串以及排序
string型的两个字符串是可以直接相加的,,如string型的a,b,,a+b:ab,,b+a:ba
#include<iostream> #include<algorithm> using namespace std; string a[55]; int cmp(string a,string b)//字符串的性质,可以直接相加 { return a+b>b+a; } int main() { int n; while(cin>>n&&n) { for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n,cmp); for(int i=0;i<n;i++) { cout<<a[i]; } } return 0; }