Educational Codeforces Round 94 [Rated for Div. 2]

B. RPG Protagonist

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.

You decided to rob a town's blacksmith and you take a follower with you. You can carry at most pp units and your follower — at most ff units.

In the blacksmith shop, you found cntscnts swords and cntwcntw war axes. Each sword weights ss units and each war axe — ww units. You don't care what to take, since each of them will melt into one steel ingot.

What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?

Input

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

The first line of each test case contains two integers pp and ff (1≤p,f≤1091≤p,f≤109) — yours and your follower's capacities.

The second line of each test case contains two integers cntscnts and cntwcntw (1≤cnts,cntw≤2⋅1051≤cnts,cntw≤2⋅105) — the number of swords and war axes in the shop.

The third line of each test case contains two integers ss and ww (1≤s,w≤1091≤s,w≤109) — the weights of each sword and each war axe.

It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed 2⋅1052⋅105.

Output

For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.

Example

input

Copy

3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5

output

Copy

11
20
3

Note

In the first test case:

  • you should take 33 swords and 33 war axes: 3⋅5+3⋅6=33≤333⋅5+3⋅6=33≤33
  • and your follower — 33 swords and 22 war axes: 3⋅5+2⋅6=27≤273⋅5+2⋅6=27≤27.

3+3+3+2=113+3+3+2=11 weapons in total.

In the second test case, you can take all available weapons even without your follower's help, since 5⋅10+5⋅10≤1005⋅10+5⋅10≤100.

In the third test case, you can't take anything, but your follower can take 33 war axes: 3⋅5≤193⋅5≤19.

       这道题,首先贪心一下,重量小的那个(假设是a)你和你的追随者肯定能拿越多越好,这种情况下,我们枚举你所拿的这个a的数量,然后进行计算,最后ans最大的输出即可。

#include<cstdio>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
typedef long long ll;
int n, m;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        long long p, f, cnts, cntw, s, w, ans = -1;
        cin >> p >> f >> cnts >> cntw >> s >> w;
        for (long long i = 0; i <= cnts; i++)
        {
            long long temp = i;
            long long _s = cnts - i;
            long long _w = cntw;
            long long temps = s;
            long long tempw = w;
            long long _p = p - i * s;
            long long _f = f;
            long long minn = min(_p / w, cntw);
            temp = temp + min(cntw, _p / w);
            _w = _w - minn;
            if (tempw < temps)
            {
                swap(temps, tempw);
                swap(_s, _w);
            }
            minn = min(_s, _f / temps);
            _f = _f - minn * temps;
            _s = _s - minn;
            temp = temp + minn;
            minn = min(_f / tempw, _w);
            temp = temp + minn;
            ans = max(ans, temp);
        }
        cout << ans << endl;
    }
    return 0;
}

C. Binary String Reconstruction

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Consider the following process. You have a binary string (a string where each character is either 0 or 1) ww of length nn and an integer xx. You build a new binary string ss consisting of nn characters. The ii-th character of ss is chosen as follows:

  • if the character wi−xwi−x exists and is equal to 1, then sisi is 1 (formally, if i>xi>x and wi−x=wi−x= 1, then si=si= 1);
  • if the character wi+xwi+x exists and is equal to 1, then sisi is 1 (formally, if i+x≤ni+x≤n and wi+x=wi+x= 1, then si=si= 1);
  • if both of the aforementioned conditions are false, then sisi is 0.

You are given the integer xx and the resulting string ss. Reconstruct the original string ww.

Input

The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases.

Each test case consists of two lines. The first line contains the resulting string ss (2≤|s|≤1052≤|s|≤105, each character of ss is either 0 or 1). The second line contains one integer xx (1≤x≤|s|−11≤x≤|s|−1).

The total length of all strings ss in the input does not exceed 105105.

Output

For each test case, print the answer on a separate line as follows:

  • if no string ww can produce the string ss at the end of the process, print −1−1;
  • otherwise, print the binary string ww consisting of |s||s| characters. If there are multiple answers, print any of them.

Example

input

Copy

3
101110
2
01
1
110
1

output

Copy

111011
10
-1

这道题主要是看原来string里面0的问题,然后标记一下分情况讨论就行了就行了。

#include<cstdio>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
typedef long long ll;
int n, m;
int w[M];
string ans = "";
string str;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int te; cin >> te;
    while (te--) {
        ans = "";
        int x;
        cin >> str;
        cin >> x;
        int len = str.length();
        for (int i = 0; i < len; i++) {
            w[i] = 1;
        }
        for (int i = 0; i < len; i++) {
            if (str[i] == '1')continue;
            if (i >= x) {
                w[i - x] = 0;
            }
            if (i + x < len) {
                w[i + x] = 0;
            }
        }
        int t = 1;
        for (int i = 0; i < len; i++)
        {
            ans += w[i] + '0';
            if (str[i] == '1') {
                bool flag1, flag2;
                if (i >= x) {
                    flag1 = w[i - x];
                }
                else {
                    flag1 = 0;
                }
                if (i + x < len) {
                    flag2 = w[i + x];
                }
                else {
                    flag2 = 0;
                }
                if (flag1 == 0 && flag2 == 0) {
                    cout << "-1\n";
                    t = 0; break;
                }
            }
        }
        if (t == 1)
            cout << ans << endl;
    }
    return 0;
}

D. Zigzags

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1,a2…ana1,a2…an. Calculate the number of tuples (i,j,k,l)(i,j,k,l) such that:

  • 1≤i<j<k<l≤n1≤i<j<k<l≤n;
  • ai=akai=ak and aj=alaj=al;

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case contains a single integer nn (4≤n≤30004≤n≤3000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the array aa.

It's guaranteed that the sum of nn in one test doesn't exceed 30003000.

Output

For each test case, print the number of described tuples.

Example

input

Copy

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

output

Copy

5
2

Note

In the first test case, for any four indices i<j<k<li<j<k<l are valid, so the answer is the number of tuples.

In the second test case, there are 22 valid tuples:

  • (1,2,4,6)(1,2,4,6): a1=a4a1=a4 and a2=a6a2=a6;
  • (1,3,4,6)(1,3,4,6): a1=a4a1=a4 and a3=a6a3=a6.

像这种枚举四元组基本都枚举中间两个,然后在做之前预处理一下就可以了。

#include<cstdio>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 3005;
const int M = 1e6 + 10;
typedef long long ll;
int n, ARR[N], ARR2[N][N];
int doit(int l, int r, int k) {
	return ARR2[r][k] - ARR2[l - 1][k];
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int te; cin >> te;
	while (te--) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++)
				ARR2[i][j] = 0;
		}
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> ARR[i];
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++)
				ARR2[i][j] = ARR2[i - 1][j];
			ARR2[i][ARR[i]] ++;
		}
		long long ans = 0;
		for (int j = 2; j <= n - 2; j++)
			for (int k = j + 1; k <= n - 1; k++)
				ans += 1LL * doit(1, j - 1, ARR[k]) * doit(k + 1, n, ARR[j]);
		cout << ans << '\n';
	}
	return 0;
}

E. Clear the Multiset

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a multiset containing several integers. Initially, it contains a1a1 elements equal to 11, a2a2 elements equal to 22, ..., anan elements equal to nn.

You may apply two types of operations:

  • choose two integers ll and rr (l≤rl≤r), then remove one occurrence of ll, one occurrence of l+1l+1, ..., one occurrence of rr from the multiset. This operation can be applied only if each number from ll to rr occurs at least once in the multiset;
  • choose two integers ii and xx (x≥1x≥1), then remove xx occurrences of ii from the multiset. This operation can be applied only if the multiset contains at least xx occurrences of ii.

What is the minimum number of operations required to delete all elements from the multiset?

Input

The first line contains one integer nn (1≤n≤50001≤n≤5000).

The second line contains nn integers a1a1, a2a2, ..., anan (0≤ai≤1090≤ai≤109).

Output

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Examples

input

Copy

4
1 4 1 1

output

Copy

2

input

Copy

5
1 0 1 0 1

output

Copy

3

分治问题,典型的如CF448C这种。可以借鉴一下,这里就懒得写了。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值