Educational Codeforces Round 4 总结

A. The Text Splitting
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.

For example, the string "Hello" for p = 2q = 3 can be split to the two strings "Hel" and "lo" or to the two strings "He" and "llo".

Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).

Input

The first line contains three positive integers n, p, q (1 ≤ p, q ≤ n ≤ 100).

The second line contains the string s consists of lowercase and uppercase latin letters and digits.

Output

If it's impossible to split the string s to the strings of length p and q print the only number "-1".

Otherwise in the first line print integer k — the number of strings in partition of s.

Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s — from left to right.

If there are several solutions print any of them.

Sample test(s)
input
5 2 3
Hello
output
2
He
llo
input
10 9 5
Codeforces
output
2
Codef
orces
input
6 4 5
Privet
output
-1
input
8 1 1
abacabac
output
8
a
b
a
c
a
b
a

c

解:模拟题。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=1e3+10;
char s[maxm];
int main()
{
    int n,p,q;
    while(scanf("%d%d%d",&n,&p,&q)!=EOF)
    {
        scanf("%s",s);
        if(n%p==0)
        {
            printf("%d\n",n/p);
            for(int i=1;i<=n;i++)
            {
                printf("%c",s[i-1]);
                if(i%p==0)
                {
                    printf("\n");
                }
            }
        }
        else if(n%q==0)
        {
            printf("%d\n",n/q);
            for(int i=1;i<=n;i++)
            {
                printf("%c",s[i-1]);
                if(i%q==0)
                {
                    printf("\n");
                }
            }
        }
        else
        {
            int ok=0;
            int x,y;
            for(int i=0;i<=100;i++)
            {
                for(int j=0;j<=100;j++)
                {
                    if(i*p+j*q==n)
                    {
                        ok=1;
                        x=i;
                        y=j;
                        break;
                    }
                }
                if(ok)
                {
                    break;
                }
            }
            if(!ok)
            {
                printf("-1\n");
            }
            else
            {
                printf("%d\n",x+y);
                for(int i=1;i<=x*p;i++)
                {
                    printf("%c",s[i-1]);
                    if(i%p==0)
                    {
                        printf("\n");
                    }
                }
                for(int i=x*p+1;i<=n;i++)
                {
                    printf("%c",s[i-1]);
                    if((i-x*p)%q==0)
                    {
                        printf("\n");
                    }
                }
            }
        }
    }
    return 0;
}

B. HDD is Outdated Technology
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.

One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.

Find the time need to read file split to n fragments. The i-th sector contains the fi-th fragment of the file (1 ≤ fi ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.

It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.

Input

The first line contains a positive integer n (1 ≤ n ≤ 2·105) — the number of fragments.

The second line contains n different integers fi (1 ≤ fi ≤ n) — the number of the fragment written in the i-th sector.

Output

Print the only integer — the number of time units needed to read the file.

Sample test(s)
input
3
3 1 2
output
3
input
5
1 3 5 4 2
output
10
Note

In the second example the head moves in the following way:

  • 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units
  • 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units
  • 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units
  • 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units

So the answer to the second example is 4 + 3 + 2 + 1 = 10.


解:水题。离散化就可以了
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define LL long long
const LL maxm=1e6+10;
struct node
{
    LL x,id;
}t[maxm];
LL cmp(node p,node q)
{
    return p.x<q.x;
}
int main()
{
    LL n;
    while(scanf("%lld",&n)!=EOF)
    {
        for(LL i=0;i<n;i++)
        {
            scanf("%lld",&t[i].x);
            t[i].id=i+1;
        }
        sort(t,t+n,cmp);
        LL sum=0;
        for(LL i=0;i<n-1;i++)
        {
            sum+=abs(t[i+1].id-t[i].id);
        }
        printf("%lld\n",sum);
    }
    return 0;
}

C. Replace To Make Regular Bracket Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given string s consists of opening and closing brackets of four kinds <>{}[](). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.

The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2{s1}s2[s1]s2,(s1)s2 are also RBS.

For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

Determine the least number of replaces to make the string s RBS.

Input

The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.

Output

If it's impossible to get RBS from s print Impossible.

Otherwise print the least number of replaces needed to get RBS from s.

Sample test(s)
input
[<}){}
output
2
input
{()}[]
output
0
input
]]
output
Impossible
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
stack<char>q;
int main()
{
    string s;
    cin>>s;
    int cnt=0;
    for(int i=0; i<s.length(); i++)
    {
        if(s[i]=='('||s[i]=='{'||s[i]=='['||s[i]=='<')
        {
            q.push(s[i]);
        }
        else
        {
            if(q.empty())
            {
                cout<<"Impossible"<<endl;
                return 0;
            }
            char x=q.top();
            if(!((x=='<'&&s[i]=='>')||(x=='{'&&s[i]=='}')||(x=='['&&s[i]==']')||(x=='('&&s[i]==')')))
            {
                cnt++;
            }
            q.pop();
        }
    }
    if(q.empty())
    {
        cout<<cnt<<endl;
    }
    else
    {
        cout<<"Impossible"<<endl;
    }
    return 0;
}


你可能感兴趣的:(Educational Codeforces Round 4 总结)