LOJ6285 block entry columns 9

LOJ6285 number of columns of the block entry 9

label

  • Block basis

Foreword

Concise meaning of the questions

  • To a sequence, you need a minimum number of public inquiries in the interval

Thinking

  • First of all to write violence. If the data range is from 1000 but group asked how much, how would you write? To solve this problem. We can open a dp [] [] array, dp [i] [j] represents the number of the congregation interval [i, j] in. The specific process is to enumerate all of the left point, and then enumerate the right point, right point to the right each time it is updated answer.
  • Violence will write, you think about this topic. On the mode actually has such a property: If a known set the mode is x, and then set on a mode of a set of either b x, b is either a number of . Then we can block the pleasurable, first-out according to the above mentioned pre-dp [i] [j] The mode of the i-th block to the j-th block, then each query, the answer is either that the middle piece the answer, either the number of those left and right sides are not monolithic.
  • So now the problem is in the number of seeking the number of left and right sides are not monolithic. This is a classic dichotomy problem. Directly opening a Vector [] all recording position of each number appears, the number of enumerated violence, and then find the binary position r and l subtracting it.
  • The last is that some large number, it can be discrete.
  • There is the size of the block 80 to open into AC

Precautions

  • no

to sum up

  • If the mode is a known set of x, and then set on a mode of a set of either b x, b is either a number of
  • Seeking the number of section numbers appear to be two points and the position of the recording.

AC Code

#pragma GCC optimize(2)
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
#include<unordered_map>
using namespace std;

const int maxn = 1e5 + 10;

int read()
{
    int x = 0, f = 1; char ch = getchar();
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}

int n, a[maxn];
int pos[maxn], len;
pair<int, int> dp[5000][5000];
vector<int> rec[maxn];//记录每个数出现的所有位置

vector<int> ls;
int mp[maxn];

int ask(int l, int r)
{
    int cur_max = dp[pos[l] + 1][pos[r] - 1].first, max_val = dp[pos[l] + 1][pos[r] - 1].second;

    for (int i = l; i <= min(pos[l] * len, r); i++)
    {
        auto& b = rec[a[i]];
        int cnt = upper_bound(b.begin(), b.end(), r) - lower_bound(b.begin(), b.end(), l);
        if (cnt > cur_max || (cnt == cur_max && a[i] < max_val))
            cur_max = cnt, max_val = a[i];
    }

    if (pos[l] != pos[r])
    {
        for (int i = pos[r] * len - len + 1; i <= r; i++)
        {
            auto& b = rec[a[i]];
            int cnt = upper_bound(b.begin(), b.end(), r) - lower_bound(b.begin(), b.end(), l);
            if (cnt > cur_max || (cnt == cur_max && a[i] < max_val))
                cur_max = cnt, max_val = a[i];
        }
    }

    return max_val;
}

void solve() 
{
    scanf("%d", &n);
    len = 80;
    for (int i = 1; i <= n; i++)
        a[i] = read(), pos[i] = (i - 1) / len + 1, ls.push_back(a[i]);

    sort(ls.begin(), ls.end());
    int ls_len = unique(ls.begin(), ls.end()) - ls.begin();

    for (int i = 1; i <= n; i++)
    {
        int t = lower_bound(ls.begin(), ls.begin() + ls_len, a[i]) - ls.begin() + 1;
        mp[t] = a[i];
        a[i] = t;
        rec[a[i]].push_back(i);
    }

    for (auto& it : rec)
        sort(it.begin(), it.end());

    //预处理dp
    int cnt[maxn];//记录一下每个数出现的次数
    for (int i = 1; i <= pos[n]; i++)//枚举起始块
    {
        int cur_max = -1e9, max_val;//分别是当前最多出现的次数、最多出现次数的那个数
        memset(cnt, 0, sizeof cnt);
        for (int j = i * len - len + 1; j <= n; j++)//枚举从第i块开始的每一个数
        {
            cnt[a[j]]++;
            if (cnt[a[j]] > cur_max || (cnt[a[j]] == cur_max && a[j] < max_val))//a[j]出现的次数多,或者a[j]出现的次数一样但更小,就更新
                cur_max = cnt[a[j]], max_val = a[j];
            dp[i][pos[j]].first = cur_max, dp[i][pos[j]].second = max_val;
        }
    }


    for (int i = 1; i <= n; i++) 
    {
        int l, r;
        l = read(), r = read();

        printf("%d\n", mp[ask(l, r)]);
    }
}

int main() {
    freopen("Testin.txt", "r", stdin);
    freopen("Testout.txt", "w", stdout);
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/danzh/p/11366588.html