NOIP2018 提高组题解
Day1
T1
据说是原题积木大赛,但是考场上蠢了,只会写数据结构,于是写了一个线段树$+$堆$+$贪心,先选出最小的,然后区间修改,然后把左右两端区间的最小值丢进堆里,不停从堆中去最小值更新即可(模拟题)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using std::pop_heap; using std::push_heap;
using std::greater; using std::min;
#define file(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout);
typedef long long ll;
template <typename T>
inline void read(T &x) {
x = 0; char ch = getchar(); int f = 1;
while(ch < '0' || ch > '9') { if(ch == '-') f = -f; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= f;
}
const int N = 1e5 + 10, LogN = 20, Inf = 1e9 + 7;
int n, d[N];
struct Range {
int minval, site, l, r;
inline bool operator < (const Range &a) const {
return minval < a.minval;
}
inline bool operator > (const Range &a) const {
return minval > a.minval;
}
};
Range val[N << 2]; int add[N << 2];
struct Heap {
Range h[N]; int siz;
void push(Range x) { h[++siz] = x, push_heap(&h[1], &h[siz + 1], greater<Range>()); }
void pop() { pop_heap(&h[1], &h[siz + 1], greater<Range>()), --siz; }
inline bool empty() { return siz == 0; }
inline int size() { return siz; }
inline Range top() { return h[1]; }
}q;
inline void pushup(int o, int lc, int rc) {
val[o] = min(val[lc], val[rc]);
}
inline void pushdown(int o, int lc, int rc) {
if(add[o]) {
val[lc].minval += add[o], val[rc].minval += add[o];
add[lc] += add[o], add[rc] += add[o], add[o] = 0;
}
}
void build(int o = 1, int l = 1, int r = n) {
if(l == r) { val[o] = (Range){d[l], l, l, l}; return ; }
int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1;
build(lc, l, mid), build(rc, mid + 1, r), pushup(o, lc, rc);
}
void modify(int ml, int mr, int k, int o = 1, int l = 1, int r = n) {
if(ml > mr) return ;
if(l >= ml && r <= mr) {
val[o].minval += k, add[o] += k;
return ;
} int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1;
pushdown(o, lc, rc);
if(ml <= mid) modify(ml, mr, k, lc, l, mid);
if(mr > mid) modify(ml, mr, k, rc, mid + 1, r);
pushup(o, lc, rc);
}
Range query(int ml, int mr, int o = 1, int l = 1, int r = n) {
if(ml > mr) return (Range){Inf,0,0,0};
if(l >= ml && r <= mr) return val[o];
int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1; Range val = (Range){Inf,0,0,0};
pushdown(o, lc, rc);
if(ml <= mid) val = query(ml, mr, lc, l, mid);
if(mr > mid) val = min(val, query(ml, mr, rc, mid + 1, r));
return val;
}
int main () {
file("road");
read(n);
for(int i = 1; i <= n; ++i) read(d[i]);
build();
int tmpn = n, ret = 0; Range now = val[1];
now.l = 1, now.r = n; q.push(now);
while(tmpn && q.size()) {
now = q.top(), q.pop(); --tmpn;
ret += now.minval;
modify(now.l, now.r, -now.minval);
Range l = query(now.l, now.site - 1), r = query(now.site + 1, now.r);
l.l = now.l, l.r = now.site - 1, r.l = now.site + 1, r.r = now.r;
q.push(l), q.push(r);
} printf("%d\n", ret);
return 0;
}
T2
不难发现,两个硬币系统是等价的当且仅当其中的某些硬币能被除自己以外的硬币凑出来。完全背包强制不选自己就行了。
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::max; using std::sort;
#define file(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout);
typedef long long ll;
template <typename T>
inline void read(T &x) {
x = 0; char ch = getchar(); int f = 1;
while(ch < '0' || ch > '9') { if(ch == '-') f = -f; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= f;
}
const int N = 1e2 + 10, M = 2.5e4 + 10;
int t, n, a[N], f[M];
int main () {
file("money");
read(t);
while(t--) {
read(n); int ret = 0, m = 0;
for(int i = 1; i <= n; ++i) read(a[i]), m = max(m, a[i]);
memset(f, 0, sizeof f), sort(&a[1], &a[n + 1]);
for(int i = 1; i <= n; ++i) {
for(int j = a[i] + 1; j <= m; ++j)
f[j] |= f[j - a[i]];
if(!f[a[i]]) {
++ret, f[a[i]] = 1;
for(int j = a[i]; j <= m; ++j)
f[j] |= f[j - a[i]];
}
}
printf("%d\n", ret);
}
return 0;
}
T3
显然,这种最小值最大可以二分答案,考虑如何$check$,不妨考虑树形$dp$,设$f[u]$表示在$u$的子树中选一条权值和最大的路径,对于一个子节点$v$,如果$f[v]+dis[u][v]$满足,显然可以选,然后在考虑不满足的情况,显然是选择两条路径拼接在一起,可以用$set$+二分搞(其实容易被卡常)。
#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::max; using std::min; using std::sort;
#define file(a) freopen(a".in", "r", stdin); freopen(a".out", "w", stdout);
typedef long long ll;
using std::multiset;
template <typename T>
inline void read(T &x) {
x = 0; char ch = getchar(); int f = 1;
while(ch < '0' || ch > '9') { if(ch == '-') f = -f; ch = getchar(); }
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
x *= f;
}
const int N = 5e4 + 10, Inf = 1e9 + 7;
int n, m, g, k;
int cnt, from[N], to[N << 1], dis[N << 1], nxt[N << 1];
int f[N], st[N]; multiset<int> s; multiset<int>::iterator it;
int l, r;
inline void addEdge(int u, int v, int w) {
to[++cnt] = v, nxt[cnt] = from[u], dis[cnt] = w, from[u] = cnt;
}
void doit (int u, int fa) {
for(int i = from[u]; i; i = nxt[i])
if(to[i] != fa) doit(to[i], u);
int top = 0;
for(int i = from[u]; i; i = nxt[i]) {
int v = to[i]; if(v == fa) continue;
f[v] += dis[i];
if(f[v] >= g) ++k;
else st[++top] = f[v];
} sort(&st[1], &st[top + 1]), s.clear();
for(int i = 1; i <= top; ++i) {
it = s.lower_bound(g - st[i]);
if(it != s.end()) s.erase(it), ++k;
else s.insert(st[i]);
}
f[u] = s.size() ? *s.rbegin() : 0;
}
int main () {
file("track");
read(n), read(m);
for(int i = 1, u, v, w; i < n; ++i) {
read(u), read(v), read(w), r += w;
addEdge(u, v, w), addEdge(v, u, w);
} r /= m; int ret = 0;
while(l <= r) {
g = (l + r) >> 1, k = 0;
doit(1, 0);
if(k >= m) ret = g, l = g + 1;
else r = g - 1;
} printf("%d\n", ret);
return 0;
}
NOIP2018 提高组题解的更多相关文章
- NOIP2018提高组题解
D1T1:铺设道路 回忆NOIP2013D2T1 积木大赛,发现这两题唯一的区别就是一个是造山一个是填坑,而把填坑的操作反序就是造山,所以可以直接使用那道题的方法. 具体方法是,从左到右每次考虑新的一 ...
- 【NOIP2018】提高组题解
[NOIP2018]提高组题解 其实就是把写过的打个包而已 道路铺设 货币系统 赛道修建 旅行 咕咕咕 咕咕咕
- NOIP2018提高组省一冲奖班模测训练(六)
NOIP2018提高组省一冲奖班模测训练(六) https://www.51nod.com/Contest/ContestDescription.html#!#contestId=80 20分钟AC掉 ...
- NOIP2018提高组省一冲奖班模测训练(五)
NOIP2018提高组省一冲奖班模测训练(五) http://www.51nod.com/Contest/ContestDescription.html#!#contestId=79 今天有点浪…… ...
- noip2010提高组题解
NOIP2010提高组题解 T1:机器翻译 题目大意:顺序输入n个数,有一个队列容量为m,遇到未出现元素入队,求入队次数. AC做法:直接开1000的队列模拟过程. T2:乌龟棋 题目大意:有长度为n ...
- NOIP 2014 提高组 题解
NOIP 2014 提高组 题解 No 1. 生活大爆炸版石头剪刀布 http://www.luogu.org/problem/show?pid=1328 这是道大水题,我都在想怎么会有人错了,没算法 ...
- NOIP 2001 提高组 题解
NOIP 2001 提高组 题解 No 1. 一元三次方程求解 https://vijos.org/p/1116 看见有人认真推导了求解公式,然后猥琐暴力过的同学们在一边偷笑~~~ 数据小 暴力枚举即 ...
- NOIP 2000 提高组 题解
NOIP2000 提高组 题解 No 1. 进制转换 https://www.rqnoj.cn/problem/295 水题 对于n和基数r, 每次用n mod r, 把余数按照逆序排列 注意 mod ...
- [NOIp2018提高组]旅行
[NOIp2018提高组]旅行: 题目大意: 一个\(n(n\le5000)\)个点,\(m(m\le n)\)条边的连通图.可以从任意一个点出发,前往任意一个相邻的未访问的结点,或沿着第一次来这个点 ...
随机推荐
- uva 1506 Largest Rectangle in a Histogram
Largest Rectangle in a Histogram http://acm.hdu.edu.cn/showproblem.php?pid=1506 Time Limit: 2000/100 ...
- 51Nod 1014 X^2 Mod P
注意潜在范围 x*x用long long #include <bits/stdc++.h> using namespace std; typedef long long LL; #defi ...
- [洛谷P2048] [NOI2010] 超级钢琴
洛谷题目链接:[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号 ...
- ZOJ3229 Shoot the Bullet [未AC]
Time Limit: 2 Seconds Memory Limit: 32768 KB Special Judge Gensokyo is a world which exist ...
- 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局
[题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
- 大聊Python----生产消费者模型
在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度. 为什么要使用生产者和消费者模式? 在线程世界里,生产者就是生产数 ...
- Winform GDI+
什么是GDI+ GDI (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface). 因为应用程序不能直 ...
- JavaScript字符串逆序
如何对字符串进行倒序呢?你首先想到的方法就是生成一个栈,从尾到头依次取出字符串中的字符压入栈中,然后把栈连接成字符串. var reverse = function( str ){ var stack ...
- input file 文件上传,js控制上传文件的大小和格式
文件上传一般是用jquery的uploadify,比较好用.后面会出文章介绍uploadify这个插件. 但是,有时候为了偷懒,直接就用input 的file进行文件和图片等的上传,input fil ...
- 【常见的SQL Server连接失败错误以及解决方法】
[常见的SQL Server连接失败错误以及解决方法] http://blog.csdn.net/feixianxxx/article/details/5523922 ADO连接SQL Server ...