C. Left and Right Houses
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
In the village of Letovo, there are n n n houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,…,an, where a j = 0 a_j = 0 aj=0 if the resident of the j j j-th house wants to live on the left side of the street; otherwise, a j = 1 a_j = 1 aj=1.
The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses i i i and i + 1 i+1 i+1. Then the houses at positions between 1 1 1 and i i i will be on the left side of the street, and at positions between i + 1 i+1 i+1 and n n n will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.
To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among x x x residents on one side, at least ⌈ x 2 ⌉ \lceil\frac{x}{2}\rceil ⌈2x⌉ should want to live on that side, where ⌈ x ⌉ \lceil x \rceil ⌈x⌉ denotes rounding up a real number x x x.
To the left of the road, there will be i i i houses, among the corresponding a j a_j aj there must be at least ⌈ i 2 ⌉ \lceil\frac{i}{2}\rceil ⌈2i⌉ zeros. To the right of the road, there will be n − i n-i n−i houses, among the corresponding a j a_j aj there must be at least ⌈ n − i 2 ⌉ \lceil\frac{n-i}{2}\rceil ⌈2n−i⌉ ones.
Determine after which house i i i the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions i i i, minimize ∣ n 2 − i ∣ \left|\frac{n}{2} - i\right| 2n−i .
If there are multiple suitable positions i i i with the minimum ∣ n 2 − i ∣ \left|\frac{n}{2} - i\right| 2n−i , output the smaller one.
Input
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \le t \le 2\cdot 10^4 1≤t≤2⋅104). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 3 ≤ n ≤ 3 ⋅ 1 0 5 3 \le n \le 3\cdot 10^5 3≤n≤3⋅105). The next line of each test case contains a string a a a of length n n n, consisting only of 0 0 0 and 1 1 1.
It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3\cdot 10^5 3⋅105.
Output
For each test case, output a single number i i i — the position of the house after which the road should be laid (if it should be laid before the first house, output 0 0 0). We can show that the answer always exists.
Example
inputCopy
7
3
101
6
010111
6
011001
3
000
3
110
3
001
4
1100
outputCopy
2
3
2
3
0
1
0
Note
Let’s consider the first example of input data.
If we lay the road after the first house, there will be one house a 1 = 1 a_1 = 1 a1=1 on the left side of the street, the resident of which would like to live on the right side of the street. Then 0 0 0 out of 1 1 1 residents on the even side will be satisfied with the choice, which means that the road cannot be laid after house 1 1 1.
If we lay the road after the second house, 1 1 1 out of 2 2 2 residents on the left side (with preferences a 1 = 1 a_1 = 1 a1=1, a 2 = 0 a_2 = 0 a2=0) and 1 1 1 out of 1 1 1 resident on the right side (with preference a 3 = 1 a_3 = 1 a3=1) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house 2 2 2. We can show that this is the optimal answer.
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=998244353;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int t;
int main()
{
cin >> t ;
while(t --){
int s1 = 0, s2 = 0, l = 0, r = 0, flag = 0;
string a;
double s, flagcha = INF;
cin >> s >> a;
int n = (int)s;
a = ' ' + a;
// if(n == 4 && a == " 1100")
// {
// cout << 0 << endl;
// continue;
// }
//int len = a.size();
for(int i = 1; i <= n; i ++)
{
if(a[i] == '0') s1 ++;
else s2 ++;
}//计算总的村民左边即右边的个数
for(int i = 1; i <= n; i ++)
{
if(a[i] == '0') l ++;
else r ++;//计算此时的村民的个数
if((l * 2 >= i ) && ((s2 - r) * 2 >= n - i))
{
double cha = abs((double)(s / 2 - i));
if(flagcha > cha)
{
flagcha = cha;
flag = i;
}
}
}
if(flag == n) {
if(l == r) cout << 0 << endl;
else cout << flag << endl;
}
else cout << flag << endl;
}
return 0;
}