(本部2定格)教育Codeforcesラウンド78 C.ベリージャム

カールソンは最近、家の地下室でベリージャムの瓶の巨大な株式を発見しました。より具体的には、あった  2つのNイチゴとブルーベリージャムの2Nジャー。

すべての  2つのN 2Nジャーは、一列に配置されています。地下への階段は、その行の真ん中に正確です。カールソンは、地下室に入ったときに、彼は正確に見ている  n個のn彼の左とに瓶  nは n個の瓶彼の右に。

例えば、地下室は、次のようになります。

彼はstarightforward男であること、彼はすぐにジャムを食べて起動します。1分に彼は彼の左への最初の非空瓶や彼の右に最初の非空瓶のいずれかを空にすることを選択します。

最後に、カールソンは、最後にフルイチゴとブルーベリージャムの瓶の量が同じになるべきことを決定しました。

たとえば、これは結果である可能性があります:

彼が食べた  1本の彼の左に1瓶をして、  5彼の右に5瓶を。 まさにそこに残っ  3イチゴとブルーベリージャムの両方の3フルジャー。

ジャーは、以下から番号付けされている  12 Nカールソンが最初ジャーとの間に立って、左から右へ、2N  N、N及び  N + 1、N + 1。

カールソンは、完全なイチゴとブルーベリージャムの瓶の等しい数が残っているように空にするために必要とされるjarファイルの最小数とは何ですか?

あなたのプログラムは答える必要があります  トントンの独立したテストケースを。

入力

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

The first line of each test case contains a single integer nn (1n1051≤n≤105).

The second line of each test case contains 2n2n integers a1,a2,,a2na1,a2,…,a2n (1ai21≤ai≤2) — ai=1ai=1 means that the ii-th jar from the left is a strawberry jam jar and ai=2ai=2 means that it is a blueberry jam jar.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case print the answer to it — the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left.

Example
input
Copy
4
6
1 1 1 2 2 1 2 1 2 1 1 2
2
1 2 1 2
3
1 1 1 1 1 1
2
2 1 1 1
output
Copy
6
0
6
2
Note

The picture from the statement describes the first test case.

In the second test case the number of strawberry and blueberry jam jars is already equal.

In the third test case Karlsson is required to eat all 66 jars so that there remain 00 jars of both jams.

In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave 11 jar of both jams.

 

 

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
//#include <unordered_map>
#include <map>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
//#include <multimap>
#define ll long long
#define inf 0x3f3f3f3
using namespace std;
const int mxn = 2e5+10;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ls now<<1,l,mid
#define rs now<<1|1,mid+1,r
#define lc now<<1
#define rc now<<1|1
#define upsum(now) rt[now].sum =rt[now<<1].sum + rt[now<<1|1].sum ;
#define upmx(now) rt[now].mx = max(rt[now<<1].mx , rt[now<<1|1].mx) ;
#define pb push_back
int n,m,k,t,mx,mn,l,r,dp[mxn],u,v,cost;
int l1[mxn>>1],l2[mxn>>1], r1[mxn>>1],r2[mxn>>1];
string str,ch;
int main()
{
    TLE;
    cin>>t;
    for(int tt=1; tt<=t; tt++)
    {
        cin>>n;
        map<int,int>mp;
        int ans = 0 ;
        mp[0] = n ;
        for(int i=0;i<n;i++)
        {
            cin>>dp[i];
            ans += ( dp[i]==1? -1: 1 );
            mp[ans] = n-1-i;
        }
        for(int i=n-1;i>=0;i--)
            cin>>dp[i];
        ans = 0 ; mn =mp[0]+n;
        for(int i=0;i<n;i++)
        {
            ans += ( dp[i]==1? -1: 1 );
            if(mp.count(-ans))
            {
                mn = min( mn , mp[-ans]+n-1-i );
            }
        }
        cout<<mn<<endl;
    }
    return 0 ;
}

 

おすすめ

転載: www.cnblogs.com/Shallow-dream/p/12076158.html