Codeforces Round #279 (Div. 2)

来源:互联网 发布:淘宝天猫优惠群可靠吗 编辑:程序博客网 时间:2024/06/07 06:40

A. Team Olympiad
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The School №0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti:

  • ti = 1, if the i-th child is good at programming,
  • ti = 2, if the i-th child is good at maths,
  • ti = 3, if the i-th child is good at PE

Each child happens to be good at exactly one of these three subjects.

The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.

What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?

Input

The first line contains integer n (1 ≤ n ≤ 5000) — the number of children in the school. The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 3), where ti describes the skill of the i-th child.

Output

In the first line output integer w — the largest possible number of teams.

Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them.

If no teams can be compiled, print the only line with value w equal to 0.

Sample test(s)
input
71 3 1 3 2 1 2
output
23 5 26 7 4
input
42 1 1 2
output
0

记录一下,然后输出来就可以了。

/***********************************************\ |Author: YMC |Created Time: 2014/11/23 17:12:30 |File Name: a.cpp |Description: \***********************************************/#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define mset(l,n) memset(l,n,sizeof(l))#define rep(i,n) for(int i=0;i<n;++i)#define maxx(a) memset(a, 0x3f, sizeof(a))#define zero(a) memset(a, 0, sizeof(a))#define srep(i,n) for(int i = 1;i <= n;i ++)#define MP make_pairconst int inf=0x3f3f3f3f ;const double eps=1e-8 ;const double pi=acos (-1.0);typedef long long ll;using namespace std;int a[5005];int b[5005];int c[5005];int t1,t2,t3;int n;int main() {//freopen("input.txt","r",stdin);     while(~scanf("%d",&n)) {        int tp;        t1 = t2 = t3 = 0;        rep(i,n) {            scanf("%d",&tp);            if(tp == 1) {                a[t1 ++] = i+1;            } else if(tp == 2) {                b[t2++] = i+1;            } else {                c[t3 ++] = i+1;            }        }        tp = min(t1,t2);        tp = min(tp,t3);        printf("%d\n",tp);        for(int i=0;i<tp;++i) {            printf("%d %d %d\n",a[i],b[i],c[i]);        }    }return 0;}

B. Queue
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.

Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).

After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.

Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.

Input

The first line contains integer n (2 ≤ n ≤ 2·105) — the number of students in the queue.

Then n lines follow, i-th line contains the pair of integers ai, bi (0 ≤ ai, bi ≤ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.

The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.

Output

Print a sequence of n integers x1, x2, ..., xn — the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.

Sample test(s)
input
492 310 731 07 141
output
92 7 31 141 
Note

The picture illustrates the queue for the first sample.


隔一个来搞。先从为0的开始,可以把奇数位置的弄出来了。然后奇数位置的就是处理出来哪个是第一个,然后再和前面一样搞。

/***********************************************\ |Author: YMC |Created Time: 2014/11/23 17:20:40 |File Name: b.cpp |Description: \***********************************************/#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define mset(l,n) memset(l,n,sizeof(l))#define rep(i,n) for(int i=0;i<n;++i)#define maxx(a) memset(a, 0x3f, sizeof(a))#define zero(a) memset(a, 0, sizeof(a))#define srep(i,n) for(int i = 1;i <= n;i ++)#define MP make_pairconst int inf=0x3f3f3f3f ;const double eps=1e-8 ;const double pi=acos (-1.0);typedef long long ll;using namespace std;int n;int m1[1000002];int m2[1000002];int m3[1000002];bool mm[1000002];int num[1000002];int a,b;int tt;int main() {//freopen("input.txt","r",stdin);     scanf("%d",&n);    memset(mm,false,sizeof(mm));    tt = 0;    rep(i,n) {        scanf("%d %d",&a,&b);        m1[a] = b;        m2[b] = a;        mm[b] = true;        num[tt ++] = a;    }    int ff;    rep(i,tt) {        if(num[i] != 0 && !mm[num[i]]) {            ff = num[i];            break;        }    }    int tot = 2;    int p = 0;    while(tot <= n) {        m3[tot] = m1[p];        p = m1[p];        tot += 2;    }    //srep(i,n) printf("%d ",m3[i]);    tot = 3;    m3[1] = ff;    p = ff;    //cout<<m2[0]<<endl;    while(tot <= n) {        m3[tot] = m1[p];        p = m1[p];        tot += 2;    }    srep(i,n) printf("%d ",m3[i]);    puts("");return 0;}

C. Hacking Cypher
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won.

Having carefully studied the interaction protocol, Polycarpus came to the conclusion that the secret key can be obtained if he properly cuts the public key of the application into two parts. The public key is a long integer which may consist of even a million digits!

Polycarpus needs to find such a way to cut the public key into two nonempty parts, that the first (left) part is divisible by a as a separate number, and the second (right) part is divisible by b as a separate number. Both parts should be positive integers that have no leading zeros. Polycarpus knows values a and b.

Help Polycarpus and find any suitable method to cut the public key.

Input

The first line of the input contains the public key of the messenger — an integer without leading zeroes, its length is in range from 1 to 106digits. The second line contains a pair of space-separated positive integers ab (1 ≤ a, b ≤ 108).

Output

In the first line print "YES" (without the quotes), if the method satisfying conditions above exists. In this case, next print two lines — the left and right parts after the cut. These two parts, being concatenated, must be exactly identical to the public key. The left part must be divisible by a, and the right part must be divisible by b. The two parts must be positive integers having no leading zeros. If there are several answers, print any of them.

If there is no answer, print in a single line "NO" (without the quotes).

Sample test(s)
input
11640102497 1024
output
YES116401024
input
2842545891539281719112818110001009 1000
output
YES284254589153928171911281811000
input
12012 1
output
NO

先正着扫一遍,标记哪些前缀可以被a除尽。然后再扫后缀。扫后缀可以加一个快速幂的优化。

/***********************************************\ |Author: YMC |Created Time: 2014/11/23 17:47:41 |File Name: c.cpp |Description: \***********************************************/#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define mset(l,n) memset(l,n,sizeof(l))#define rep(i,n) for(int i=0;i<n;++i)#define maxx(a) memset(a, 0x3f, sizeof(a))#define zero(a) memset(a, 0, sizeof(a))#define srep(i,n) for(int i = 1;i <= n;i ++)#define MP make_pairconst int inf=0x3f3f3f3f ;const double eps=1e-8 ;const double pi=acos (-1.0);typedef long long ll;using namespace std;char s[1000006];bool m1[1000006];ll m2[1000006];int pp[1000006];ll a,b;ll ppow(ll x,ll n,ll mod){    ll res = 1;    while(n > 0){        if(n & 1) res = res * x % mod;        x = x * x % mod;        n >>= 1;    }    return res;}int main() {    //freopen("input.txt","r",stdin);     scanf("%s",&s);    scanf("%I64d %I64d",&a,&b);    int len = strlen(s);    ll now = 0;    int tot = 0;    rep(i,len) {        now = now * 10 + s[i] - '0';        if(now % a == 0 && s[i+1] != '0') {            m1[i] = true;            //cout<<i<<endl;            pp[tot ++] = i;        }        now = now % a;    }    if(pp[tot - 1] == len - 1) tot --;    int ans = -1;    for(int i = tot - 1;i >= 0;--i) {        int p = pp[i];        now = 0;        for(int j = p+1;j < len;++j) {            if(m2[j] == 0){                now = now * 10 + s[j] - '0';                now = now % b;            } else {                now = now*ppow(10,len-j,b) + m2[j];                now = now % b;                break;            }        }        if(now == 0) {            ans = p;            break;        } else {            m2[p+1] = now;            //cout<<"now == "<<now<<endl;        }    }    if(ans == -1) {        puts("NO");    } else {        puts("YES");        for(int i=0;i<=ans;++i) {            putchar(s[i]);        }        puts("");        for(int i=ans+1;i<len;++i) {            putchar(s[i]);        }        puts("");    }return 0;}

D. Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is a1 × b1 segments large and the second one is a2 × b2 segments large.

Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.

To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:

  • he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar,
  • or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.

In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.

Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16 × 23, then Polycarpus can chip off a half, but not a third. If the bar is 20 × 18, then Polycarpus can chip off both a half and a third. If the bar is 5 × 7, then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.

Input

The first line of the input contains integers a1, b1 (1 ≤ a1, b1 ≤ 109) — the initial sizes of the first chocolate bar. The second line of the input contains integers a2, b2 (1 ≤ a2, b2 ≤ 109) — the initial sizes of the second bar.

You can use the data of type int64 (in Pascal), long long (in С++), long (in Java) to process large integers (exceeding 231 - 1).

Output

In the first line print m — the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.

If there is no solution, print a single line with integer -1.

Sample test(s)
input
2 62 3
output
11 62 3
input
36 510 16
output
316 55 16
input
3 52 1
output
-1

记忆化搜索,保存路径。具体可以看代码

/***********************************************\ |Author: YMC |Created Time: 2014/11/23 18:08:30 |File Name: d.cpp |Description: \***********************************************/#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>#include <algorithm>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define mset(l,n) memset(l,n,sizeof(l))#define rep(i,n) for(int i=0;i<n;++i)#define maxx(a) memset(a, 0x3f, sizeof(a))#define zero(a) memset(a, 0, sizeof(a))#define srep(i,n) for(int i = 1;i <= n;i ++)#define MP make_pairconst int inf=0x3f3f3f3f ;const double eps=1e-8 ;const double pi=acos (-1.0);typedef long long ll;using namespace std;ll a1,b1,a2,b2;ll aa1,bb1,aa2,bb2;int dp[50][50][50][50];int dd[50][50][50][50];int solve(ll a1,ll b1,ll a2,ll b2,int a,int b,int c,int d) {    if(dp[a][b][c][d] != -1) {        return dp[a][b][c][d];    }    ll tt1,tt2;    tt1 = a1 * b1;    tt2 = a2 * b2;    if(tt1 == tt2) {        dp[a][b][c][d] = 0;        dd[a][b][c][d] = 0;        return 0;    }    //dp[0][0][0][0] = inf;    int &ans = dp[a][b][c][d];    ans = inf;    int tp;    if(tt1 > tt2) {        if(a1 % 2 == 0) {            tp = ans;            ans = min(solve(a1/2,b1,a2,b2,a+1,b,c,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 1;            }        }         if(b1 % 2 == 0) {            tp = ans;            ans = min(solve(a1,b1/2,a2,b2,a+1,b,c,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 2;            }        }        if(a1 % 3 == 0) {            tp = ans;            ans = min(solve(a1/3*2,b1,a2,b2,a,b+1,c,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 3;            }        }        if(b1 % 3 == 0) {            tp = ans;            ans = min(solve(a1,b1/3*2,a2,b2,a,b+1,c,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 4;            }        }    } else {        if(a2 % 2 == 0) {            tp = ans;            ans = min(solve(a1,b1,a2/2,b2,a,b,c+1,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 5;            }        }         if(b2 % 2 == 0) {            tp = ans;            ans = min(solve(a1,b1,a2,b2/2,a,b,c+1,d) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 6;            }        }        if(a2 % 3 == 0) {            tp = ans;            ans = min(solve(a1,b1,a2/3*2,b2,a,b,c,d+1) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 7;            }        }        if(b2 % 3 == 0) {            tp = ans;            ans = min(solve(a1,b1,a2,b2/3*2,a,b,c,d+1) + 1,ans);            if(ans != tp) {                dd[a][b][c][d] = 8;            }        }    }    return ans;}void get(int a,int b,int c,int d) {    int tt = dd[a][b][c][d];    if(tt == 0) return ;    if(tt == 1) {        a1 /= 2;        get(a + 1,b,c,d);    }     if(tt == 2) {        b1 /= 2;        get(a + 1,b,c,d);    }     if(tt == 3) {        a1 /= 3;        a1 *= 2;        get(a,b+1,c,d);    }     if(tt == 4) {        b1 /= 3;        b1 *= 2;        get(a,b+1,c,d);    }     if(tt == 5) {        a2 /= 2;        get(a,b,c+1,d);    }     if(tt == 6) {        b2 /= 2;        get(a,b,c+1,d);    }     if(tt == 7) {        a2 /= 3;        a2 *= 2;        get(a,b,c,d+1);    }     if(tt == 8) {        b2 /= 3;        b2 *= 2;        get(a,b,c,d+1);    } }int main() {//freopen("input.txt","r",stdin);     scanf("%I64d %I64d",&a1,&b1);    scanf("%I64d %I64d",&a2,&b2);    memset(dp,-1,sizeof(dp));    int ans = solve(a1,b1,a2,b2,0,0,0,0);    if(ans >= inf) ans = -1;    //solve(a1,b1,a2,b2,0,0,0,0);    if(ans == -1) {        puts("-1");    } else {        printf("%d\n",ans);        get(0,0,0,0);        printf("%I64d %I64d\n",a1,b1);        printf("%I64d %I64d\n",a2,b2);    }return 0;}

e题好像也可以随便搞。自己的写搓了。。。






0 0
原创粉丝点击