美文网首页
Educational Codeforces Round 78

Educational Codeforces Round 78

作者: frans4x | 来源:发表于2019-12-29 21:46 被阅读0次

    Educational Codeforces Round 78 C - Berry Jam

    题目

    Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n jars of strawberry and blueberry jam.

    All the 2n jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly n jars to his left and n jars to his right.

    For example, the basement might look like this: img

    Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right.

    Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same.

    For example, this might be the result:

    He has eaten 1 jar to his left and img

    then 5 jars to his right. There remained exactly 3 full jars of both strawberry and blueberry jam. Jars are numbered from 1 to 2n from left to right, so Karlsson initially stands between jars n and n+1.

    What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left?

    Your program should answer t independent test cases.

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

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

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

    It is guaranteed that the sum of n over all test cases does not exceed 105.

    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

    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n24" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 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</pre>

    output

    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 6
    0
    6
    2</pre>

    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 6 jars so that there remain 0 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 1 jar of both jams.

    题意

    可以从中间往左连续吃几罐,从中间往右连续吃几罐,最少一共吃多少可以使剩下的红蓝罐数目相等


    思路

    这个题需要换成1-1序列之后,求出一个前缀和与后缀和相加等于0的位置,后缀和可以由前缀和相减得到,所以我们只计算一个前缀和;枚举右边吃了多少,对于每一个后缀和查找是否有和他互为相反数的前缀和(用一个map来记录前一半数的前缀和等于某个值时出现的位置,后缀和用前缀和相减就可以了。然后从中间向后遍历,遍历的值减去最后前缀和的值,然后看看map里面是否有这个值,如果有计算一下需要吃几个,每次取最小的结果就可以了),最后就是注意边界问题,因为可能往左往右一个不吃;

    <pre mdtype="fences" cid="n70" lang="" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e5+10;
    int sum[maxn2];
    void solve(){
    int n;
    cin >> n;
    map<int, int> pos;
    pos[0] = 0; //往左全部吃完
    memset(sum, 0, sizeof(sum));
    for(int i=1; i<=n
    2; i++){
    int a;
    cin >> a;
    if(a == 2)
    a = -1;
    sum[i] = sum[i-1] + a;
    if(i <= n)
    pos[sum[i]] = i;
    }
    int ans = n2;
    for(int i=n; i<=n
    2; i++){
    auto it = pos.find(sum[i]-sum[2*n]);
    if(it != pos.end()){
    ans = min(i-it->second, ans);
    }
    }
    cout << ans << endl;
    }
    int main(){
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--){
    solve();
    }
    return 0;
    }</pre>

    我们用sum[i]-sum[n*2],是因为前缀合计算的是 1比2多了几个,我们这样减了一下刚好是2比1多了几个。如果前面刚好有1比2多的数量等于后面从i这个位置2比1多的数量,那么把中间的这部分吃了就刚好。我们这样顺序遍历下最后取最小值就可以了。

    相关文章

      网友评论

          本文标题:Educational Codeforces Round 78

          本文链接:https://www.haomeiwen.com/subject/wjyuoctx.html