zoukankan      html  css  js  c++  java
  • Army Creation

    As you might remember from our previous rounds, Vova really likes computer games. Now he is playing a strategy game known as Rage of Empires.

    In the game Vova can hire n different warriors; ith warrior has the type ai. Vova wants to create a balanced army hiring some subset of warriors. An army is called balanced if for each type of warrior present in the game there are not more than k warriors of this type in the army. Of course, Vova wants his army to be as large as possible.

    To make things more complicated, Vova has to consider q different plans of creating his army. ith plan allows him to hire only warriors whose numbers are not less than li and not greater than ri.

    Help Vova to determine the largest size of a balanced army for each plan.

    Be aware that the plans are given in a modified way. See input section for details.

    Input

    The first line contains two integers n and k (1 ≤ n, k ≤ 100000).

    The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 100000).

    The third line contains one integer q (1 ≤ q ≤ 100000).

    Then q lines follow. ith line contains two numbers xi and yi which represent ith plan (1 ≤ xi, yi ≤ n).

    You have to keep track of the answer to the last plan (let's call it last). In the beginning last = 0. Then to restore values of li and ri for the ith plan, you have to do the following:

    1. li = ((xi + lastmod n) + 1;
    2. ri = ((yi + lastmod n) + 1;
    3. If li > ri, swap li and ri.
    Output

    Print q numbers. ith number must be equal to the maximum size of a balanced army when considering ith plan.

    Example
    Input
    Copy
    6 2
    1 1 1 2 2 2
    5
    1 6
    4 3
    1 1
    2 6
    2 6
    Output
    Copy
    2
    4
    1
    3
    2
    题解:b [ i ] 表示从 i 开始数 k 个和 a [ i ] 相同颜色的数的位置。那么在[ l , r ]区间内只有 b [ i ] 大于 r 的数才能被选中,等价于 [ l , r ] 区间内有多少个数大于r。套上主席树就能使用前缀和。算b[i]时要注意一下!!
    注意:
    void Cal(){
      for ( int i = n ; i >= 1 ; i --){
        pos[ a[ i ] ].push_back( i );
        int k = poa[ a[ i ] ].size();
        if ( k >= m) b[ i ] = pos[ a[ i ] ][ k - m ];
        else b[ i ] = n + 1;
      }
    }
    这样写会WA,因为长度刚好m的那个位置不会统计。以第一个case来说,b [ ] = { 2 , 3  , 7 ,5 , 6 ,7 },当计算 [ 1 , 6 ]时,显然 b [ 2 ]是不会被统计的。
    #pragma warning(disable:4996)
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define ll long long 
    #define mem(arr,in) memset(arr,in,sizeof(arr))
    using namespace std;
    
    const int maxn = 1e5 + 5;
    
    int n, m, q, cnt;
    int root[maxn], a[maxn], b[maxn];
    
    struct node { int l, r, sum; } T[maxn * 40];
    
    vector<int> pos[maxn];
    
    void Cal()
    {
        for (int i = n; i >= 1; i--)
        {
            int sz = pos[a[i]].size();               
            if (sz<m)
                b[i] = n + 1;
            else
                b[i] = pos[a[i]][sz - m];
            pos[a[i]].push_back(i);   //这句放在开头会WA!!!
        }
    }

    void Update(int l, int r, int &rt, int pre, int p) { T[++cnt] = T[pre], T[cnt].sum++, rt = cnt; if (l == r) return; int mid = (l + r) >> 1; if (mid >= p) Update(l, mid, T[rt].l, T[pre].l, p); else Update(mid + 1, r, T[rt].r, T[pre].r, p); } int Query(int l, int r, int rt, int L, int R) { if (l > R || r < L) return 0; if (L <= l && r <= R) return T[rt].sum; int mid = (l + r) >> 1; int ans = 0; ans += Query(l, mid, T[rt].l, L, R); ans += Query(mid + 1, r, T[rt].r, L, R); return ans; } int main() { while (scanf("%d%d", &n, &m) != EOF) { cnt = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); Cal(); for (int i = 1; i <= n; i++) Update(1, n + 1, root[i], root[i - 1], b[i]); scanf("%d", &q); int l, r, ans, last = 0; for (int i = 1; i <= q; i++) { scanf("%d%d", &l, &r); l = ((l + last) % n) + 1; r = ((r + last) % n) + 1; if (l > r) swap(l, r); ans = Query(1, n + 1, root[r], r + 1, n + 1) - Query(1, n + 1, root[l - 1], r + 1, n + 1); last = ans; printf("%d ", ans); } } return 0; }
  • 相关阅读:
    Wireshark抓包原理(ARP劫持、MAC泛洪)及数据流追踪和图像抓取(二)
    Wireshark抓包原理(ARP劫持、MAC泛洪)及数据流追踪和图像抓取(二)
    Pythonlog() 函数
    Rabbitmq+sockjs+stomp.js前端的使用
    alter table t_user alter column create_date set default CURRENT_TIMESTAMP; 报错(idea生成的sql)
    eldatepicker日期控件日期少一天
    实际开发中String转换为json串作为入参发生"JSON parse error:Cannot deserialize value of type Date......not a valid解决
    Cannot deserialize value of type `java.util.Date` from String
    ProtoBuf试用与JSON的比较
    TCP粘包/拆包问题
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9053607.html
Copyright © 2011-2022 走看看