Educational Codeforces Round 4:(612A:枚举、 612B:水 、 612C:stack、612D:sort )

A. The Text Splitting

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 = 2, q = 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

/*  problem:cf-612A           */
/*  author: dang              */
/*  date:   2016-1-27  14:15  */
/*  题目大意:
    给出字符串的长度n,然后是a、b
    输入长度为n的字符串。
  
    问:n能否由任意数量的a、b组成,
    可以的话,首先输出n被分成几段,再从左到右分段输出字符串
    否则输出-1

    方法:枚举
*/
#include<cstdio>
#include<cstring>
using  namespace std;
int main(){
    int n, a, a_1=0, b;
    int flag;
    char str[105];
    scanf("%d%d%d", &n, &a, &b);
    scanf("%s", str);
    flag = 1;
    if(n%a==0) {             //这个判断是为了节省时间,只用while也可以
        printf("%d\n", n/a);
        for(int i = 0; i < n; i++){
            printf("%c", str[i]);
            if((i+1)%a==0)  printf("\n");
            flag=0;
        }
    }
   else{
      while(flag==1&&a_1<n){      //枚举
        int k = n;
        k -= a_1;
        if(k%b==0){
            printf("%d\n", a_1/a+k/b);
            for(int i = 0; i < n; i ++){
                printf("%c", str[i]);
                if((i+1)%a==0&&i+1<=a_1) printf("\n");
                if(i+1>a_1&&(i+1-a_1)%b==0) printf("\n");
            }
            flag = 0;
        }
        a_1 += a;
     }
   }
    if(flag) printf("-1\n");
    return 0;
}

B. HDD is Outdated Technology

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
/*  problem:cf-612B           */
/*  author: dang              */
/*  date:   2016-1-27  14:39  */
/*  题目大意:
    
    除了用long long 外, 没什么注意点
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
    long long  a, pos;
}f[2*100005];
bool cmp(const node &h, const node &l){
    return h.a < l.a;
}
long long labs(long long  a){
    if(a>0) return a;
    else return -a;
}
int main(){
    long long n, ans = 0;
    scanf("%I64d", &n);
    for(long long  i = 0; i < n; i++){
       scanf("%I64d", &f[i].a);
       f[i].pos = i;
    }
    sort(f, f+n, cmp);
    for(long long i = 1; i < n; i++){
        ans += labs(f[i].pos-f[i-1].pos);
    }
    printf("%I64d\n", ans);
    return 0;
}

C. Replace To Make Regular Bracket Sequence

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
/*  problem:cf-612C           */
/*  author: dang              */
/*  date:   2016-1-27  16:00  */
/*  题目大意:给定一个只有'{' '[' '(' '<'开字符和'}' ']' ')' '>'闭字符的字符串,你可以

将开字符互相转化,闭字符互相转化,要求得到一个括号匹配的串。若可以输出最少转化次数,反

之输出Impossible。
*/
/*
   开始以为<(>) 输出0    
   理解错题意 Wrong answer on test 11
   那么就没有最少之说了,不匹配转化就行了。
*/
#include<cstdio>
#include<cstring>
int main(){
    int a[5]={0}, len;
    char str[1000005];
    scanf("%s", str);
    len = strlen(str);
    if(str[0]==']'||str[0]==')'||str[0]=='}'||str[0]=='>') printf("Impossible\n");
    else if(str[len-1]=='['||str[len-1]=='{'||str[len-1]=='<'||str[len-1]=='(') printf

("Impossible\n");
    else{
        for(int i =0; i < len; i ++){
            if(str[i]=='[') a[1]++;
            else if(str[i]=='{') a[2]++;
            else if(str[i]=='<') a[3]++;
            else if(str[i]=='(') a[4]++;
            else if(str[i]==']') a[1]--;
            else if(str[i]=='}') a[2]--;
            else if(str[i]=='>') a[3]--;
            else if(str[i]==')') a[4]--;

            if(a[1]+a[2]+a[3]+a[4]<0) break;
        }
        if(a[1]+a[2]+a[3]+a[4]==0) {
            for(int i = 1; i <= 4; i++){
                if(a[i]>0) a[0]+=a[i];
            }
            printf("%d\n", a[0]);
        }
        else {
            printf("Impossible\n");
        }
    }
    return 0;
}

/*      
    ac   

    第一反应就是用stack , 然后又想不用栈试试,结果理解错题意了, 还是用栈
*/
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int main(){
    int ans=0, len;
    char str[1000005];
    scanf("%s", str);
    len = strlen(str);
    stack<char> s;
    for(int i = 0; i < len; i++){
        if(str[i]=='['||str[i]=='{'||str[i]=='('||str[i]=='<') s.push(str[i]);
        else {
            if(!s.empty()){
                char ch = s.top(); s.pop();
                if(ch=='['&&str[i]!=']') ans++;
                else if(ch=='{'&&str[i]!='}') ans++;
                else if(ch=='<'&&str[i]!='>') ans++;
                else if(ch=='('&&str[i]!=')') ans++;
            }
            else {
                i = len;
                ans=-1;
            }
        }
    }
    if(!s.empty()||ans==-1) printf("Impossible\n");
    else {
        printf("%d\n", ans);
    }
    return 0;
}

D. The Union of k-Segments

You are given n segments on the coordinate axis Ox and the number k. The point is satisfied if it belongs to at least k segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 106) — the number of segments and the value of k.

The next n lines contain two integers li, ri ( - 109 ≤ li ≤ ri ≤ 109) each — the endpoints of the i-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order.

Output

First line contains integer m — the smallest number of segments.

Next m lines contain two integers aj, bj (aj ≤ bj) — the ends of j-th segment in the answer. The segments should be listed in the order from left to right.

Sample test(s)
Input
3 2
0 5
-3 2
3 8
Output
2
0 2
3 5
Input
3 2
0 5
-3 3
3 8
Output
1
0 5

/*  problem:cf-612D           */
/*  author: dang              */
/*  date:   2016-1-27  19:56  */
/*   
    左右区间分别标记并排序,遇到左就加一,遇到右就减一,等于时k记录此时左右区间
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int dir;
    int val;
}a[2000005];
bool cmp(const node &h, const node &l){
    if(h.val==l.val){
        return h.dir > l.dir;
    }
    else return h.val<l.val;
}
int ans[2000010];
int main(){
    int n, k, cnt=0, num_s=0, num=0;
    int l, r;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++){
        scanf("%d%d",&l, &r);
        a[num_s].val = l;
        a[num_s++].dir = 1;
        a[num_s].val = r;
        a[num_s++].dir=0;
    }
    sort(a, a+num_s,cmp);
    for(int i = 0; i<num_s;i++){
        if(a[i].dir){
           num++;
           if(num==k) ans[cnt++] = a[i].val;
        }else{
            if(num==k) ans[cnt++]=a[i].val;
            num--;
        }
    }
    printf("%d\n", cnt/2);
    for(int i = 0; i < cnt; i+=2)
        printf("%d %d\n", ans[i], ans[i+1]);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值