@codeforces - 674G@ Choosing Ads
@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的更多相关文章
- Codeforces 643G - Choosing Ads(线段树)
Codeforces 题目传送门 & 洛谷题目传送门 首先考虑 \(p>50\) 的时候怎么处理,也就是求一个区间的绝对众数.我们知道众数这个东西是不能用线段树直接维护的,因为对于区间 ...
- 题解-CF643G Choosing Ads
CF643G Choosing Ads \(n\) 和 \(m\) 和 \(p\) 和序列 \(a_i(1\le i\le n)\).\(m\) 种如下操作: 1 l r id 令 \(i\in[l, ...
- 「CF643G」 Choosing Ads
「CF643G」 Choosing Ads 传送门 如果你知道摩尔投票法可以扩展事实上是个一眼题,又好写又好调. 首先摩尔投票法是用来求众数定义为超过所有数个数一半的数的一个算法. 大致算法流程: 将 ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces 219D Choosing Capital for Treeland
http://codeforces.com/problemset/problem/219/D 题目大意: 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达 ...
- (纪念第一道完全自己想的树DP)CodeForces 219D Choosing Capital for Treeland
Choosing Capital for Treeland time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 219D Choosing Capit
题目链接:http://codeforces.com/contest/219/problem/D 题目大意: 给定一个n个节点的数和连接n个节点的n - 1条有向边,现在要选定一个节点作为起始节点,从 ...
- Codeforces 219D - Choosing Capital for Treeland(树形dp)
http://codeforces.com/problemset/problem/219/D 题意 给一颗树但边是单向边,求至少旋转多少条单向边的方向,可以使得树上有一点可以到达树上任意一点,若有多个 ...
- Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
随机推荐
- 利用js实现 禁用浏览器后退| 去除上一个历史记录链接
也是查找了好多资料才找到的,这种方式,可以消除 后退的所有动作.包括 键盘.鼠标手势等产生的后退动作. <script language="javascript"> / ...
- select 下拉框样式修改 option文字居右
select { direction: rtl; /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的sele ...
- vue实现elment 可编辑树形控件
功能实现程度: elment树形控件,已具备新增.删除功能,此项目,添加编辑功能,新增时可对节点命名,也可直接给已添加的节点修改名字 先看效果: 控件有所改动,如加输入框,输入关键字过滤,节点图标变换 ...
- CodeChef - TELEPORT
题目链接:https://vjudge.net/problem/CodeChef-TELEPORT 题目大意: 有\(Q\)个指令,指令为:\(+\) \(x\) \(y\)(在二维平面内添加一个点, ...
- SQL Server存储过程模拟HTTP请求POST和GET协议
/****** Object: StoredProcedure [dbo].[sp_http_get] Script Date: 05/23/2020 15:47:09 ******/ SET ANS ...
- VUE添加class绑定
class class的样式应用四种 1.数组用法 显示用标识符v-bind :class="['red','thin']" 2. ...
- Xilinx的DocNav文件导航中的文档开头字母缩写都是什么意思?
在安装Xilinx的开发软件后都会附带安装一个用于查阅Xilinx技术文档的文件导航工具DocNav. 在DocNav中可以找到几乎所有对我们开发Xilinx FPGA有用的技术文档,其中的文档数量更 ...
- Mybaties 的缓存
1.mybaties 默认会话是一级缓存,没有开启二级缓存.开启方式在mapper文件中设置<cache /> 2.<cache eviction="LRU" f ...
- Cypress系列(1)- Window下安装 Cypress 并打开
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 系统要求 Cypress 是一个被安装在 ...
- Pycharm虚拟环境的使用
Pycharm虚拟环境的使用 pycharm创建项目的时候,一定勾选inhert global site-packages.不然系统的库就没法用了. 打开Project Interpreters页面: ...