Educational Codeforces Round 64 (Rated for Div. 2)

A. Inscribed Figures
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

题意:模拟
题解:根据题意模拟即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
int a[105];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int sum=0;
    int f=1;
    for(int i=1;i<n;i++){
        if(a[i]+a[i+1]==5){
            f=0;
            break;
        }
        else{
            if(a[i]==1&&a[i+1]==2){sum+=3;if(i>1&&a[i-1]==3)sum--;}
            if(a[i]==1&&a[i+1]==3)sum+=4;
            if(a[i+1]==1&&a[i]==2)sum+=3;
            if(a[i+1]==1&&a[i]==3)sum+=4;
        }
    }
    if(f){
        printf("Finite\n"); 
        printf("%d\n",sum);
    }
    else{
        printf("Infinite\n");
    }
    return 0;
}

B. Ugly Pairs
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

题意:给出一个小写字母串,重排字母串各个字母的顺序,要求相邻位置的字母不能是字典序相邻的字母,其中字典序中’a’和’b’相邻但是’a’和’z’不相邻
题解:构造,按照奇偶可以分成两队即ace…和bdf…,可知队内字母字典序都不相邻,两队合并可得只有相邻处的字母字典序可能相邻,所以要尝试奇偶和偶奇两种合并方式,如果两种合并方式都不合法,也就是说肯定有一队只有一种字母而且与另一队中所有字母字典序相邻,这种情况下直接输出No answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
char ch[105],ch2[105];
int w[105];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",ch+1);
        int len=strlen(ch+1);
        memset(w,0,sizeof(w));
        for(int i=1;i<=len;i++){
            w[ch[i]-'a']++;
        }
        int tot=0;
        for(int i=0;i<='z'-'a';i+=2){
            for(int j=0;j<w[i];j++){
                ch2[++tot]='a'+i;
            }
        }
        for(int i=1;i<='z'-'a';i+=2){
            for(int j=0;j<w[i];j++){
                ch2[++tot]='a'+i;
            }
        }
        int f=1;
        for(int i=1;i<tot;i++){
            if(abs(ch2[i]-ch2[i+1])==1){
                //debug(ch[i]);
                //debug(ch[i+1]);
                f=0;
                break;
            }
        }
        if(!f){
            f=1;
            tot=0;
            for(int i=1;i<='z'-'a';i+=2){
                for(int j=0;j<w[i];j++){
                    ch2[++tot]='a'+i;
                }
            }
            for(int i=0;i<='z'-'a';i+=2){
                for(int j=0;j<w[i];j++){
                    ch2[++tot]='a'+i;
                }
            }
            for(int i=1;i<tot;i++){
                if(abs(ch2[i]-ch2[i+1])==1){
                    f=0;
                    break;
                }
            }
        }
        if(!f)printf("No answer\n");
        else{
            for(int i=1;i<=tot;i++){
                printf("%c",ch2[i]);
            }
            printf("\n");
        }
    }
    return 0;
}

C. Match Points
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).

题意:给出一个序列,要求能组成最多的数对满足|ai-aj|>=z,求可以组成的最多的数对数
题解:二分

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
ll a[200005],n,z;
bool check(ll x){
    int sta=x;
    if(x*2>n)return false;
    for(int i=1;i<=x;i++){
        int pos=lower_bound(a+1+sta,a+1+n,a[i]+z)-a;
        if(pos>n)return false;
        sta=pos;
    }
    return true;
}
int main(){
   // ll n,z;
    scanf("%lld%lld",&n,&z);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
    ll L=0;
    ll ans;
    sort(a+1,a+1+n);
    ll R=n/2;
    while(L<=R){
        ll mid=(L+R)/2;
        if(check(mid)){
            ans=mid;
            L=mid+1;
        }
        else{
            R=mid-1;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

D. 0-1-Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a tree (an undirected connected acyclic graph) consisting of n vertices and n−1 edges. A number is written on each edge, each number is either 0 (let’s call such edges 0-edges) or 1 (those are 1-edges).

Let’s call an ordered pair of vertices (x,y) (x≠y) valid if, while traversing the simple path from x to y, we never go through a 0-edge after going through a 1-edge. Your task is to calculate the number of valid pairs in the tree.

Input
The first line contains one integer n (2≤n≤200000) — the number of vertices in the tree.

Then n−1 lines follow, each denoting an edge of the tree. Each edge is represented by three integers xi, yi and ci (1≤xi,yi≤n, 0≤ci≤1, xi≠yi) — the vertices connected by this edge and the number written on it, respectively.

It is guaranteed that the given edges form a tree.

Output
Print one integer — the number of valid pairs of vertices.

Example
inputCopy
7
2 1 1
3 2 0
4 2 1
5 2 0
6 7 1
7 2 1
outputCopy
34
Note
The picture corresponding to the first example:

题意:给一颗树,树的边权为0或者1,规定走过1之后就不能走0,求可以走的路径总数
题解:树形dp。记录每个子树中可以走到父节点的全1路径数和全0路径数和01路径数以及10路径数,计算兄弟子树对当前子树的贡献以及父节点对子树的贡献即可

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
const int maxn=2e5+5;
struct edge{
    int y;
    int nex;
    int v;
}e[maxn<<1];
int head[maxn],cnt;
ll dp[maxn][4];
void adde(int w,int ww,int www){
    e[cnt].y=ww;
    e[cnt].v=www;
    e[cnt].nex=head[w];
    head[w]=cnt++;
}
ll ans=0;
void dfs(int u,int f){
    ll sum1=0;
    ll sum2=0;
    ll sum3=0;
    ll sum4=0;
    for(int i=head[u];i!=-1;i=e[i].nex){
        int z=e[i].y;
        if(z==f)continue;
        dfs(z,u);
        if(e[i].v){
            dp[u][1]+=1+dp[z][1];
            dp[u][2]+=dp[z][0]+dp[z][2];
            ans+=sum1*(dp[z][1]+1)*2+sum1*dp[z][2]+dp[z][0]*sum1;
            ans+=sum2*(dp[z][1]+1);
            ans+=sum3*(dp[z][1]+1);
            ans+=2*(1+dp[z][1])+dp[z][0]+dp[z][2];
            sum1+=1+dp[z][1];
            sum3+=dp[z][0]+dp[z][2];

        }
        else{
            dp[u][0]+=1+dp[z][0];
            dp[u][3]+=dp[z][1]+dp[z][3];
            ans+=sum2*(dp[z][0]+1)*2+sum2*dp[z][3]+sum2*dp[z][1];
            ans+=sum1*(dp[z][0]+1);
            ans+=sum4*(dp[z][0]+1);
            ans+=2*(1+dp[z][0])+dp[z][1]+dp[z][3];
            sum2+=1+dp[z][0];
            sum4+=dp[z][1]+dp[z][3];
        }
    }

}
int main(){
    int n;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(int i=1;i<n;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        adde(a,b,c);
        adde(b,a,c);
    }
    dfs(1,-1);
    printf("%lld\n",ans);
    return 0;
}

E. Special Segments of Permutation
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You are given a permutation p of n integers 1, 2, …, n (a permutation is an array where each element from 1 to n occurs exactly once).

Let’s call some subsegment p[l,r] of this permutation special if pl+pr=maxi=lrpi. Please calculate the number of special subsegments.

Input
The first line contains one integer n (3≤n≤2⋅105).

The second line contains n integers p1, p2, …, pn (1≤pi≤n). All these integers are pairwise distinct.

Output
Print the number of special subsegments of the given permutation.

Examples
inputCopy
5
3 4 1 5 2
outputCopy
2
inputCopy
3
1 3 2
outputCopy
1
Note
Special subsegments in the first example are [1,5] and [1,3].

The only special subsegment in the second example is [1,3].

待补

F. Card Bag
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
You have a bag which contains n cards. There is a number written on each card; the number on i-th card is ai.

You are playing the following game. During each turn, you choose and remove a random card from the bag (all cards that are still left inside the bag are chosen equiprobably). Nothing else happens during the first turn — but during the next turns, after removing a card (let the number on it be x), you compare it with the card that was removed during the previous turn (let the number on it be y). Possible outcomes are:

if x<y, the game ends and you lose;
if x=y, the game ends and you win;
if x>y, the game continues.
If there are no cards left in the bag, you lose. Cards are not returned into the bag after you remove them.

You have to calculate the probability of winning in this game. It can be shown that it is in the form of PQ where P and Q are non-negative integers and Q≠0, P≤Q. Output the value of P⋅Q−1 (mod 998244353).

Input
The first line contains one integer n (2≤n≤5000) — the number of cards in the bag.

The second live contains n integers a1,a2,…an (1≤ai≤n) — the i-th integer is the number written on the i-th card.

Output
Print one integer — the probability of winning in this game modulo 998244353.

Examples
inputCopy
5
1 1 4 2 3
outputCopy
299473306
inputCopy
2
2 2
outputCopy
1
inputCopy
5
4 5 1 3 2
outputCopy
0
inputCopy
4
1 3 4 3
outputCopy
748683265
Note
In the first test case the probability of winning is 110.

In the second test case the probability of winning is 1.

In the third test case the probability of winning is 0.

In the fourth test case the probability of winning is 14.

待补

G. Optimizer
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.

There are four types of operations in the program, each denoted by one of the characters: $, ^, # or &.

Each line of the program has one of the following formats:

=, where and are valid variable names;
=, where , and are valid variable names, and is an operation character.
The program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.

Two programs are called equivalent if no matter which operations do characters $, ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).

You are given a program consisting of n lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given.

Input
The first line contains one integer n (1≤n≤1000) — the number of lines in the program.

Then n lines follow — the program itself. Each line corresponds to the format described in the statement and has no extra whitespaces.

Output
In the first line print k — the minimum number of lines in the equivalent program.

Then print k lines without any whitespaces — an equivalent program having exactly k lines, in the same format it is described in the statement.

Examples
inputCopy
4
c=aa#bb
d12=c
res=c^d12
tmp=aaKaTeX parse error: Expected 'EOF', got '#' at position 23: …tCopy 2 aaaa=aa#̲bb res=aaaa^aaa…bbbb
min=bbbb^aaaa
outputCopy
0

待补

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值