目录
A-Keanu Reeves
After playing Neo in the legendary "Matrix" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.
Let's call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.
We are given a string ss of length nn consisting of only zeroes and ones. We need to cut ss into minimal possible number of substrings s_1, s_2, \ldots, s_ks1,s2,…,sk such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings s_1, s_2, \ldots, s_ks1,s2,…,sk such that their concatenation (joining) equals ss, i.e. s_1 + s_2 + \dots + s_k = ss1+s2+⋯+sk=s.
For example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can't cut 110010 to the smaller number of substrings as 110010 isn't good itself. At the same time, cutting of 110010 into 1100 and 10 isn't valid as both strings aren't good. Also, cutting of 110010 into 1, 1, 0010 isn't valid, as it isn't minimal, even though all 33 strings are good.
Can you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.
Input
The first line of the input contains a single integer nn (1\le n \le 1001≤n≤100) — the length of the string ss.
The second line contains the string ss of length nn consisting only from zeros and ones.
Output
In the first line, output a single integer kk (1\le k1≤k) — a minimal number of strings you have cut ss into.
In the second line, output kk strings s_1, s_2, \ldots, s_ks1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to ss and all of them have to be good.
If there are multiple answers, print any.
Examples
Input
1 1
Output
1 1
Input
2 10
Output
2 1 0
Input
6 100011
Output
2 100 011
Note
In the first example, the string 1 wasn't cut at all. As it is good, the condition is satisfied.
In the second example, 1 and 0 both are good. As 10 isn't good, the answer is indeed minimal.
In the third example, 100 and 011 both are good. As 100011 isn't good, the answer is indeed minimal.
题意:
输入一个由0和1组成的串,将串拆成若干子串(可以不拆)保证每个子串0和1数量不同,输出最少子串数和每个子串。
思路:
如果输入的串0和1数量不同就直接输出;否则只需要分成两个串,第一个数字为一个,后面的所有数字组成一个。
代码:
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
int main()
{
ll n,a=0,b=0;
string s;
cin>>n;
cin>>s;
for(int i=0;i<n;i++)
{
if(s[i]=='0')
a++;
else
b++;
}
if(a!=b)
{
cout<<1<<endl;
cout<<s<<endl;
}
else
{
cout<<2<<endl;
cout<<s[0]<<" ";
for(int i=1;i<n;i++)
cout<<s[i];
cout<<endl;
}
}
B-Number Circle
You are given nn numbers a_1, a_2, \ldots, a_na1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?
For example, for the array [1, 4, 5, 6, 7, 8][1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5\ge 4 + 15≥4+1 and 8> 1 + 68>1+6.
Input
The first line contains a single integer nn (3\le n \le 10^53≤n≤105) — the number of numbers.
The second line contains nn integers a_1, a_2, \ldots, a_na1,a2,…,an (1 \leq a_i \le 10^91≤ai≤109) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
Output
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output nn numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
Examples
Input
3 2 4 3
Output
YES 4 2 3
Input
5 1 2 3 4 4
Output
YES 4 4 2 1 3
Input
3 13 8 5
Output
NO
Input
4 1 10 100 1000
Output
NO
Note
One of the possible arrangements is shown in the first example:
4< 2 + 34<2+3;
2 < 4 + 32<4+3;
3< 4 + 23<4+2.
One of the possible arrangements is shown in the second example.
No matter how we arrange 13, 8, 513,8,5 in a circle in the third example, 1313 will have 88 and 55 as neighbors, but 13\ge 8 + 513≥8+5.
There is no solution in the fourth example.
题意:
给你n个数字,将这些数按一定的顺序排成一个圈,问是否能形成这样一个圈:其中任意一个数都小于两边的数之和。输出yes或no。
思路:
将所有数降序排列,将第一个和第二个数互换顺序,只要判断当前第二个数是否小于左右数之和即可。
代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[1000000];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
ll n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n,cmp);
if(a[1]+a[2]>a[0])
{
cout<<"YES"<<endl;
cout<<a[1]<<" "<<a[0]<<" "<<a[2]<<" ";
for(int i=3;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
else
cout<<"NO"<<endl;
}
C-Candies!
Consider a sequence of digits of length 2^k2k [a_1, a_2, \ldots, a_{2^k}][a1,a2,…,a2k]. We perform the following operation with it: replace pairs (a_{2i+1}, a_{2i+2})(a2i+1,a2i+2) with (a_{2i+1} + a_{2i+2})\bmod 10(a2i+1+a2i+2)mod10 for 0\le i<2^{k-1}0≤i<2k−1. For every ii where a_{2i+1} + a_{2i+2}\ge 10a2i+1+a2i+2≥10 we get a candy! As a result, we will get a sequence of length 2^{k-1}2k−1.
Less formally, we partition sequence of length 2^k2k into 2^{k-1}2k−1 pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth \ldots…, the last pair consists of the (2^k-12k−1)-th and (2^k2k)-th numbers. For every pair such that sum of numbers in it is at least 1010, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by 1010 (and don't change the order of the numbers).
Perform this operation with a resulting array until it becomes of length 11. Let f([a_1, a_2, \ldots, a_{2^k}])f([a1,a2,…,a2k]) denote the number of candies we get in this process.
For example: if the starting sequence is [8, 7, 3, 1, 7, 0, 9, 4][8,7,3,1,7,0,9,4] then:
After the first operation the sequence becomes [(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10][(8+7)mod10,(3+1)mod10,(7+0)mod10,(9+4)mod10] == [5, 4, 7, 3][5,4,7,3], and we get 22 candies as 8 + 7 \ge 108+7≥10 and 9 + 4 \ge 109+4≥10.
After the second operation the sequence becomes [(5 + 4)\bmod 10, (7 + 3)\bmod 10][(5+4)mod10,(7+3)mod10] == [9, 0][9,0], and we get one more candy as 7 + 3 \ge 107+3≥10.
After the final operation sequence becomes [(9 + 0) \bmod 10][(9+0)mod10] == [9][9].
Therefore, f([8, 7, 3, 1, 7, 0, 9, 4]) = 3f([8,7,3,1,7,0,9,4])=3 as we got 33 candies in total.
You are given a sequence of digits of length nn s_1, s_2, \ldots s_ns1,s2,…sn. You have to answer qq queries of the form (l_i, r_i)(li,ri), where for ii-th query you have to output f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])f([sli,sli+1,…,sri]). It is guaranteed that r_i-l_i+1ri−li+1 is of form 2^k2k for some nonnegative integer kk.
Input
The first line contains a single integer nn (1 \le n \le 10^51≤n≤105) — the length of the sequence.
The second line contains nn digits s_1, s_2, \ldots, s_ns1,s2,…,sn (0 \le s_i \le 90≤si≤9).
The third line contains a single integer qq (1 \le q \le 10^51≤q≤105) — the number of queries.
Each of the next qq lines contains two integers l_ili, r_iri (1 \le l_i \le r_i \le n1≤li≤ri≤n) — ii-th query. It is guaranteed that r_i-l_i+1ri−li+1 is a nonnegative integer power of 22.
Output
Output qq lines, in ii-th line output single integer — f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])f([sli,sli+1,…,sri]), answer to the ii-th query.
Examples
Input
8 8 7 3 1 7 0 9 4 3 1 8 2 5 7 7
Output
3 1 0
Input
6 0 1 2 3 3 5 3 1 2 1 4 3 6
Output
0 0 1
Note
The first example illustrates an example from the statement.
f([7, 3, 1, 7]) = 1f([7,3,1,7])=1: sequence of operations is [7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10][7,3,1,7]→[(7+3)mod10,(1+7)mod10] == [0, 8][0,8] and one candy as 7 + 3 \ge 107+3≥10 \to→ [(0 + 8) \bmod 10][(0+8)mod10] == [8][8], so we get only 11 candy.
f([9]) = 0f([9])=0 as we don't perform operations with it.
题意:
给你n个数,q次询问,每次询问输入一个下标区间[l,r],对区间内的数一直进行两两合并操作:将a1,a2合并成(a1+a2)%10,如果a1+a2>=10则ans+=1;直到只剩一个数;每次询问输出ans。
思路:
通过前缀和得到区间内的数求和除以10输出
代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll a[1000000];
ll b[1000000];
int main()
{
ll n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i+1]=b[i]+a[i];
}
ll q;
cin>>q;
while(q--)
{
ll l,r;
cin>>l>>r;
cout<<(b[r]-b[l-1])/10<<endl;
}
}