Codeforces 374 Div2

A One-dimensional Japanese Crosswor


time limit per test1 secondmemory limit per test256 megabytes

Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).
这里写图片描述
Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.

The example of encrypting of a single row of japanese crossword.
Help Adaltik find the numbers encrypting the row he drew.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters ‘B’ or ‘W’, (‘B’ corresponds to black square, ‘W’ — to white square in the row that Adaltik drew).

Output

The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.

The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.

Examples

input
3
BBW
output
1
2
input
5
BWBWB
output
3
1 1 1
input
4
WWWW
output
0
input
4
BBBB
output
1
4
input
13
WBBBBWWBWBBBW
output
3
4 1 3

Note

The last sample case correspond to the picture in the statement.

水题:输出连续的B的数量

#include <bits/stdc++.h>

using namespace std;

char str[1100];
int a[1100];
int main() {
    int n;
    scanf("%d",&n);
    scanf("%s",str);
    int num = 0 ;
    int op = 0;
    for(int i = 0; i<n; i++) {
        if(str[i] == 'B') {
            num++;
        } else {
            if(num)
                a[op++] = num;
            num = 0;
        }
    }
    if(num) a[op++] = num;

    printf("%d\n",op);
    for(int i =0; i<op; i++) {
        if(i) printf(" ");
        printf("%d",a[i]);
    }
    if(op)
        printf("\n");
    return 0;
}

B. Passwords

time limit per test2 secondsmemory limit per test256 megabytes

Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can’t remember which one exactly he specified during Codehorses registration.

Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.

Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.

Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).

Input

The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya’s passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.

The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.

The last line of the input contains the Vanya’s Codehorses password. It is guaranteed that the Vanya’s Codehorses password is equal to some of his n passwords.

Output

Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.

Examples

input
5 2
cba
abc
bb1
abC
ABC
abc
output
1 15
input
4 100
11
22
1
2
22
output
3 4

Note

Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.

Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.

#include <bits/stdc++.h>

using namespace std;

int vis[110];
char str[110];
int main() {
    int n,k;
    scanf("%d %d",&n,&k);
    memset(vis,false,sizeof(vis));
    for(int i = 0;i<n;i++) {
        scanf("%s",str);
        vis[strlen(str)]++;
    }
    scanf("%s",str);
    int len = strlen(str);
    int num1 = 0 ,num2 = 0;
    for(int i = 1;i<len;i++)num1+=vis[i];
    num2 = vis[len];
    printf("%d %d\n",num1/k*5+(num1+1),(num1+num2-1)/k*5+num2+num1);
    return 0;
}

C. Journey

time limit per test3 secondsmemory limit per test256 megabytes

Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina’s stay in Berlatov is limited and she can’t be there for more than T time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.

Input

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina’s stay in Berlatov respectively.

The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

Output

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

Examples

input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6
input
5 5 6
1 3 3
3 5 3f
1 2 2
2 4 3
4 5 2
output
3
1 3 5

我们记dp[i][j]为到达i经过j个城市的时间。那么我们就可以得到如果dp[i][j] + t[s]<=k,dp[s][j+1] = dp[i][j] + t[s],最后从后向前找到最大到达n时间小于T的路径

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5555;
const int INF = 0x3f3f3f3f;

int n,m,t;
int dp[maxn][maxn];
int pre[maxn][maxn];
vector<pair<int,int> >E[maxn];

void dfs(int u,int num,int val,int fa) {
    if(dp[u][num] <= val) return ;
    dp[u][num] = val; pre[u][num] = fa;
    for(int i = 0;i<E[u].size();i++) {
        if(val + E[u][i].second <=t) {
            dfs(E[u][i].first,num+1,val+E[u][i].second,u);
        }
    }
}
stack<int>st;
void out(int u,int num) {
    st.push(u);
    if(pre[u][num] == -1) {
        printf("%d\n",st.size());
        bool op = false;
        while(!st.empty()) {
            if(op) printf(" ");
            else op = true;
            printf("%d",st.top());
            st.pop();
        }
        printf("\n");
        return ;
    }
    out(pre[u][num],num-1);
}

int main() {
    int u,v,w;
    scanf("%d %d %d",&n,&m,&t);
    memset(dp,INF,sizeof(dp));
    memset(pre,-1,sizeof(pre));
    for(int i = 0;i<m;i++) {
        scanf("%d %d %d",&u,&v,&w);
        E[u].push_back(make_pair(v,w));
    }
    dfs(1,1,0,-1);
    for(int i = n;i>=2;i--) {
        if(dp[n][i] <= t) {
            out(n,i);
            break;
        }
    }
    return 0;
}

D. Maxim and Array

time limit per test2 secondsmemory limit per test256 megabytes

Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≤ i ≤ n) and replaces the i-th element of array ai either with ai + x or with ai - x. Please note that the operation may be applied more than once to the same position.

Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than k operations to it. Please help him in that.

Input

The first line of the input contains three integers n, k and x (1 ≤ n, k ≤ 200 000, 1 ≤ x ≤ 109) — the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively.

The second line contains n integers a1, a2, …, an () — the elements of the array found by Maxim.

Output

Print n integers b1, b2, …, bn in the only line — the array elements after applying no more than k operations to the array. In particular, should stay true for every 1 ≤ i ≤ n, but the product of all array elements should be minimum possible.

If there are multiple answers, print any of them.

Examples

input
5 3 1
5 4 3 5 2
output
5 4 3 5 -1
input
5 3 1
5 4 3 5 5
output
5 4 0 5 5
input
5 3 1
5 4 4 5 5
output
5 1 4 5 5
input
3 2 7
5 4 2
output
5 11 -5

贪心:每次取出绝对值最小的数,判断此时最后的乘积是正还是负,如果是正,则正减负加,如果是负则正加负减。

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int maxn = 1e5;
struct node {
    LL a;
    int id;
    bool operator < (const node &b) const {
        return abs(a)>abs(b.a);
    }
}p[maxn*2];
bool cmp(node a,node b) {
    return a.id<b.id;
}
int main() {
    int n,k,x; node da;
    scanf("%d %d %d",&n,&k,&x);
    bool op = false;
    priority_queue<node>Q;
    for(int i = 0;i<n;i++) {
        scanf("%I64d",&da.a);
        da.id = i;
        Q.push(da);
        if(da.a<0) op = !op;
    }
    while(k--) {
        da = Q.top() ; Q.pop();
        if(da.a < 0) {
            if(op) {
                da.a-=x;
            }
            else da.a +=x;
            if(da.a >= 0) op=!op;
            Q.push(da);
        }
        else {
            if(op) {
                da.a+=x;
            }
            else da.a-=x;
            if(da.a < 0) op=!op;
            Q.push(da);
        }
    }
    int num = 0;
    while(!Q.empty()) {
        p[num++] = Q.top(); Q.pop();
    }
    sort(p,p+n,cmp);
    for(int i = 0;i<n;i++) {
        if(i) printf(" ");
        printf("%I64d",p[i].a);
    }
    printf("\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值