Codeforces Round #704 (Div. 2)

Codeforces Round #704 (Div. 2)

本场Div2共五道题,题目链接:https://codeforces.com/contest/1492

AC两道,0次罚时。

A. Three swimmers

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.

It takes the first swimmer exactly 𝑎a minutes to swim across the entire pool and come back, exactly 𝑏b minutes for the second swimmer and 𝑐c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 00, 𝑎a, 2𝑎2a, 3𝑎3a, ... minutes after the start time, the second one will be at 00, 𝑏b, 2𝑏2b, 3𝑏3b, ... minutes, and the third one will be on the left side of the pool after 00, 𝑐c, 2𝑐2c, 3𝑐3c, ... minutes.

You came to the left side of the pool exactly 𝑝p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.

Input

The first line of the input contains a single integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of test cases. Next 𝑡t lines contains test case descriptions, one per line.

Each line contains four integers 𝑝p, 𝑎a, 𝑏b and 𝑐c (1≤𝑝,𝑎,𝑏,𝑐≤10181≤p,a,b,c≤1018), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.

Output

For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.

Example

input

Copy

4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9

output

Copy

1
4
0
8

Note

In the first test case, the first swimmer is on the left side in 0,5,10,15,…0,5,10,15,… minutes after the start time, the second swimmer is on the left side in 0,4,8,12,…0,4,8,12,… minutes after the start time, and the third swimmer is on the left side in 0,8,16,24,…0,8,16,24,… minutes after the start time. You arrived at the pool in 99 minutes after the start time and in a minute you will meet the first swimmer on the left side.

In the second test case, the first swimmer is on the left side in 0,6,12,18,…0,6,12,18,… minutes after the start time, the second swimmer is on the left side in 0,10,20,30,…0,10,20,30,… minutes after the start time, and the third swimmer is on the left side in 0,9,18,27,…0,9,18,27,… minutes after the start time. You arrived at the pool 22 minutes after the start time and after 44 minutes meet the first swimmer on the left side.

In the third test case, you came to the pool 1010 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!

In the fourth test case, all swimmers are located on the left side in 0,9,18,27,…0,9,18,27,… minutes after the start time. You arrived at the pool 1010 minutes after the start time and after 88 minutes meet all three swimmers on the left side.

题解: 水题(不多解释)

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

ll MIN(ll a, ll b)
{
    if(a < b)
        return a;
    else
        return b;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        ll p, a, b, c;
        scanf("%lld%lld%lld%lld", &p, &a, &b, &c);
        ll ta = p/a;
        ll tb = p/b;
        ll tc = p/c;
        ll ansa = 0, ansb = 0, ansc = 0;
        if(p % a != 0)
            ansa = (ta + 1) * a - p;
        if(p % b != 0)
            ansb = (tb + 1) * b - p;
        if(p % c != 0)
            ansc = (tc + 1) * c - p;
        ll ans = MIN(ansa, ansb);
        ans = MIN(ans, ansc);
        printf("%lld\n", ans);
    }
    return 0;
}

B. Card Deck

time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

You have a deck of 𝑛n cards, and you'd like to reorder it to a new one.

Each card has a value between 11 and 𝑛n equal to 𝑝𝑖pi. All 𝑝𝑖pi are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. 𝑝1p1 stands for the bottom card, 𝑝𝑛pn is the top card.

In each step you pick some integer 𝑘>0k>0, take the top 𝑘k cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)

Let's define an order of a deck as ∑𝑖=1𝑛𝑛𝑛−𝑖⋅𝑝𝑖∑i=1nnn−i⋅pi.

Given the original deck, output the deck with maximum possible order you can make using the operation above.

Input

The first line contains a single integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer 𝑛n (1≤𝑛≤1051≤n≤105) — the size of deck you have.

The second line contains 𝑛n integers 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn (1≤𝑝𝑖≤𝑛1≤pi≤n; 𝑝𝑖≠𝑝𝑗pi≠pj if 𝑖≠𝑗i≠j) — values of card in the deck from bottom to top.

It's guaranteed that the sum of 𝑛n over all test cases doesn't exceed 105105.

Output

For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.

If there are multiple answers, print any of them.

Example

input

Copy

4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1

output

Copy

4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1

Note

In the first test case, one of the optimal strategies is the next one:

  1. take 11 card from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes [1,2,3][1,2,3], 𝑝′p′ becomes [4][4];
  2. take 11 card from the top of 𝑝p: 𝑝p becomes [1,2][1,2], 𝑝′p′ becomes [4,3][4,3];
  3. take 11 card from the top of 𝑝p: 𝑝p becomes [1][1], 𝑝′p′ becomes [4,3,2][4,3,2];
  4. take 11 card from the top of 𝑝p: 𝑝p becomes empty, 𝑝′p′ becomes [4,3,2,1][4,3,2,1].

In result, 𝑝′p′ has order equal to 43⋅4+42⋅3+41⋅2+40⋅143⋅4+42⋅3+41⋅2+40⋅1 == 256+48+8+1=313256+48+8+1=313.

In the second test case, one of the optimal strategies is:

  1. take 44 cards from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes [1][1], 𝑝′p′ becomes [5,2,4,3][5,2,4,3];
  2. take 11 card from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes empty, 𝑝′p′ becomes [5,2,4,3,1][5,2,4,3,1];

In result, 𝑝′p′ has order equal to 54⋅5+53⋅2+52⋅4+51⋅3+50⋅154⋅5+53⋅2+52⋅4+51⋅3+50⋅1 == 3125+250+100+15+1=34913125+250+100+15+1=3491.

In the third test case, one of the optimal strategies is:

  1. take 22 cards from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes [4,2,5,3][4,2,5,3], 𝑝′p′ becomes [6,1][6,1];
  2. take 22 cards from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes [4,2][4,2], 𝑝′p′ becomes [6,1,5,3][6,1,5,3];
  3. take 22 cards from the top of 𝑝p and move it to 𝑝′p′: 𝑝p becomes empty, 𝑝′p′ becomes [6,1,5,3,4,2][6,1,5,3,4,2].

In result, 𝑝′p′ has order equal to 65⋅6+64⋅1+63⋅5+62⋅3+61⋅4+60⋅265⋅6+64⋅1+63⋅5+62⋅3+61⋅4+60⋅2 == 46656+1296+1080+108+24+2=4916646656+1296+1080+108+24+2=49166.

题解:水题、一维数组记录递增位置,重新排序

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn];
int b[maxn];

int ans[maxn];
int val[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        scanf("%d", &n);
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(ans, 0, sizeof(ans));

        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
        }
        int max_ = a[0];
        b[0] = 1;
        for(int i=1; i<n; i++)
        {
            if(a[i] > max_)
            {
                max_ = a[i];
                b[i] = 1;
            }
        }

        int count_ = 0;
        for(int i=0; i<n; i++)
        {
            if(b[i] == 1)
            {
                ans[count_] = i;
                count_++;
            }
        }
        ans[count_++] = n;
        /*
        for(int i=0; i<count_; i++)
        {
            cout << ans[i] << endl;
        }
        */

        int COUNT = 0;
        for(int i=count_ - 2; i>=0; i--)
        {
            for(int j = ans[i]; j<ans[i+1]; j++)
            {
                val[COUNT++] = a[j];
            }
        }
        for(int i=0; i<n-1; i++)
        {
            printf("%d ", val[i]);
        }
        printf("%d\n", val[n-1]);
    }
    return 0;
}

C. Maximum width

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: 𝑠s of length 𝑛n and 𝑡t of length 𝑚m.

A sequence 𝑝1,𝑝2,…,𝑝𝑚p1,p2,…,pm, where 1≤𝑝1<𝑝2<…<𝑝𝑚≤𝑛1≤p1<p2<…<pm≤n, is called beautiful, if 𝑠𝑝𝑖=𝑡𝑖spi=ti for all 𝑖i from 11 to 𝑚m. The width of a sequence is defined as max1≤𝑖<𝑚(𝑝𝑖+1−𝑝𝑖)max1≤i<m(pi+1−pi).

Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings 𝑠s and 𝑡t there is at least one beautiful sequence.

Input

The first input line contains two integers 𝑛n and 𝑚m (2≤𝑚≤𝑛≤2⋅1052≤m≤n≤2⋅105) — the lengths of the strings 𝑠s and 𝑡t.

The following line contains a single string 𝑠s of length 𝑛n, consisting of lowercase letters of the Latin alphabet.

The last line contains a single string 𝑡t of length 𝑚m, consisting of lowercase letters of the Latin alphabet.

It is guaranteed that there is at least one beautiful sequence for the given strings.

Output

Output one integer — the maximum width of a beautiful sequence.

Examples

input

Copy

5 3
abbbc
abc

output

Copy

3

input

Copy

5 2
aaaaa
aa

output

Copy

4

input

Copy

5 5
abcdf
abcdf

output

Copy

1

input

Copy

2 2
ab
ab

output

Copy

1

Note

In the first example there are two beautiful sequences of width 33: they are {1,2,5}{1,2,5} and {1,4,5}{1,4,5}.

In the second example the beautiful sequence with the maximum width is {1,5}{1,5}.

In the third example there is exactly one beautiful sequence — it is {1,2,3,4,5}{1,2,3,4,5}.

In the fourth example there is exactly one beautiful sequence — it is {1,2}{1,2}.

题解:记录两个字符串、子串必定存在于母串中,记录包含子串的母串字符的位置,记录最大的宽度

分别从正反两个方向记录每个字符的位置,对字符进行分割,最后求得最大的宽度

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

const int maxn = 2e5 + 10;

char str1[maxn], str2[maxn];
int ans1[maxn], ans2[maxn];

int main()
{
		int len1, len2;
		scanf("%d%d", &len1, &len2);
		scanf("%s", str1);
		scanf("%s", str2);
		
		int count_ = 0;
		for(int i=0; i<len1; i++)
		{
				if(str1[i] == str2[count_])
				{
						ans1[count_] = i;
						count_++;
				}
		}
		
		count_ = len2 - 1;
		for(int i=len1-1; i>=0; i--)
		{
				if(str1[i] == str2[count_])
				{
						ans2[count_] = i;
						count_--;
				}
		}
		
		int max_ = 0;
		for(int i=0; i<len2 - 1; i++)
		{
				max_ = max(ans2[i+1] - ans1[i], max_);
		}
		printf("%d\n", max_);
		
		
		return 0;
}

D. Genius's Gambit

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are given three integers 𝑎a, 𝑏b, 𝑘k.

Find two binary integers 𝑥x and 𝑦y (𝑥≥𝑦x≥y) such that

  1. both 𝑥x and 𝑦y consist of 𝑎a zeroes and 𝑏b ones;
  2. 𝑥−𝑦x−y (also written in binary form) has exactly 𝑘k ones.

You are not allowed to use leading zeros for 𝑥x and 𝑦y.

Input

The only line contains three integers 𝑎a, 𝑏b, and 𝑘k (0≤𝑎0≤a; 1≤𝑏1≤b; 0≤𝑘≤𝑎+𝑏≤2⋅1050≤k≤a+b≤2⋅105) — the number of zeroes, ones, and the number of ones in the result.

Output

If it's possible to find two suitable integers, print "Yes" followed by 𝑥x and 𝑦y in base-2.

Otherwise print "No".

If there are multiple possible answers, print any of them.

Examples

input

Copy

4 2 3

output

Copy

Yes
101000
100001

input

Copy

3 2 1

output

Copy

Yes
10100
10010

input

Copy

3 2 5

output

Copy

No

Note

In the first example, 𝑥=1010002=25+23=4010x=1010002=25+23=4010, 𝑦=1000012=25+20=3310y=1000012=25+20=3310, 4010−3310=710=22+21+20=11124010−3310=710=22+21+20=1112. Hence 𝑥−𝑦x−y has 33 ones in base-2.

In the second example, 𝑥=101002=24+22=2010x=101002=24+22=2010, 𝑦=100102=24+21=18y=100102=24+21=18, 𝑥−𝑦=20−18=210=102x−y=20−18=210=102. This is precisely one 1.

In the third example, one may show, that it's impossible to find an answer.

题解:给予a个0, b个1,构造两个二进制数字,使得两个数字想减产生的结果的数字包含k个二进制数字1

第一步:构造的数字的第一个数字必须为1。

第二步:倘若不需要产生1,则上下两个数字0、1对应即可。

第三步:倘若需要产生1,则需要构造的数字两侧不同,中间相同。如下代码段所示。

对于3个1, 3个0的情况

倘若相同则
111000
111000
即可

倘若不同(需要产生2个1)
110100
110001

倘若不同(需要产生3个1)
111000
110001

倘若不同(需要产生4个1)
111000
101001

第四步:k需要分为等于0, 不等于0两种情况。a需要分为等于0, 大于0两种情况。b需要分为等于1,大于1的情况。

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int a, b, k;
    while(scanf("%d%d%d", &a, &b, &k)!=EOF)
    {
        if(k == 0)
        {
            printf("Yes\n");
            for(int i=0; i<b; i++)
            {
                printf("1");
            }
            for(int i=0; i<a; i++)
            {
                printf("0");
            }
            printf("\n");

            for(int i=0; i<b; i++)
            {
                printf("1");
            }
            for(int i=0; i<a; i++)
            {
                printf("0");
            }
            printf("\n");
        }
        else if(k >= 1)
        {
            if(b == 1 || a == 0)
            {
                printf("No\n");
            }
            else
            {
            		if(a + b - 2 >= k)
            		{
            				printf("Yes\n");
            				if(a - 1 >= k)
            				{
            						printf("1");
            						for(int i=0; i<b-2; i++)
            						{
            								printf("1");
            						}
            						for(int i=0; i<a - k; i++)
            						{
            								printf("0");
            						}
            						printf("1");
            						for(int i=0; i<k - 1; i++)
            						{
            								printf("0");
            						}
            						printf("0");
            						printf("\n");
            						
            						printf("1");
            						for(int i=0; i<b-2; i++)
            						{
            								printf("1");
            						}
            						for(int i=0; i<a - k; i++)
            						{
            								printf("0");
            						}
            						printf("0");
            						for(int i=0; i<k - 1; i++)
            						{
            								printf("0");
            						}
            						printf("1");
            						printf("\n");
            				}
            				else if(a - 1 < k)
            				{
            						printf("1");
            						for(int i=0; i< b - (k - a) - 2; i++)
            						{
            								printf("1");
            						}
            						printf("1");
            						for(int i=0; i<a-1; i++)
            						{
            								printf("0");
            						}
            						for(int i=0; i< k - a; i++)
            						{
            								printf("1");
            						}
            						printf("0");
            						printf("\n");
            						
            						printf("1");
            						for(int i=0; i< b - (k - a) - 2; i++)
            						{
            								printf("1");
            						}
            						printf("0");
            						for(int i=0; i<a-1; i++)
            						{
            								printf("0");
            						}
            						for(int i=0; i< k - a; i++)
            						{
            								printf("1");
            						}
            						printf("1");
            						printf("\n");
            				}
            		}
            		else
            		{
            				printf("No\n");
            		}
            }
        }
    }
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值