Codeforces Round #325 (Div. 2)

A. Alena’s Schedule

Alena has successfully passed the entrance exams to the university and is now looking forward to start studying.

One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes).

The University works in such a way that every day it holds exactly n lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks).

The official website of the university has already published the schedule for tomorrow for Alena’s group. Thus, for each of the n pairs she knows if there will be a class at that time or not.

Alena’s House is far from the university, so if there are breaks, she doesn’t always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university.

Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home.

Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair.

Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 100) — the number of lessons at the university.

The second line contains n numbers ai (0 ≤ ai ≤ 1). Number ai equals 0, if Alena doesn’t have the i-th pairs, otherwise it is equal to 1. Numbers a1, a2, …, an are separated by spaces.

Output
Print a single number — the number of pairs during which Alena stays at the university.

Sample test(s)
input
5
0 1 0 1 1
output
4
input
7
1 0 1 0 0 1 0
output
4
input
1
0
output
0
Note
In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair.

In the last sample Alena doesn’t have a single pair, so she spends all the time at home.

Solution :
水题,读懂题就行。

Code:

/*************************************************************************
    > File Name: A.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
using namespace std;

const int N = 222;
int n;
int a[N];
int main(){
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    int l = 1, r = n;
    for (int i = 1; i <= n && a[i] == 0; i++, l++);
    for (int i = n; i >= 1 && a[i] == 0; i--, r--);
    int ans = 0;
    for (int i = l; i <= r; i++)
        if (a[i]) ans++;
        else if (a[i - 1] == 1 && a[i + 1] == 1) ans++;
    printf("%d\n", ans);
    return 0;
}

B. Laurenty and Shop

A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese.

The town where Laurenty lives in is not large. The houses in it are located in two rows, n houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row.

The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets.

Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time.

The traffic light on the crosswalk from the j-th house of the i-th row to the (j + 1)-th house of the same row has waiting time equal to aij (1 ≤ i ≤ 2, 1 ≤ j ≤ n - 1). For the traffic light on the crossing from the j-th house of one row to the j-th house of another row the waiting time equals bj (1 ≤ j ≤ n). The city doesn’t have any other crossings.

The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing.

Figure to the first sample.
Help Laurenty determine the minimum total time he needs to wait at the crossroads.

Input
The first line of the input contains integer n (2 ≤ n ≤ 50) — the number of houses in each row.

Each of the next two lines contains n - 1 space-separated integer — values aij (1 ≤ aij ≤ 100).

The last line contains n space-separated integers bj (1 ≤ bj ≤ 100).

Output
Print a single integer — the least total time Laurenty needs to wait at the crossroads, given that he crosses the avenue only once both on his way to the store and on his way back home.

Sample test(s)
input
4
1 2 3
3 2 1
3 2 2 3
output
12
input
3
1 2
3 3
2 1 3
output
11
input
2
1
1
1 1
output
4
Note
The first sample is shown on the figure above.

In the second sample, Laurenty’s path can look as follows:

Laurenty crosses the avenue, the waiting time is 3;
Laurenty uses the second crossing in the first row, the waiting time is 2;
Laurenty uses the first crossing in the first row, the waiting time is 1;
Laurenty uses the first crossing in the first row, the waiting time is 1;
Laurenty crosses the avenue, the waiting time is 1;
Laurenty uses the second crossing in the second row, the waiting time is 3.
In total we get that the answer equals 11.
In the last sample Laurenty visits all the crossings, so the answer is 4.

Solution:
上一行统计出(1,1)到它的时间,下一行统计出它到(2,n)的时间, O(n) 扫一遍就行

Code:

/*************************************************************************
    > File Name: B.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

const int N = 555;
int n, a[N][N], b[N], t[N];

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= 2; i++)
        for (int j = 2; j <= n; j++)
            scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i++) scanf("%d", &b[i]);

    for (int i = 1; i <= 2; i++)
        for (int j = 2; j <= n; j++)
            a[i][j] += a[i][j - 1];

    for (int i = 1; i <= n; i++)
        t[i] = a[1][i] + b[i] + a[2][n] - a[2][i];
    sort(t + 1, t + 1 + n);
    printf("%d\n", t[1] + t[2]);

    return 0;
}

C. Gennady the Dentist

Gennady is one of the best child dentists in Berland. Today n children got an appointment with him, they lined up in front of his office.

All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to n in the order they go in the line. Every child is associated with the value of his cofidence pi. The children take turns one after another to come into the office; each time the child that is the first in the line goes to the doctor.

While Gennady treats the teeth of the i-th child, the child is crying with the volume of vi. At that the confidence of the first child in the line is reduced by the amount of vi, the second one — by value vi - 1, and so on. The children in the queue after the vi-th child almost do not hear the crying, so their confidence remains unchanged.

If at any point in time the confidence of the j-th child is less than zero, he begins to cry with the volume of dj and leaves the line, running towards the exit, without going to the doctor’s office. At this the confidence of all the children after the j-th one in the line is reduced by the amount of dj.

All these events occur immediately one after the other in some order. Some cries may lead to other cries, causing a chain reaction. Once in the hallway it is quiet, the child, who is first in the line, goes into the doctor’s office.

Help Gennady the Dentist to determine the numbers of kids, whose teeth he will cure. Print their numbers in the chronological order.

Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 4000) — the number of kids in the line.

Next n lines contain three integers each vi, di, pi (1 ≤ vi, di, pi ≤ 106) — the volume of the cry in the doctor’s office, the volume of the cry in the hall and the confidence of the i-th child.

Output
In the first line print number k — the number of children whose teeth Gennady will cure.

In the second line print k integers — the numbers of the children who will make it to the end of the line in the increasing order.

Sample test(s)
input
5
4 2 2
4 1 2
5 2 4
3 3 5
5 1 2
output
2
1 3
input
5
4 5 1
5 3 9
4 1 2
2 1 8
4 1 9
output
4
1 2 4 5
Note
In the first example, Gennady first treats the teeth of the first child who will cry with volume 4. The confidences of the remaining children will get equal to  - 2, 1, 3, 1, respectively. Thus, the second child also cries at the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 0, 2, 0. Then the third child will go to the office, and cry with volume 5. The other children won’t bear this, and with a loud cry they will run to the exit.

In the second sample, first the first child goes into the office, he will cry with volume 4. The confidence of the remaining children will be equal to 5,  - 1, 6, 8. Thus, the third child will cry with the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 5, 5, 7. After that, the second child goes to the office and cry with the volume of 5. The confidences of the remaining children will be equal to 0, 3. Then the fourth child will go into the office and cry with the volume of 2. Because of this the confidence of the fifth child will be 1, and he will go into the office last.

Solution :
虽然是水题,但要注意long long

Code :

/*************************************************************************
    > File Name: C.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef long long ll;
const int N = 4444;
int lst[N], n, v[N], d[N];
ll p[N];
bool away[N];

int main(){
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d%d%I64d", &v[i], &d[i], &p[i]);
    memset(away, 0, sizeof(away));

    int cnt = 0;
    for (int i = 1; i <= n; i++){
        if (away[i]) continue;
        lst[++cnt] = i;
        for (int j = i + 1; v[i] > 0 && j <= n; j++)
            if (!away[j]){ p[j] -= v[i] * 1ll; v[i]--; }
        for (int j = i + 1; j <= n; j++)
            if (!away[j] && p[j] < 0){
                away[j] = 1;
                for (int k = j + 1; k <= n; k++)
                    if (!away[k]) p[k] -= d[j] * 1ll; 
            }
    }
    printf("%d\n", cnt);
    printf("%d", lst[1]);
    for (int i = 2; i <= cnt; i++) printf(" %d", lst[i]);
    printf("\n");
    return 0;
}

D. Phillip and Trains

The mobile application store has a new game called “Subway Roller”.

The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmost column. Some number of trains rides towards the hero. Each train consists of two or more neighbouring cells in some row of the field.

All trains are moving from right to left at a speed of two cells per second, and the hero runs from left to right at the speed of one cell per second. For simplicity, the game is implemented so that the hero and the trains move in turns. First, the hero moves one cell to the right, then one square up or down, or stays idle. Then all the trains move twice simultaneously one cell to the left. Thus, in one move, Philip definitely makes a move to the right and can move up or down. If at any point, Philip is in the same cell with a train, he loses. If the train reaches the left column, it continues to move as before, leaving the tunnel.

Your task is to answer the question whether there is a sequence of movements of Philip, such that he would be able to get to the rightmost column.

Input
Each test contains from one to ten sets of the input data. The first line of the test contains a single integer t (1 ≤ t ≤ 10 for pretests and tests or t = 1 for hacks; see the Notes section for details) — the number of sets.

Then follows the description of t sets of the input data.

The first line of the description of each set contains two integers n, k (2 ≤ n ≤ 100, 1 ≤ k ≤ 26) — the number of columns on the field and the number of trains. Each of the following three lines contains the sequence of n character, representing the row of the field where the game is on. Philip’s initial position is marked as ‘s’, he is in the leftmost column. Each of the k trains is marked by some sequence of identical uppercase letters of the English alphabet, located in one line. Distinct trains are represented by distinct letters. Character ‘.’ represents an empty cell, that is, the cell that doesn’t contain either Philip or the trains.

Output
For each set of the input data print on a single line word YES, if it is possible to win the game and word NO otherwise.

Sample test(s)
input
2
16 4
…AAAAA……..
s.BBB……CCCCC
……..DDDDD…
16 4
…AAAAA……..
s.BBB….CCCCC..
…….DDDDD….
output
YES
NO
input
2
10 4
s.ZZ……
…..AAABB
.YYYYYY…
10 4
s.ZZ……
….AAAABB
.YYYYYY…
output
YES
NO
Note
In the first set of the input of the first sample Philip must first go forward and go down to the third row of the field, then go only forward, then go forward and climb to the second row, go forward again and go up to the first row. After that way no train blocks Philip’s path, so he can go straight to the end of the tunnel.

Note that in this problem the challenges are restricted to tests that contain only one testset.

Solution:
利用相对运动,以火车为参考系,人向右速度为3,终点线向右速度为2。

Code :

/*************************************************************************
    > File Name: D.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

const int N = 1111;
typedef pair<int, int> PII;
#define MP make_pair
int n, k;
PII q[N << 4];
bool vis[5][N];
char s[5][N];

int main(){
    int case_cnt; scanf("%d", &case_cnt);
    while (case_cnt--){
        scanf("%d%d", &n, &k);
        for (int i = 1; i <= 3; i++) scanf("%s", s[i] + 1);
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= 3; i++)
            for (int j = 1; j <= n; j++)
                if (s[i][j] == 's') q[1] = make_pair(i, j);
        for (int i = 1; i <= 3; i++)
            for (int j = n + 1; j <= 3*n; j++)
                s[i][j] = '.';
        int l, r; PII now; vis[q[1].first][q[1].second] = 1;

        for (l = r = 1; l <= r; l++){
            PII now = q[l];
            int nx = now.first, ny = now.second;
            if (ny >= 3*n) continue;
            if (s[nx][ny + 1] != '.') continue;
            ny++;
            if (nx > 1 && s[nx - 1][ny] == '.' && s[nx - 1][ny + 1] == '.' && s[nx - 1][ny + 2] == '.' && !vis[nx - 1][ny + 2]){
                q[++r] = make_pair(nx - 1, ny + 2);
                vis[nx - 1][ny + 2] = 1;
            }
            if (nx < 3 && s[nx + 1][ny] == '.' && s[nx + 1][ny + 1] == '.' && s[nx + 1][ny + 2] == '.' && !vis[nx + 1][ny + 2]){
                q[++r] = make_pair(nx + 1, ny + 2);
                vis[nx + 1][ny + 2] = 1;
            }
            if (s[nx][ny + 1] == '.' && s[nx][ny + 2] == '.' && !vis[nx][ny + 2]){
                q[++r] = make_pair(nx, ny + 2);
                vis[nx][ny + 2] = 1;
            }
        }
        if (!vis[1][3*n - 2]&& !vis[2][3*n - 2] && !vis[3][3*n - 2]
         && !vis[1][3*n - 1]&& !vis[2][3*n - 1] && !vis[3][3*n - 1]
         && !vis[1][3*n]    && !vis[2][3*n] && !vis[3][3*n]) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

E. Alice, Bob, Oranges and Apples

Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either ‘A’, or the letter ‘B’. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter ‘A’, Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter ‘B’, then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.

You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.

Input
The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag.

Output
Print any sequence of cards that would meet the problem conditions as a compressed string of characters ‘A’ and ‘B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them.

If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.

Sample test(s)
input
1 4
output
3B
input
2 2
output
Impossible
input
3 2
output
1A1B
Note
In the first sample, if the row contained three cards with letter ‘B’, then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.

In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.

In the third sample, cards contain letters ‘AB’, so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.

Solution :
精妙结论题。
有一种东西叫 SternBrocotTree ,可以看这里https://en.wikipedia.org/wiki/Stern%E2%80%93Brocot_tree
或者看《具体数学》
我发现这东西没有中文名字,于是明明为桐谷-明日奈树,即 KirigayaAsunaTree ,可以区别于SBT将其2命名为 KATree

Code :

/*************************************************************************
    > File Name: E.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
inline int gcd_1(ll x, ll y){ return y == 0 ? x : gcd_1(y, x % y); }
inline void gcd_2(ll x, ll y){
    char ch = 'A';
    if (y > x) ch = 'B';
    ll x1 = max(x, y), y1 = min(x, y);
    if (x1 % y1 == 0){
        printf("%I64d%c", (x1 / y1) - 1, ch);
        return;
    }else{
        printf("%I64d%c", x1 / y1, ch);
        if (y > x) gcd_2(y1, x1 % y1);
        else gcd_2(x1 % y1, y1);
    }
}
int main(){ 
    ll P, Q;
    scanf("%I64d%I64d", &P, &Q);
    if (gcd_1(P, Q) != 1){ printf("Impossible\n"); return 0; }
    gcd_2(P, Q);
    printf("\n");
    return 0;
}

F. Lizard Era: Beginning

In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions.

The attitude of each of the companions to the hero is an integer. Initially, the attitude of each of them to the hero of neutral and equal to 0. As the hero completes quests, he makes actions that change the attitude of the companions, whom he took to perform this task, in positive or negative direction.

Tell us what companions the hero needs to choose to make their attitude equal after completing all the quests. If this can be done in several ways, choose the one in which the value of resulting attitude is greatest possible.

Input
The first line contains positive integer n (1 ≤ n ≤ 25) — the number of important tasks.

Next n lines contain the descriptions of the tasks — the i-th line contains three integers li, mi, wi — the values by which the attitude of Lynn, Meliana and Worrigan respectively will change towards the hero if the hero takes them on the i-th task. All the numbers in the input are integers and do not exceed 107 in absolute value.

Output
If there is no solution, print in the first line “Impossible”.

Otherwise, print n lines, two characters is each line — in the i-th line print the first letters of the companions’ names that hero should take to complete the i-th task (‘L’ for Lynn, ‘M’ for Meliana, ‘W’ for Worrigan). Print the letters in any order, if there are multiple solutions, print any of them.

Sample test(s)
input
3
1 0 0
0 1 0
0 0 1
output
LM
MW
MW
input
7
0 8 9
5 9 -2
6 -8 -7
9 4 5
-4 -9 9
-4 5 2
-6 8 -7
output
LM
MW
LM
LW
MW
LM
LW
input
2
1 0 0
1 1 0
output
Impossible

Solution :
meet in the middle。
用hash记录前一半的状态,后一半状态对应找就行了。

Code :

/*************************************************************************
    > File Name: F.cpp
    > Author: Archer
 ************************************************************************/

#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, ll> PIL;
map<PII, PIL> h;
#define MP make_pair
const int N = 55;
const int INF = 2147483647;
PIL ans;
int n, n1, v[N][5], lst[N];
ll ans_state;
inline void work(int a, int b, int c, ll state){
    if ( h.find(MP(b-a, c-b)) == h.end()
     ||(h.find(MP(b-a, c-b)) != h.end() && h[MP(b-a,c-b)].first < a) ){
        h[MP(b-a,c-b)] = MP(a, state);  
    }
}
inline void dfs_1(int dep, int a, int b, int c, ll state){
    if (dep > n1){ work(a, b, c, state); return; }
    dfs_1(dep+1, a+v[dep][1], b+v[dep][2], c, (state<<2)|1);
    dfs_1(dep+1, a+v[dep][1], b, c+v[dep][3], ((state<<1)|1)<<1);
    dfs_1(dep+1, a, b+v[dep][2], c+v[dep][3], (((state<<1)|1)<<1)|1);
}
inline void check(int a, int b, int c, ll state){
    if (h.find(MP(a-b, b-c)) != h.end()){
        if (a + h[MP(a-b, b-c)].first > ans.first){ 
            ans = MP(a + h[MP(a-b, b-c)].first, (h[MP(a-b, b-c)].second << (2*(n-n1)))|state );
            ans_state = ans.second;
        }
    }
}
inline void dfs_2(int dep, int a, int b, int c, ll state){
    if (dep > n){ check(a, b, c, state); return; }
    dfs_2(dep+1, a+v[dep][1], b+v[dep][2], c, (state<<2)|1);
    dfs_2(dep+1, a+v[dep][1], b, c+v[dep][3], ((state<<1)|1)<<1);
    dfs_2(dep+1, a, b+v[dep][2], c+v[dep][3], (((state<<1)|1)<<1)|1);
}
inline void print(ll state){
    int cnt = 0;
    if (state == 0){
        printf("Impossible\n"); return;
    }
    while (state > 0){
        int a = state & 1; state >>= 1;
        int b = state & 1; state >>= 1;
        lst[++cnt] = b * 2 + a;
    }
    for (int i = n; i >= 1; i--){
        if (lst[i] == 1) cout << "LM" << endl;
        if (lst[i] == 2) cout << "LW" << endl;
        if (lst[i] == 3) cout << "MW" << endl;
    } 
}
int main(){
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= 3; j++)
            scanf("%d", &v[i][j]);
    if (n == 1){
        if (v[1][1] == v[1][2] && v[1][1] == 0) cout << "LM" << endl; else
        if (v[1][1] == v[1][3] && v[1][1] == 0) cout << "LW" << endl; else
        if (v[1][2] == v[1][3] && v[1][2] == 0) cout << "MW" << endl; 
        else printf("Impossible\n");
        return 0;
    }   
    n1 = n / 2; ans = MP(-INF, 0ll);
    dfs_1(1, 0, 0, 0, 0ll);
    dfs_2(n1 + 1, 0, 0, 0, 0ll); 
    print(ans_state);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值