Educational Codeforces Round 64 (Rated for Div. 2)ABC

A. Inscribed Figures

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<bits/stdc++.h>
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;
}

B. Ugly Pairs

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<bits/stdc++.h>
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;
}

C. Match Points

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<b<c<d,若a c滿足,則a d必然滿足,同理,若b c滿足,則b d必定滿足

#include<bits/stdc++.h>
using namespace std;
int a[210000];
int main()
{
    int n,z;
    cin>>n>>z;
    for(int i=0;i<n;i++)    scanf("%d",&a[i]);
    sort(a,a+n);
    int ans=0,l=0,r=n/2;
    if(n%2) r++;
    while(l<r){
        if(a[r]-a[l]>=z){
            ans++;
            l++;
        }
        r++;
        if(r == n)  break;
    }
    cout<<ans<<endl;
    return 0;
}

發佈了48 篇原創文章 · 獲贊 2 · 訪問量 3033
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章