@description@

给定长度为 n 的序列,m 次询问以及参数 p。共有两类询问:

(1)"1 l r id":将区间 [l, r] 的颜色改成 id。

(2)"2 l r":对于区间 [l, r],输出不超过 100/p 种颜色,要求在区间内占比超过 >= p% 的颜色都应该被输出。

点我查看原题

@solution@

看了好几遍题意才看懂那个奇怪的输出方法。。。

其实就是让你找到另一个判定条件,使得题目给的判定条件是你所找的判定条件的充分条件。。。

考虑 p >= 51 时,该问题是寻找区间占比 > 1/2 的数的充分条件。

这个问题。。。其实是一个经典问题(网上搜得到很多相关博客)。

寻找区间占比 > 1/2 的数(如果存在),只需要每次选中 2 个不同的数并同时消去,最后剩下的数就是所寻找的数。

类似地,可以大胆猜测寻找区间占比 > 1/k 的数(如果存在):只需要每次选中 k 个不同的数并同时消去,最后所剩下的 k-1 个数就是所寻找的数。

正确性?假如原先有 n 个数,其中 r 个为 x,且 r/n > 1/k。

假如你所选的 k 个数不包含 x,则新的占比 r/(n - k) > 1/k(这是显然的);否则,新的占比 (r - 1)/(n - k) > 1/k(这也是显然的)。

即操作一次后占比依然 > 1/k。那么归纳到 n < k 时,就可以得证了。

注意到只有前提 “存在这样的数”,两个过程才是等价的。

也就是说找出来的数不一定是区间占比 > 1/k 的数,但区间占比 > 1/k 的数一定会被找出。两者形成充分条件。

这也就用上了题目那奇怪的条件。

先找到最小的整数 k 使得 x > 1/k 的充分条件为 x >= p%。

考虑用数据结构(线段树)加速这一过程:我们对于每个结点只维护区间内删完后剩下的 <= k-1 个数以及它们的出现次数 cnt。

合并两个区间的信息时,只需要把一个区间的数往另一个区间插入。

分情况简单讨论一下,当数的种类数 = k 的时候就把这些数的出现次数 cnt 同时减去出现次数最少的数的出现次数 min{cnt}。

@accepted code@

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; typedef pair<int, int> pii;
#define mp make_pair
#define fi first
#define se second const int MAXN = 150000; int n, m, p, q; struct node{
pii a[5]; int s;
node() {s = 0;}
node(pii x) {a[0] = x, s = 1;}
friend node insert(const node &A, const pii &x) {
node B = A;
for(int i=0;i<B.s;i++)
if( B.a[i].se == x.se ) {
B.a[i].fi += x.fi;
return B;
}
if( B.s < q )
B.a[B.s++] = x;
else {
int mn = 0;
for(int i=0;i<B.s;i++)
if( B.a[i].fi < B.a[mn].fi ) mn = i;
if( B.a[mn].fi >= x.fi ) {
for(int i=0;i<B.s;i++)
B.a[i].fi -= x.fi;
}
else {
pii y = B.a[mn]; B.a[mn] = x;
for(int i=0;i<B.s;i++)
B.a[i].fi -= y.fi;
}
}
return B;
}
friend node merge(const node &A, const node &B) {
if( A.s == 0 ) return B;
if( B.s == 0 ) return A;
node C = A;
for(int i=0;i<B.s;i++)
C = insert(C, B.a[i]);
return C;
}
}; int a[MAXN + 5];
struct segtree{
#define lch (x << 1)
#define rch (x << 1 | 1) int le[4*MAXN + 5], ri[4*MAXN + 5], tg[4*MAXN + 5];
node nd[4*MAXN + 5];
void pushup(int x) {
nd[x] = merge(nd[lch], nd[rch]);
}
void pushdown(int x) {
if( tg[x] ) {
nd[lch] = node(mp(ri[lch]-le[lch]+1, tg[x]));
nd[rch] = node(mp(ri[rch]-le[rch]+1, tg[x]));
tg[lch] = tg[rch] = tg[x], tg[x] = 0;
}
}
void build(int x, int l, int r) {
le[x] = l, ri[x] = r, tg[x] = 0;
if( l == r ) {
nd[x] = node(mp(1, a[l]));
return ;
}
int m = (l + r) >> 1;
build(lch, l, m), build(rch, m + 1, r);
pushup(x);
}
void modify(int x, int l, int r, int k) {
if( l > ri[x] || r < le[x] )
return ;
if( l <= le[x] && ri[x] <= r ) {
nd[x] = node(mp(ri[x]-le[x]+1, k)), tg[x] = k;
return ;
}
pushdown(x);
modify(lch, l, r, k), modify(rch, l, r, k);
pushup(x);
}
node query(int x, int l, int r) {
if( l > ri[x] || r < le[x] )
return node();
if( l <= le[x] && ri[x] <= r )
return nd[x];
pushdown(x);
return merge(query(lch, l, r), query(rch, l, r));
}
}T; void solve(int l, int r) {
node nd = T.query(1, l, r);
printf("%d", nd.s);
for(int i=0;i<nd.s;i++)
printf(" %d", nd.a[i].se);
puts("");
} int main() {
scanf("%d%d%d", &n, &m, &p), q = 100 / p;
for(int i=1;i<=n;i++) scanf("%d", &a[i]);
T.build(1, 1, n);
for(int i=1;i<=m;i++) {
int op; scanf("%d", &op);
if( op == 1 ) {
int l, r, id; scanf("%d%d%d", &l, &r, &id);
T.modify(1, l, r, id);
}
else {
int l, r; scanf("%d%d", &l, &r);
solve(l, r);
}
}
}

@details@

没错我也开始做集训队作业了。

什么?集训队作业一共 150 道题?抱歉告辞告辞。

没见过经典模型的我一开始本来想的是随机抽样选点(大概率选中占比更多的颜色) + 判定,结果不是 WA 就是 TLE。

这样来来回回 15 次过后(途中还发现系统库的rand函数好像有些点无论选什么种子都找不到)我决定还是看看题解。

然后我就死了。。。

@codeforces - 674G@ Choosing Ads的更多相关文章

  1. Codeforces 643G - Choosing Ads(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(p>50\) 的时候怎么处理,也就是求一个区间的绝对众数.我们知道众数这个东西是不能用线段树直接维护的,因为对于区间 ...

  2. 题解-CF643G Choosing Ads

    CF643G Choosing Ads \(n\) 和 \(m\) 和 \(p\) 和序列 \(a_i(1\le i\le n)\).\(m\) 种如下操作: 1 l r id 令 \(i\in[l, ...

  3. 「CF643G」 Choosing Ads

    「CF643G」 Choosing Ads 传送门 如果你知道摩尔投票法可以扩展事实上是个一眼题,又好写又好调. 首先摩尔投票法是用来求众数定义为超过所有数个数一半的数的一个算法. 大致算法流程: 将 ...

  4. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  5. Codeforces 219D Choosing Capital for Treeland

    http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...

  6. (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland

    Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  7. CodeForces 219D Choosing Capit

    题目链接:http://codeforces.com/contest/219/problem/D 题目大意: 给定一个n个节点的数和连接n个节点的n - 1条有向边,现在要选定一个节点作为起始节点,从 ...

  8. Codeforces 219D - Choosing Capital for Treeland(树形dp)

    http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...

  9. Codeforces 219D Choosing Capital for Treeland:Tree dp

    题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...

随机推荐

  1. 移除项目中的UIWebView

    1,AFN升级4.0 2,代码中搜索UIWebView移除相关文件 3,检查库是否使用的UIWebView 参考 https://www.jianshu.com/p/3a645500d461

  2. PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  3. PAT-1056 Mice and Rice (分组决胜问题)

    1056. Mice and Rice Mice and Rice is the name of a programming contest in which each programmer must ...

  4. ShoneSharp语言(S#)软件更新13.7版

    ShoneSharp语言(S#)编辑解析运行器 软件更新13.7版 作者:Shone 近期在写博客过程中对S#进行增强,把语法规则更新到2.0版,并同步更新软件到ShoneSharp.13.7.exe ...

  5. Vue接入百度地图显示及相关操作

    Vue接入百度地图的API 首先你要有一个百度地图的key就是CK 申请网址:http://lbsyun.baidu.com/index.php?title=jspopular/guide/getke ...

  6. 自定义reaml创建使用实现认证

    注意清空shiro.ini 创建User对象 package cn.zys.Bean; public class User { private Integer id; private String u ...

  7. APP定位元素之UiSelector

    1.UiSelector 类介绍 功能:通过各种属性与节点关系定位组件 操作步骤:找到对象->操作对象 2.四中匹配关系的介绍 (1)完全匹配 (2)包含匹配 (3)正则匹配 (4)起始匹 例子 ...

  8. [Firefox附加组件]0002.添加菜单项

    Add-onSDK 还不能为火狐浏览器提供一个API添加新的菜单项.但它是可扩展的设计,所以任何人都可以建立和发布模块,使用插件开发者.大牛埃里克沃尔德写的MenuItems模块,能够使我们很方便的添 ...

  9. [Objective-C] 006_Protocol(协议)

    学过java的同学都知道Interface(接口),那么在Objective-C中有没有接口呢?其实 Objective-C中用Protocol(协议)来实现的,在Objective-C具体怎么用,我 ...

  10. 使用Burpsuite对手机抓包的配置

    之前使用dSploit的时候就一直在想怎么对手机进行抓包分析,前两天使用了Burpsuite神器,发现通过简单的配置就可以抓手机app的数据包了,进而分析手机app的流量. 配置环境: 1.win7下 ...