Educational Codeforces Round 125 (Rated for Div. 2)A-D

A:

A. Integer Moves

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There's a chip in the point (0,0)(0,0) of the coordinate plane. In one operation, you can move the chip from some point (x1,y1)(x1,y1) to some point (x2,y2)(x2,y2) if the Euclidean distance between these two points is an integer (i.e. (x1−x2)2+(y1−y2)2−−−−−−−−−−−−−−−−−−√(x1−x2)2+(y1−y2)2 is integer).

Your task is to determine the minimum number of operations required to move the chip from the point (0,0)(0,0) to the point (x,y)(x,y).

Input

The first line contains a single integer tt (1≤t≤30001≤t≤3000) — number of test cases.

The single line of each test case contains two integers xx and yy (0≤x,y≤500≤x,y≤50) — the coordinates of the destination point.

Output

For each test case, print one integer — the minimum number of operations required to move the chip from the point (0,0)(0,0) to the point (x,y)(x,y).

Example

input

Copy

3
8 6
0 0
9 15

output

Copy

1
0
2

Note

In the first example, one operation (0,0)→(8,6)(0,0)→(8,6) is enough. (0−8)2+(0−6)2−−−−−−−−−−−−−−−√=64+36−−−−−−√=100−−−√=10(0−8)2+(0−6)2=64+36=100=10 is an integer.

In the second example, the chip is already at the destination point.

In the third example, the chip can be moved as follows: (0,0)→(5,12)→(9,15)(0,0)→(5,12)→(9,15). (0−5)2+(0−12)2−−−−−−−−−−−−−−−−√=25+144−−−−−−−√=169−−−√=13(0−5)2+(0−12)2=25+144=169=13 and (5−9)2+(12−15)2−−−−−−−−−−−−−−−−−√=16+9−−−−−√=25−−√=5(5−9)2+(12−15)2=16+9=25=5 are integers.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,x,y;
int main()
{
    cin>>n;
    while(n--)
    {
        int x,y;
        cin>>x>>y;
        int t=sqrt(x*x+y*y);
        int s=x*x+y*y;
        if(x==0&&y==0)
        {
            cout<<0<<endl;
            continue;
        }

        else if(t*t==s)
        {
            cout<<1<<endl;
        }
        else
        {
            cout<<2<<endl;
        }
    }
    return 0;
}

B:

B. XY Sequence

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given four integers nn, BB, xx and yy. You should build a sequence a0,a1,a2,…,ana0,a1,a2,…,an where a0=0a0=0 and for each i≥1i≥1 you can choose:

  • either ai=ai−1+xai=ai−1+x
  • or ai=ai−1−yai=ai−1−y.

Your goal is to build such a sequence aa that ai≤Bai≤B for all ii and ∑i=0nai∑i=0nai is maximum possible.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Next tt cases follow.

The first and only line of each test case contains four integers nn, BB, xx and yy (1≤n≤2⋅1051≤n≤2⋅105; 1≤B,x,y≤1091≤B,x,y≤109).

It's guaranteed that the total sum of nn doesn't exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the maximum possible ∑i=0nai∑i=0nai.

Example

input

Copy

3
5 100 1 30
7 1000000000 1000000000 1000000000
4 1 7 3

output

Copy

15
4000000000
-10

Note

In the first test case, the optimal sequence aa is [0,1,2,3,4,5][0,1,2,3,4,5].

In the second test case, the optimal sequence aa is [0,109,0,109,0,109,0,109][0,109,0,109,0,109,0,109].

In the third test case, the optimal sequence aa is [0,−3,−6,1,−2][0,−3,−6,1,−2].

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
const int maxn=2e5+5;
int main()
{
    cin>>t;
    while(t--)
    {
        ll n,b,x,y,a[maxn],sum=0;
        memset(a,0,sizeof(a));
        cin>>n>>b>>x>>y;
        for(int i=1; i<=n; i++)
        {
            if(a[i-1]+x>b)
            {
                a[i]=a[i-1]-y;
            }
            else
            {
                a[i]=a[i-1]+x;
            }
        }
        for(int i=1; i<=n; i++)
        {
            sum+=a[i];
        }
        cout<<sum<<endl;

    }


    return 0;
}

C:

C. Bracket Sequence Deletion

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a bracket sequence consisting of nn characters '(' and/or )'. You perform several operations with it.

During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.

The prefix is considered good if one of the following two conditions is satisfied:

  • this prefix is a regular bracket sequence;
  • this prefix is a palindrome of length at least two.

A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not.

The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes.

You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string.

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. The next 2t2t lines describe test cases.

The first line of the test case contains one integer nn (1≤n≤5⋅1051≤n≤5⋅105) — the length of the bracket sequence.

The second line of the test case contains nn characters '(' and/or ')' — the bracket sequence itself.

It is guaranteed that the sum of nn over all test cases do not exceed 5⋅1055⋅105 (∑n≤5⋅105∑n≤5⋅105).

Output

For each test case, print two integers cc and rr — the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations.

Example

input

Copy

5
2
()
3
())
4
((((
5
)((()
6
)((()(

output

Copy

1 0
1 1
2 0
1 0
1 1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t;
int main()
{
    cin>>t;
    while(t--)
    {
        string s;
        ll n;
        cin>>n;
        cin>>s;
        stack<char>st;
        ll ans=0;
        ll i;
        char start=s[0];
        st.push(s[0]);
        ll last=-1;
        for(i=1;i<n;i++)
        {
            if(s[i]==start)
            {
                ans++;
                while(!st.empty())
                {
                    st.pop();
                }
                start='*';
                last=i;
                continue;
            }

            if(st.empty())
            {
                st.push(s[i]);
                start=s[i];
                continue;
            }

            if(st.top()!=s[i] && s[i]==')')
            {
                st.pop();
            }
            else
            {
                st.push(s[i]);
            }

            if(st.empty())
            {
                last=i;
                ans++;
                start='*';
            }
        }
        cout<<ans<<" "<<n-last-1<<endl;
    }

}

D:

D. For Gamers. By Gamers.

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has CC coins to spend on his squad.

Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with CC coins.

There are nn types of units. Every unit type has three parameters:

  • cici — the cost of recruiting one unit of the ii-th type;
  • didi — the damage that one unit of the ii-th type deals in a second;
  • hihi — the amount of health of one unit of the ii-th type.

Monocarp has to face mm monsters. Every monster has two parameters:

  • DjDj — the damage that the jj-th monster deals in a second;
  • HjHj — the amount of health the jj-th monster has.

Monocarp has to fight only the jj-th monster during the jj-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.

For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than CC, then report that it's impossible to kill that monster.

Input

The first line contains two integers nn and CC (1≤n≤3⋅1051≤n≤3⋅105; 1≤C≤1061≤C≤106) — the number of types of units and the amount of coins Monocarp has before each battle.

The ii-th of the next nn lines contains three integers ci,dici,di and hihi (1≤ci≤C1≤ci≤C; 1≤di,hi≤1061≤di,hi≤106).

The next line contains a single integer mm (1≤m≤3⋅1051≤m≤3⋅105) — the number of monsters that Monocarp has to face.

The jj-th of the next mm lines contains two integers DjDj and HjHj (1≤Dj≤1061≤Dj≤106; 1≤Hj≤10121≤Hj≤1012).

Output

Print mm integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than CC, then print −1−1.

Examples

input

Copy

3 10
3 4 6
5 5 5
10 3 4
3
8 3
5 4
10 15

output

Copy

5 3 -1 

input

Copy

5 15
14 10 3
9 2 2
10 4 3
7 3 5
4 3 1
6
11 2
1 1
4 7
2 1
1 14
3 3

output

Copy

14 4 14 4 7 7 

input

Copy

5 13
13 1 9
6 4 5
12 18 4
9 13 2
5 4 5
2
16 3
6 2

output

Copy

12 5 

Note

Consider the first monster of the first example.

Monocarp can't recruit one unit of the first type, because it will take both him and the monster 0.750.75 seconds to kill each other. He can recruit two units for the cost of 66 coins and kill the monster in 0.3750.375 second.

Monocarp can recruit one unit of the second type, because he kills the monster in 0.60.6 seconds, and the monster kills him in 0.6250.625 seconds. The unit is faster. Thus, 55 coins is enough.

Monocarp will need at least three units of the third type to kill the first monster, that will cost 3030 coins.

Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type.

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define all(x) (x).begin(), (x).end()
#define int long long
#define sg signed
using namespace std;

void solve() {
	int n, c;
	cin >> n >> c;
	vector<int> f(c + 1);
	for(int i = 1; i <= n; i++){
		int cst, dps, hp;
		cin >> cst >> dps >> hp;
		f[cst] = max(f[cst], dps * hp);
	}
	for(int i = 1; i <= c; i++){
		for(int j = i * 2; j <= c; j += i){
			f[j] = max(f[j], f[i] * (j / i));
		}
	}
	for(int i = 1; i <= c; i++){
		f[i] = max(f[i], f[i - 1]);
	}
	int m; cin >> m;
	for(int i = 1; i <= m; i++){
		int x, y;
		cin >> x >> y;
		int t = x * y;
		int j = upper_bound(all(f), t) - f.begin();
		if(j == c + 1){
			cout << -1 << " ";
		}
		else{
			cout << j << " ";
		}
	}

}

sg main() {
	int q = 1;// cin >> q;
	while (q--) {
		solve();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值