Codeforces Round #683 (Div. 2)

Codeforces Round #683 (Div. 2, by Meet IT)

A. Add Candies

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n bags with candies, initially the i-th bag contains i candies. You want all the bags to contain an equal amount of candies in the end.

To achieve this, you will:

  • Choose m such that 1≤m≤1000
  • Perform m operations. In the j-th operation, you will pick one bag and add j candies to all bags apart from the chosen one.

Your goal is to find a valid sequence of operations after which all the bags will contain an equal amount of candies.

  • It can be proved that for the given constraints such a sequence always exists.
  • You don’t have to minimize mm.
  • If there are several valid sequences, you can output any.

Input

Each test contains multiple test cases.

The first line contains the number of test cases tt (1≤t≤100). Description of the test cases follows.

The first and only line of each test case contains one integer nn (2≤n≤100).

Output

For each testcase, print two lines with your answer.

In the first line print m (1≤m≤1000) — the number of operations you want to take.

In the second line print mm positive integers a1,a2,…,am (1≤ai≤n), where aj is the number of bag you chose on the j-th operation

Example

input

2
2
3

output

1
2
5
3 3 3 1 2

Note

In the first case, adding 1 candy to all bags except of the second one leads to the arrangement with [2,2]candies.

In the second case, firstly you use first three operations to add 1+2+3=6 candies in total to each bag except of the third one, which gives you [7,8,3] . Later, you add 4candies to second and third bag, so you have [7,12,7], and 5 candies to first and third bag — and the result is [12,12,12].

题意:第i个包有i颗糖,问如何操作才能使每个包里面的糖果数量相同,第i次操作,你可以选择其中一个包,除这个包之外 的所有包会加上i颗糖果

思路:第i次操作在第I个包上,操作n次之后,每个包的糖果数为n*(n+1)/2

代码:

#include <iostream>
#include<cstdio>

using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n;
        scanf("%d", &n);
        printf("%d\n", n);
        for(int i = 1; i <= n; i++)
            printf("%d%c", i, " \n"[i == n]);
    }

    return 0;
}

B. Numbers Box

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a rectangular grid with n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value aij written in it.

You can perform the following operation any number of times (possibly zero):

  • Choose any two adjacent cells and multiply the values in them by −1. Two cells are called adjacent if they share a side.

Note that you can use a cell more than once in different operations.

You are interested in X, the sum of all the numbers in the grid.

What is the maximum X you can achieve with these operations?

Input

Each test contains multiple test cases. The first line contains the number of test cases t(1≤t≤100). Description of the test cases follows.

The first line of each test case contains two integers n,m (2≤n, m≤10).

The following n lines contain m integers each, the j-th element in the i-th line is aij (−100≤aij≤100).

Output

For each testcase, print one integer X, the maximum possible sum of all the values in the grid after applying the operation as many times as you want.

Example

input

2
2 2
-1 1
1 1
3 4
0 -1 -2 -3
-1 -2 -3 -4
-2 -3 -4 -5

output

2
30

Note

In the first test case, there will always be at least one −1, so the answer is 2.

In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: 2×1+3×2+3×3+2×4+1×5=30.

题意:有n*m的矩阵,有一个操作:相邻元素可以同时乘以-1,这个操作可以执行无数次,同一个格子可以执行多次

思路:统计负数的个数,负数个数为偶数个,不难证明,可以经过数次操作之后,全变成正数,若负数为奇数个,则减去绝对值最小的元素即可

代码:

#include <iostream>
#include<cstdio>
#include<cmath>

using namespace std;

int a[20][20];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n, m;
        scanf("%d%d", &n, &m);
        int sum, minn, ne;
        sum = ne = 0;
        minn = 100;//初始化成范围内的最大值
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                scanf("%d", &a[i][j]);
                if(a[i][j] < 0)
                {
                    a[i][j] = -a[i][j];
                    ne++;
                }
                sum += a[i][j];
                minn = min(minn, a[i][j]);//找出最小的元素
            }
        }

        if(ne & 1)//负数的个数为奇数个时
        {
            sum -= 2 * minn;//前面加了一个,前去他要减去俩倍
        }
        printf("%d\n", sum);

    }

    return 0;
}

C. Knapsack

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a knapsack with the capacity of W. There are also n items, the i-th one has weight wi

You want to put some of these items into the knapsack in such a way that their total weight C is at least half of its size, but (obviously) does not exceed it. Formally, C should satisfy: ⌈W2⌉≤C≤W.

Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible.

If there are several possible lists of items satisfying the conditions, you can output any. Note that you don’t have to maximize the sum of weights of items in the knapsack.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains integers n and W (1≤n≤200000, 1≤W≤1018).

The second line of each test case contains n integers w1,w2,…,wn (1≤wi≤109) — weights of the items.

The sum of n over all test cases does not exceed 200000.

Output

For each test case, if there is no solution, print a single integer −1.

If there exists a solution consisting of mm items, print m in the first line of the output and m integers j1, j2, …, jm (1≤ji≤n, all ji are distinct) in the second line of the output — indices of the items you would like to pack into the knapsack.

If there are several possible lists of items satisfying the conditions, you can output any. Note that you don’t have to maximize the sum of weights items in the knapsack.

Example

input

3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1

output

1
1
-1
6
1 2 3 5 6 7

Note

In the first test case, you can take the item of weight 3 and fill the knapsack just right.

In the second test case, all the items are larger than the knapsack’s capacity. Therefore, the answer is−1.

In the third test case, you fill the knapsack exactly in half.

题意:有一个容量为w的包,有n个物品,每件物品的重量为wi,问装哪些物品才能使装入包的容量在区间[(w+1))/2, w]内

思路:对物品重量排序,然后从大到小贪心,如果装入包里的总量小于w/2,则无解

代码:

#include <iostream>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N = 2e5 + 10;

struct node{
    int id;
    ll w;
}a[N];

bool cmp(const node a, const node b)
{
    return a.w < b.w;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n;
        ll w;
        scanf("%d%lld", &n, &w);
        for(int i = 1; i <= n; i++){
            scanf("%lld", &a[i].w);
            a[i].id = i;
        }

        sort(a + 1, a + n + 1, cmp);

        ll remain = w;
        vector<int>v;//装入选中的物品
        for(int i = n; i >= 1; i--){
            if(remain >= a[i].w){
                remain -= a[i].w;
                v.push_back(a[i].id);
            }
        }

        if(remain > w / 2){
            printf("-1\n");
            continue;
        }

        printf("%d\n", v.size());
        sort(v.begin(), v.end(), less<int>() );
        for(int i = 0; i < v.size(); i++){
            printf("%d%c", v[i], " \n"[i == v.size()-1]);
        }

    }
    return 0;
}

D. Catching Cheaters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings A and B representing essays of two students who are suspected cheaters. For any two strings C, D we define their similarity score S(C,D) as 4⋅LCS(C,D)−|C|−|D|, where LCS(C,D) denotes the length of the Longest Common Subsequence of strings C and D.

You believe that only some part of the essays could have been copied, therefore you’re interested in their substrings.

Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal S(C,D) over all pairs (C,D), where C is some substring of A, and D is some substring of B.

If X is a string, |X|denotes its length.

A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement.

You may wish to read the Wikipedia page about the Longest Common Subsequence problem.

Input

The first line contains two positive integers n and m (1≤n,m≤5000) — lengths of the two strings A and B.

The second line contains a string consisting of n lowercase Latin letters — string A.

The third line contains a string consisting of m lowercase Latin letters — string B.

Output

Output maximal S(C,D) over all pairs (C,D), where C is some substring of A, and D is some substring of B.

Examples

input

4 5
abba
babab

output

5

input

8 10
bbbbabab
bbbabaaaaa

output

12

input

7 7
uiibwws
qhtkxcn

output

0

Note

For the first case:

abb from the first string and abab from the second string have LCS equal to abb.

The result is S(abb,abab)=(4⋅|abb|) - |abb| - |abab| = 4⋅3−3−4=5.

题意:C是A的子字符串,D是B的子字符串,问所有的C,D中S(C,D) = LCS(C,D) - |C| - |D|的最大值是多少

思路:dp

代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int N = 5e3 + 10;
int dp[N][N];
char a[N], b[N];

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    getchar();
    scanf("%s%s", a + 1, b + 1);

    int ans = 0;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            dp[i][j] = max(0, 4 * (a[i] == b[j]) - 2);
            dp[i][j] = max(dp[i][j], max(dp[i - 1][j] - 1, dp[i][j - 1] - 1));
            if(a[i] == b[j]){
                dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 2);
            }
            ans = max(ans, dp[i][j]);
        }
    }

    printf("%d", ans);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值