The math faculty of Berland State University has suffered the sudden drop in the math skills of enrolling students. This year the highest grade on the entrance math test was 8. Out of 100! Thus, the decision was made to make the test easier.
Future students will be asked just a single question. They are given a sequence of integer numbers a1,a2,…,an, each number is from 1 to 3 and ai≠ai+1 for each valid i. The i-th number represents a type of the i-th figure:
circle;
isosceles triangle with the length of height equal to the length of base;
square.
The figures of the given sequence are placed somewhere on a Cartesian plane in such a way that:
(i+1)-th figure is inscribed into the i-th one;
each triangle base is parallel to OX;
the triangle is oriented in such a way that the vertex opposite to its base is at the top;
each square sides are parallel to the axes;
for each i from 2 to n figure i has the maximum possible length of side for triangle and square and maximum radius for circle.
Note that the construction is unique for some fixed position and size of just the first figure.
The task is to calculate the number of distinct points (not necessarily with integer coordinates) where figures touch. The trick is, however, that the number is sometimes infinite. But that won’t make the task difficult for you, will it?
So can you pass the math test and enroll into Berland State University?
Input
The first line contains a single integer n (2≤n≤100) — the number of figures.
The second line contains n integer numbers a1,a2,…,an (1≤ai≤3, ai≠ai+1) — types of the figures.
Output
The first line should contain either the word “Infinite” if the number of distinct points where figures touch is infinite or “Finite” otherwise.
If the number is finite than print it in the second line. It’s guaranteed that the number fits into 32-bit integer type.
Examples
inputCopy
3
2 1 3
outputCopy
Finite
7
inputCopy
3
1 2 3
outputCopy
Infinite
Note
Here are the glorious pictures for the examples. Note that the triangle is not equilateral but just isosceles with the length of height equal to the length of base. Thus it fits into a square in a unique way.
The distinct points where figures touch are marked red.
In the second example the triangle and the square touch each other for the whole segment, it contains infinite number of points.
题意: 有三种图形,1表示圆,2表示等腰三角形,3表示正方形。现在有n个1~3的数组成,分别代表三个图形,且xi内切于xi-1,问他们一共有多少个点相切。
思路: 直接暴力,特判3 1 2的情况,因为此时正方形 圆 等腰三角形同时切于一个点
#include
using namespace std;
int main()
{
int n,ans=0,flag=0;
int a[111];
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=1;i<n;i++){
if(a[i]==2){
if(a[i-1]==1){
if(i>=2&&a[i-2]==3) ans+=2;
else ans+=3;
}
else {flag=1;break;}
}
else if(a[i]==1){
if(a[i-1]==2) ans+=3;
else if(a[i-1]==3) ans+=4;
else {flag=1;break;}
}
else{
if(a[i-1]==1) ans+=4;
else {flag=1;break;}
}
}
if(flag) cout<<"Infinite"<<endl;
else {cout<<"Finite"<<endl;cout<<ans<<endl;}
return 0;
}
You are given a string, consisting of lowercase Latin letters.
A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string “abaca” contains ugly pairs at positions (1,2) — “ab” and (2,3) — “ba”. Letters ‘a’ and ‘z’ aren’t considered neighbouring in a alphabet.
Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can’t add any new letters or remove the existing ones. You can also leave the order the same.
If there are multiple answers, print any of them.
You also have to answer T separate queries.
Input
The first line contains a single integer T (1≤T≤100) — the number of queries.
Each of the next T lines contains string s (1≤|s|≤100) — the string for the next query. It is guaranteed that it contains only lowercase Latin letters.
Note that in hacks you have to set T=1.
Output
Print T lines. The i-th line should contain the answer to the i-th query.
If the answer for the i-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can’t add any new letters or remove the existing ones. You can also leave the order the same.
If there are multiple answers, print any of them.
Otherwise print “No answer” for that query.
Example
inputCopy
4
abcd
gg
codeforces
abaca
outputCopy
cadb
gg
codfoerces
No answer
Note
In the first example answer “bdac” is also correct.
The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok.
There are lots of valid answers for the third example.
题意: 题意:给你一个串,将他们重新排列,使得相邻的字符在字母表中不是相邻的。
例:abcd->dbca
思路: 将字符从小到大排序,分奇数位偶数位字符,分别放在不同的串内,判断他们的首字符和未字符是否相等!!
我的编译器不能用back和front,而别的电脑的编译器就可以用,提交代码也A了,搞了半天…
#include
using namespace std;
bool cmp(char c1,char c2)
{
return c1<c2;
}
int main(){
int t;
cin>>t;
while(t--){
string s;
cin>>s;
sort(s.begin(),s.end(),cmp);
string s1,s2;
for(int i=0;i<s.size();i++){
if((s[i]-'a')%2) s2+=s[i];
else s1+=s[i];
}
if(abs(s1.back()-s2.front())>1) cout<<s1<<s2<<endl;
else if(abs(s1.front()-s2.back())>1) cout<<s2<<s1<<endl;
else cout<<"No answer\n";
}
return 0;
}
You are given a set of points x1, x2, …, xn on the number line.
Two points i and j can be matched with each other if the following conditions hold:
neither i nor j is matched with any other point;
|xi−xj|≥z.
What is the maximum number of pairs of points you can match with each other?
Input
The first line contains two integers n and z (2≤n≤2⋅105, 1≤z≤109) — the number of points and the constraint on the distance between matched points, respectively.
The second line contains n integers x1, x2, …, xn (1≤xi≤109).
Output
Print one integer — the maximum number of pairs of points you can match with each other.
Examples
inputCopy
4 2
1 3 3 7
outputCopy
2
inputCopy
5 5
10 9 5 8 7
outputCopy
1
Note
In the first example, you may match point 1 with point 2 (|3−1|≥2), and point 3 with point 4 (|7−3|≥2).
In the second example, you may match point 1 with point 3 (|5−10|≥5).
题意: 有n个数和z,使得唯一一对数|xi-xj|>=z。这样的数最多有多少?
思路: 由于唯一确定一对数,所以最多有n/2对数,所以可以将序列从小到大排序,分成左右两组a
#include