Educational Codeforces Round 82 (Rated for Div. 2) A ~ D

                                                                                   A. Erasing Zeroes

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string ss. Each character is either 0 or 1.

You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.

You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?

Input

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

Then tt lines follow, each representing a test case. Each line contains one string ss (1≤|s|≤1001≤|s|≤100); each character of ss is either 0 or 1.

Output

Print tt integers, where the ii-th integer is the answer to the ii-th testcase (the minimum number of 0's that you have to erase from ss).

Example

input

Copy

3
010011
0
1111000

output

Copy

2
0
0

Note

In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).

题目大意 :

输入一个01字符串,输出删除多少个0才能使所有的1相邻

思路 :

记录所有1之间的距离,然后所有距离 - 1求和就好

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
string str;
 
int main()
{
	int T; cin >> T;
	while (T--) {
		cin >> str;
		int last = -1, ans = 0;
		for (int i = 0; i < SZ(str); i++) {
			if (str[i] == '1') {
				if (last != -1)              // 第一次不算
					ans += i - last - 1;
				last = i;
			}
		}
		cout << ans << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                                                   B. National Project

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Your company was appointed to lay new asphalt on the highway of length nn. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.

Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are gg days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next bb days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again gg good days, bb bad days and so on.

You can be sure that you start repairing at the start of a good season, in other words, days 1,2,…,g1,2,…,g are good.

You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5n=5 then at least 33 units of the highway should have high quality; if n=4n=4 then at least 22 units should have high quality.

What is the minimum number of days is needed to finish the repair of the whole highway?

Input

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

Next TT lines contain test cases — one per line. Each line contains three integers nn, gg and bb (1≤n,g,b≤1091≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.

Output

Print TT integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.

Example

input

Copy

3
5 1 1
8 10 10
1000000 1 1000000

output

Copy

5
8
499999500000

Note

In the first test case, you can just lay new asphalt each day, since days 1,3,51,3,5 are good.

In the second test case, you can also lay new asphalt each day, since days 11-88 are good.

题目大意 :

现在有长度为N的铁路,好天气和坏天气会交互出现,例如好天气持续X天,坏天气持续Y天,那么情况就是X天好天气,接着Y天坏天气,再接着X天好天气……在好天气修铁路时铁路的质量也是好的,你需要修完整条铁路,一天一个单位长度,并且得保证铁路的长度除以2向上取整是质量好的,每天你可以选择修或者不修,但是都会算一天,输出最少要多少天才能满足条件修好铁路

思路 :

第一反应条件具备单调性,并且数据大小允许,所以直接二分答案,每次判断该时间下的好天气时间是否大于等于所需要的,再判断一下好天气和坏天气在一起能否大于等于N就好

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
ll n, a, b;
bool check(ll x) {
	ll t = x / (a + b);   // 周期
	ll need = (n - 1) / 2 + 1;   // 需要的好天气数量
	ll good = t * a + min(a, x % (a + b));  // 可以达到的好天气数量
	ll bad = x - good;  // 剩下的都是坏的
	if (good < need || good + bad < n)  
		return false;
	return true;
}
 
int main()
{
	int T; cin >> T;
	while (T--) {
		cin >> n >> a >> b;
		ll l = 1, r = (a + b) * n, ans = INF;
		while (l <= r) {
			ll mid = (l + r) >> 1;
			if (check(mid))
				r = mid - 1, ans = mid;
			else
				l = mid + 1;
		}
		cout << ans << endl;
	}
	return 0;  // 改数组大小!!!
}

                                                                               C. Perfect Keyboard

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row, where all 2626 lowercase Latin letters will be arranged in some order.

Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).

Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?

Input

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

Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.

Output

For each test case, do the following:

  • if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);
  • otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them.

Example

input

Copy

5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza

output

Copy

YES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

题目大意 :

输入一个只包含小写字母的字符串,表示按键顺序,键盘只有一行, 问你该按键顺序能否保证每次都是按相邻的位置(第一次除外),如果能输出任意一种键盘的样式(包含26个英文字母),不能则输出 “NO”

思路 :

第一反应是判环,但是写了一会发现还是直接模拟方便些,以字符串的第一个字母作为模式串,维护一个指针,从字符串的第二个开始遍历,如果模式串的两边有和当前字母相等的,指针左移或者右移,如果没有,看看指针两边是否是空的,是的话任选一个空的地方把字母插进去,如果都不是空的,那么就不可行,最后再判断一下模式串有没有重复的字母,再输出剩余的字母就OK了

Accpeted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
string str, line;
set <char> st, e;
int idx;
 
void init() {
	line = "";
	st.clear();
	idx = 0;
	for (char i = 'a'; i <= 'z'; i++)
		e.insert(i);
}
 
int main()
{
	int T; cin >> T;
	while (T--) {
		cin >> str;
		init();
		line.push_back(str[0]);
		bool ok = true;
		for (int i = 1; i < SZ(str); i++) {
			if (str[i] == line[idx]) {   // 特判相等的情况
				ok = false;
				break;
			}
			if ((idx > 0 && line[idx - 1] != str[i]) && ((idx + 1 < SZ(line) && line[idx + 1] != str[i]))) {
				ok = false; // 两边都有字母且都不相等
				break;
			}
			if (idx > 0 && line[idx - 1] == str[i]) // 指针左移
				idx--;
			else if (idx + 1 < SZ(line) && line[idx + 1] == str[i])  // 指针右移
				idx++;
			else {
				if (idx + 1 == SZ(line))
					line.push_back(str[i]), idx++; // 右边为空,插入字母
				else if (idx == 0) {   // 左边为空,插入字母
					string s;
					s.push_back(str[i]);
					s += line;
					line = s;
				}
			}
		}
		for (auto it : line)
			st.insert(it);
		if (SZ(st) != SZ(line) || !ok)  // 重复的
			cout << "NO" << endl;
		else {
			cout << "YES" << endl;
			for (auto it : st)
				e.erase(it);
			cout << line;
			for (auto it : e)
				cout << it;
			cout << endl;
		}
	}
	return 0;  // 改数组大小!!!
}

                                                                              D. Fill The Bag

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have a bag of size nn. Also you have mm boxes. The size of ii-th box is aiai, where each aiai is an integer non-negative power of two.

You can divide boxes into two parts of equal size. Your goal is to fill the bag completely.

For example, if n=10n=10 and a=[1,1,32]a=[1,1,32] then you have to divide the box of size 3232 into two parts of size 1616, and then divide the box of size 1616. So you can fill the bag with boxes of size 11, 11 and 88.

Calculate the minimum number of divisions required to fill the bag of size nn.

Input

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

The first line of each test case contains two integers nn and mm (1≤n≤1018,1≤m≤1051≤n≤1018,1≤m≤105) — the size of bag and the number of boxes, respectively.

The second line of each test case contains mm integers a1,a2,…,ama1,a2,…,am (1≤ai≤1091≤ai≤109) — the sizes of boxes. It is guaranteed that each aiai is a power of two.

It is also guaranteed that sum of all mm over all test cases does not exceed 105105.

Output

For each test case print one integer — the minimum number of divisions required to fill the bag of size nn (or −1−1, if it is impossible).

Example

input

Copy

3
10 3
1 32 1
23 4
16 1 4 1
20 5
2 1 16 1 8

output

Copy

2
-1
0

题目大意 :

有一个容量为N的背包,和M个盒子,每个盒子的体积都是2的幂次,你可以将体积大的盒子分成两个一半体积的盒子,例如32分成两个16,输出最少分多少次,才能将背包正好装满,如果不行,输出-1

思路 :

题目说了都是2的幂次,很显然利用二进制来做,那么这道题就可以转换成,给你一个二进制串,拥有的条件是每个位上包含了多少个1,高位可以分成2个低位但是有花费,低位合成高位不需要,只要每次操作完,把当前位剩余的送到高位,如果没有,就从高位借,中间可以计算次数

Accepted code

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
 
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pir pair <int, int>
#define MK(x, y) make_pair(x, y)
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & -(x))
#define P2(x) ((x) * (x))
 
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
 
ll n, m;
int s[110], ans, last;
bool ok;
void init() {
	MEM(s, 0);
	ans = last = 0;
	ok = true;
}
void dfs(int x, int y) {
	if (s[x] || s[y] || x > last)  // 最近的高位有1或者超过最大范围了
		return;
	dfs(x + 1, y);
	if (s[y])   
		return;
	ans++;
	if (s[x + 1])
		s[x + 1]--, s[x] += 2;  // 当前位 - 1, 地位 + 2
}
void solve(ll x) {
	int tot = 0;
	while (x) {
		if (x & 1) {
			if (s[tot])
				s[tot]--;  // 有就用
			else {
				dfs(tot, tot);  // 从高位借
				if (!s[tot]) {  // 仍然没有,无法满足条件
					ok = false;
					return;
				}
				s[tot]--;
			}
		}
		int num = s[tot] / 2;  // 剩余的全部送给高位
		s[tot + 1] += num;
		x >>= 1;
		tot++;
	}
}
 
int main()
{
	int T; cin >> T;
	while (T--) {
		sc("%lld %lld", &n, &m);
		init();
		for (int i = 0; i < m; i++) {
			int t, tot = 0; 
			sc("%d", &t);
			while (t) {
				if (t & 1)
					s[tot]++, Max(last, tot); // 计算每一位和最高位1的大小
				t >>= 1;
				tot++;
			}
		}
		solve(n);
		if (!ok)
			printf("-1\n");
		else
			printf("%d\n", ans);
	}
	return 0;  // 改数组大小!!!
}
发布了213 篇原创文章 · 获赞 264 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43851525/article/details/104316089