NOIP做题练习(day5)
A - 中位数图
题解
先找出题意中的\(b\)所在的位置。
再以这个位置为中心,向右\(for\)一遍有多少个大于/小于该数的数
大于就\(++cs\) 小于就\(--cs\)。
因为这个数是中位数,所以大于它的数小于它的数的数量是一样的。
然后每次以\(cs\)为下标的数\(++\)。
因为\(cs\)可能小于零,
所以需要将\(cs\)加上\(100000\)后再\(++\)。
统计答案时直接加上下标为\(0-cs+100000\)的数的值即可。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#define int long long
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, b, a[100003], p[100003], c, s[100003], cs, cj, sum, ans, pp[200003];
signed main()
{
//File("A");
n = gi(), b = gi();
for (itn i = 1; i <= n; i+=1)
{
a[i] = gi();
if (a[i] == b) c = i;
}
for (int i = c; i <= n; i+=1)
{
if (a[i] > b) ++cs;
if (a[i] < b) --cs;
++pp[cs + 100000];
}
for (int i = c; i >= 1; i-=1)
{
if (a[i] > b) ++cj;
if (a[i] < b) --cj;
ans = ans + pp[0 - cj + 100000];
}
printf("%lld\n", ans);
return 0;
}
B - 树
题解
直接暴力\(dfs\)即可\(AC\)。
数据很水啊……
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m, s, a[100003], fa[100003], ans;
int tot, head[300003], nxt[300003], ver[300003];
inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void dfs(int u, int fa, int w)
{
if (w > s) return;
if (w == s) {++ans; return;}
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == fa) continue;
dfs(v, u, w + a[v]);
}
}
int main()
{
//File("B");
n = gi(), s = gi();
for (int i = 1; i <= n; i+=1) a[i] = gi();
for (int i = 1; i < n; i+=1)
{
int u = gi(), v = gI();
add(u, v);
fa[v] = u;
}
for (int i = 1; i <= n; i+=1)
{
dfs(i, fa[i], a[i]);
}
printf("%d\n", ans);
return 0;
}
C - 松鼠的新家
题解
倍增\(LCA+\)树上差分一遍过。
注意要判断两个房间是不是存在祖先关系。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <vector>
#define gI gi
#define itn int
#define File(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
itn n, a[300003], dep[300003], w[300003], fa[300003][23], sum, ans, tot, head[1200003], nxt[1200003], ver[1200003];
int f[300003], logn;
inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
void dfs(int u, int baba, int step)
{
dep[u] = step; fa[u][0] = baba;
for (int i = 1; i <= logn; i+=1)
{
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == baba) continue;
dfs(v, u, step + 1);
}
}
inline void getlca(int u, int v)
{
if (dep[u] < dep[v]) swap(u, v);
int x = u, y = v;
while (dep[u] > dep[v])
{
for (int i = logn; i >= 0; i-=1)
{
if (dep[fa[u][i]] > dep[v]) u = fa[u][i];
}
u = fa[u][0];
}
if (u == v)
{
++w[x], --w[fa[u][0]]; return;
}
for (itn i = logn; i >= 0; i-=1)
{
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
}
u = fa[u][0];
++w[x], ++w[y], --w[u], --w[fa[u][0]];
}
void getans(int u)
{
f[u] = w[u];
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == fa[u][0]) continue;
getans(v);
f[u] = f[u] + f[v];
}
}
int main()
{
//File("C");
n = gi();
while ((1 << logn) < n) ++logn;
for (int i = 1; i <= n; i+=1)
{
a[i] = gi();
}
for (int i = 1; i < n; i+=1)
{
int u = gi(), v = gi();
add(u, v); add(v, u);
}
add(0, a[1]);
dfs(0, 0, 1);
for (int i = 1; i < n; i+=1)
{
getlca(a[i], a[i + 1]);
}
getans(a[1]);
for (int i = 1; i <= n; i+=1)
{
printf("%d\n", (i == a[1]) ? (f[i]) : (f[i] - 1));
}
return 0;
}
总结
为什么每次时间内\(0AC\),时间结束之后再切题呢?
思维要更活跃一点啊。
NOIP做题练习(day5)的更多相关文章
- noip做题记录+挑战一句话题解?
因为灵巧实在太弱辽不得不做点noip续下命QQAQQQ 2018 积木大赛/铺设道路 傻逼原题? 然后傻逼的我居然检查了半天是不是有陷阱最后花了差不多一个小时才做掉我做过的原题...真的傻逼了我:( ...
- NOIP做题练习(day2)
A - Reign 题面 题解 最大子段和+\(DP\). 预处理两个数组: \(p[i]\)表示 \(i\) 之前的最大子段和. \(l[i]\)表示 \(i\) 之后的最大子段和. 最后直接输出即 ...
- NOIP做题练习(day1)
A - Xenny and Alternating Tasks 题面 题解 枚举第一天是谁做,将两个答案取\(min\)即可. 代码 #include <iostream> #includ ...
- $NOIp$做题记录
虽然去年做了挺多了也写了篇一句话题解了但一年过去也忘得差不多了$kk$ 所以重新来整理下$kk$ $2018(4/6$ [X]积木大赛 大概讲下$O(n)$的数学方法. 我是从分治类比来的$QwQ$. ...
- NOIP做题练习(day4)
A - 同花顺 题面 题解 30分做法 爆搜即可. 60分做法 去重+贪心. 100分做法 去重+贪心后,我们要寻找一段符合条件的最长同花上升子序列 \(L\),\(n-L\) 即为所求的答案. 首先 ...
- NOIP做题练习(day3)
A - 军队 问题描述 给定一个有 \(n\) 个队伍的人组成的序列,第 \(i\) 个队伍 \(i\) 有 \(s[i]\)个人组成,一个 \(l\) 到 \(r\)的子序列是合法的,当且仅当\(( ...
- NOIP初赛:完善程序做题技巧
最近写的文章好像还很多的.那么今天我们来讨论NOIP初赛的题型--完善程序.完善程序相对是比较难的题目了.全卷100分,完善程序占了大概26分,占比非常大.如果和英语考试试卷做比较,相当于首字母填空( ...
- [日记&做题记录]-Noip2016提高组复赛 倒数十天
写这篇博客的时候有点激动 为了让自己不颓 还是写写日记 存存模板 Nov.8 2016 今天早上买了两个蛋挞 吃了一个 然后就做数论(前天晚上还是想放弃数论 但是昨天被数论虐了 woc noip模拟赛 ...
- CodeM美团点评编程大赛复赛 做题感悟&题解
[T1] [简要题意] 长度为N的括号序列,随机确定括号的方向:对于一个已确定的序列,每次消除相邻的左右括号(右左不行),消除后可以进一步合并和消除直到不能消为止.求剩下的括号的期望.\(N \l ...
随机推荐
- py 二级习题(重新输出文本-----每行一句话)
#需要的一小段文本 txt = "人生得意须尽欢,莫使金樽空对月.天生我才必有用,千金散尽还复来." #对文本进行分割,转换成列表形式 def txt_split(txt): li ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...
- 使用_slots_变量限制class实例能添加的属性
如果我们想要限制实例的属性怎么办?比如,只允许对Student实例添加name和age属性. 那么我们在Student类里面增添_slots_变量 例如: class Student(object): ...
- P1341 无序字母对【欧拉路径】- Hierholzer模板
P1341 无序字母对 提交 24.87k 通过 6.80k 时间限制 1.00s 内存限制 125.00MB 题目提供者yeszy 难度提高+/省选- 历史分数100 提交记录 查看题解 标签 福建 ...
- 中间件c10k问题
中间件c10k问题 没有使用iocp/epoll/kqueue通讯的中间件,中间件就算部署在拥有多核CPU的强大服务器上,最头痛的问题是C10K问题. 中间件没有办法通过优化程序,提升CPU利用率来处 ...
- GaussDB T 单机模式手工建库
目录 你需要知道的 创建文件夹 编辑参数文件 将数据库启动到 NOMOUNT 状态 连接实例查询状态 创建数据库PROD1 如何连接原来 GAUSS 数据库 相关文章 GaussDB T 单机搭建 G ...
- tomcat-embeded-core源码编译
使用spring-boot创建web工程时,默认采用embeded tomcat作为容器,实际使用过程中,可能会需要对其中的某些功能做微调,而tomcat又没有给出预留配 ,这时就需要对tomcat- ...
- git 命令 总结
1.添加所有文件 git add . 2.添加某个文件 git add filename 3.commit 注释 git commit -m'commit 注释' 4.修改commit 注释 git ...
- 异常处理_python
一.异常处理格式: name=[1,2]data={}try: name[3] data['name']except (KeyError,IndexError) as e : #捕捉指定的几个错误类型 ...
- HP大中华区总裁孙振耀退休感言
HP大中华区总裁孙振耀退休感言 : 如果这篇文章没有分享给你,那是我的错. 如果这篇文章分享给你了,你却没有读,继续走弯路的你不要怪我. 如果你看了这篇文章,只读了一半你就说没时间了,说明你已经是个“ ...