zoukankan      html  css  js  c++  java
  • Codeforces 840D Destiny

    (mathcal{Translate})

    给定长度为 (n) 的序列 (a_1,a_2,...,a_n),共 (q) 次询问,每次询问给定 (l,r,k),询问区间 (a_l,a_{l+1},...,a_{r}) 的最小的出现次数严格大于 (frac{r-l+1}{k}) 的数的值。

    (1leq n,q leq 3 imes 10^5,1leq a_ileq n,1leq lleq rleq n,2leq kleq 5)

    (mathcal{Solution})

    总思路:根号分治卡常数。

    (M) 为一个分界线,询问区间小于等于 (M) 的直接暴力扫一遍找,可以使用 unordered_map 做到一次扫 (mathcal{O}(M))。对于大于 (M) 的询问区间,通过记录某些出现次数可以当做答案的数的前缀出现次数和,遍历这些数,如果出现次数合法就更新答案。

    那么那些出现次数可以当做答案的数有多少个呢?

    只有区间大于等于 (M) 的才需要查询,也就是说区间长度最少为 (M+1) (后文当作 (M),影响不大),而这个询问长度最少是 (M),询问的 (k)(5) 的时候需要的出现次数最少,需要的出现次数为 (frac{M}{5}),这些数最多有 (frac{n}{frac{M}{5}}=frac{5n}{M})(q) 次查询的复杂度为 (mathcal{O}(q imes max{M,frac{5n}{M}}))

    考虑让里面那个 (max),尽可能小,画出 (y=x-frac{5n}{x})图表可知,(x=pm1224.745)(y=0),也就是 (M) 取值为 (1225) 最优,但是注意前缀和的空间消耗是很大的,即使选择记录下这个数在只需要记录的那些数中的排名,空间消耗也达到了 (n imes frac{5n}{M}) 的级别,会超出空间限制,然而调大 (M) 过多又会超出时间限制。

    减少空间使用可以考虑前缀出现次数用 vector 来存储,动态开内存,用多少开多少。(M) 我调到 (2200) 差不多是可以的。

    (M_2=frac{M}{5})

    时间复杂度 (mathcal{O}(nMlog M_2+q imes max(M,M_2)))

    如果 (M) 值取的比较好的话就是 (mathcal{O}(n^{1.5}log{sqrt{n}+qsqrt{n}}))

    (mathcal{Code})

    借用了 CYJian 的快读。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    #include<string>
    #include<unordered_map>
    #define ll long long
    #define pb push_back
    #define re register
    using namespace std;
    #define Min(x, y) (x < y ? x : y)
    namespace io {
    	const int __SIZE = (1 << 21) + 1;
    	char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
    	#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    	inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
    	inline void gc (char &x) { x = Gc(); }
    	inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
    	inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
    	inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';)  __c = Gc();
    		for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
    	template <class I> inline bool gi (I &x) { _eof = 0;
    		for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
    		for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
    	template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
    		while (x) qu[++ qr] = x % 10 + '0',  x /= 10; while (qr) pc (qu[qr --]); }
    	struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
    } using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;
    const int N = 300010;
    int M = 2200, M2 = M/5 - 1;
    int n, q;
    int a[N], cnt[N], num;
    vector<int>sum[N];
    vector<int>vec;
    unordered_map<int, int>vis;
    int main() {
    	gi(n); gi(q);
    	for(re int i = 1; i <= n; ++i) {
    		gi(a[i]), ++cnt[a[i]];
    		if(cnt[a[i]] == M2 + 1) {
    			vec.pb(a[i]);
    			++num;
    		}
    	}
    	sort(vec.begin(), vec.end());
    	re int x;
    	for(int j = 0; j <= num; ++j) sum[0].pb(0);
    	for(re int i = 1; i <= n; ++i) {
    		sum[i].pb(0);
    		for(re int j = 1; j <= num; ++j) {
    			sum[i].pb(0);
    			sum[i][j] += sum[i-1][j];
    		}
    		if(cnt[a[i]] > M2) {
    			x = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin() + 1;
    			++sum[i][x];
    		}
    	}
    	while(q--) {
    		int l, r, k, sumq = -1;
    		gi(l); gi(r); gi(k);
    		if(r - l + 1 <= M) {
    			vis.clear();
    			for(re int i = l; i <= r; ++i) {
    				x = ++vis[a[i]];
    				if(x > (r - l + 1) / k) sumq = sumq == -1 ? a[i] : Min(sumq, a[i]);
    			}
    		}
    		else {
    			for(int i = num; i >= 1; --i)
    				if(sum[r][i] - sum[l-1][i] > (r - l + 1) / k)
    					sumq = sumq == -1 ? vec[i-1] : Min(sumq, vec[i-1]);
    		}
    		print(sumq);pc('
    ');
    	}
    	return 0;
    }
    
  • 相关阅读:
    python总结4
    python中if __name__ == '__main__': 的解析
    matlab学习1
    phpstorm xdebug环境搭建
    uniapp 直播跳转小程序组件
    vue中异步函数async和await的用法
    TFS 2010安装配置(Advance)失败记录
    WIN2003 SMTP Service issue
    WIN2003 ftp server权限设置
    Discuz 7.2 SC UTF8设置
  • 原文地址:https://www.cnblogs.com/do-while-true/p/13882042.html
Copyright © 2011-2022 走看看