Nowcoder 提高 Day1
比赛链接
A 中位数(前缀和 二分)
额,确实没想到逼近...
然后写了n^2log的暴力,还CE了
只需要判断是否能有大于当前mid的中位数就好
这显然是可以二分的
代码
#include<bits/stdc++.h>
using namespace std;
#define li long long
#define gc getchar()
#define pc putchar
li read(){
li x = 0,y = 0,c = gc;
while(!isdigit(c)) y = c,c = gc;
while(isdigit(c)) x = (x << 1) + (x << 3) + (c ^ '0'),c = gc;
return y == '-' ? -x : x;
}
void print(li q){
if(q < 0){
pc('-');
q = -q;
}
if(q >= 10) print(q / 10);
pc(q % 10 + '0');
}
int n,m;
int a[100010],b[100010],c[100010],d[100010];
bool chk(int p){
int i,j;
for(i = 1;i <= n;++i) b[i] = a[i] >= p ? 1 : -1;
for(i = 1;i <= n;++i) c[i] = c[i - 1] + b[i];
for(i = 1;i <= n;++i) d[i] = min(d[i - 1],c[i]);
for(i = n,j = -n;i >= m;--i){
j = max(j,c[i]);
if(j - d[i - m] > 0) return 1;
}
return 0;
}
int main(){
int i,j;
n = read();m = read();
for(i = 1;i <= n;++i) a[i] = read();
int l = 1,r = 1000000000,mid,as = 1;
while(l <= r){
mid = l + r >> 1;
if(chk(mid)) as = mid,l = mid + 1;
else r = mid - 1;
}
print(as);
return 0;
}
B 数数字(数位DP)
数位DP不会
不过yy一下,写个暴力dp,map转移似乎就...过了
代码
#include<iostream>
#include<cstdio>
#include<map>
#define LL long long
using namespace std;
inline LL read() {
char c = getchar(); LL x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
LL L, R, L1, R1;
map<LL, LL> f[20];
LL s[21], num = 0;
LL dfs(int x, int lim, LL mul) {
if(x < 0) return 0;
if(x == 0) {
if(mul == -1) mul = 0;
return mul >= L1 && mul <= R1;
}
if((!lim) && (f[x].find(mul) != f[x].end())) return f[x][mul];
LL ans = 0;
for(int i = 0; i <= (lim ? s[x] : 9); i++) {
if(mul == -1) {
if(i == 0) ans += dfs(x - 1, 0, -1);//tag
else ans += dfs(x - 1, lim && (i == s[x]), i);
} else ans += dfs(x - 1, lim && (i == s[x]), i * mul);
}
if(!lim) f[x][mul] = ans;
return ans;
}
LL solve(LL x) {
if(x == -1) return 0;
num = 0;
while(x) s[++num] = x % 10, x /= 10;
return dfs(num, 1, -1);
}
int main() {
L = read(); R = read(); L1 = read(); R1 = read();
LL ans = solve(R) - solve(L - 1);
cout << ans;
return 0;
}
C 保护(dfs序 主席树)
额,主席树维护一下子树中点能覆盖的第k小深度..就没了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
struct sj{int to,next;}line[maxn*2];
int size,n,m,num,q,siz[maxn];
int head[maxn],id[maxn],tot,idx[maxn];
int a[maxn],b[maxn],c[maxn],T[maxn];
int L[maxn * 64],R[maxn * 64],sum[maxn * 64];
void add(int x,int y) {
line[++size].to=y;
line[size].next=head[x];
head[x]=size;
}
int old[maxn],deep[maxn];
int dad[maxn][27];
void dfs(int x,int fa = 0) {
siz[x] = 1;
++ num;
id[x]=num;
old[num]=x;
deep[x] = deep[fa] + 1;
for(int i=head[x];i;i=line[i].next) {
int tt=line[i].to;
if(!siz[tt]) {dad[tt][0] = x;
dfs(tt,x);
siz[x]+=siz[tt];
}
}
}
inline int lca(int x,int y) {
if(deep[x] > deep[y]) std::swap(x,y);
for(int i = 20;i >= 0;-- i) if(deep[dad[y][i]] >= deep[x]) y = dad[y][i];
if(x == y) return x;
for(int i = 20;i >= 0;-- i)
if(dad[x][i] != dad[y][i])
x = dad[x][i],y = dad[y][i];
return dad[x][0];
}
int build(int l,int r) {
int rt=++tot;
if(l!=r) {
int mid=(l+r)/2;
L[rt]=build(l,mid);
R[rt]=build(mid+1,r);
}
return rt;
}
int update(int pre,int l,int r,int x) {
int rt =++ tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre] + 1;
int mid=(l+r)/2;
if(l!=r) {
if (x<=mid) L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+1,r,x);
}
return rt;
}
int query(int x,int y,int l,int r,int k) {
if(l==r)return l;
int now=sum[L[y]]-sum[L[x]];
int mid=(l+r)/2;
if(now>=k) return query(L[x],L[y],l,mid,k);
else return query(R[x],R[y],mid+1,r,k-now);
}
vector<int> qq[maxn];
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++) {
int x,y; scanf("%d%d",&x,&y);
add(x,y); add(y,x);
}
dfs(1);
for(int i = 1;i <= 20;++ i)
for(int x = 1;x <= n;++ x) dad[x][i] = dad[dad[x][i - 1]][i - 1];
for(int u,v,i = 1;i <= m;++ i) {
scanf("%d%d",&u,&v);
int l = lca(u,v);
qq[u].push_back(deep[l]);
qq[v].push_back(deep[l]);
}
T[0]=build(1,n + 1);
for(int i=1;i <= n;i ++) {
if(qq[old[i]].size()) {
for(int j = 0;j < qq[old[i]].size();++ j) {
if(j == 0) T[i] = update(T[i - 1],1,n + 1,qq[old[i]][j]);
else T[i] = update(T[i],1,n + 1,qq[old[i]][j]);
}
}
else T[i] = update(T[i - 1],1,n + 1,n + 1);
}
scanf("%d",&q);
while(q--) {
int x,k;
scanf("%d%d",&x,&k);
int p=query(T[id[x]-1],T[id[x]+siz[x] - 1],1,n + 1,k);
printf("%d\n",std::max(deep[x] - p,0));
}
return 0;
}
Nowcoder 提高 Day1的更多相关文章
- Nowcoder 提高组练习赛-R7
Nowcoder 提高组练习赛-R7 https://www.nowcoder.com/acm/contest/179#question 中间空了两场,因为实在是太难了... 第五场的第二题好像还比较 ...
- nowcoder提高集训营第5场
凉 (比赛链接)[https://www.nowcoder.com/acm/contest/177#question] T1 (题目链接)[https://www.nowcoder.com/acm/c ...
- Nowcoder 提高组练习赛-R3
https://www.nowcoder.com/acm/contest/174#question 今天的题好难呀,只有94个人有分.然后我就爆零光荣 考到一半发现我们班要上物理课,还要去做物理实验( ...
- Nowcoder 提高组练习赛-R2
https://www.nowcoder.com/acm/contest/173#question T1:https://www.nowcoder.com/acm/contest/173/A 题意概述 ...
- Nowcoder 提高组练习赛-R1
https://www.nowcoder.com/acm/contest/172#question 单人报名300元,五人合报免费,于是就和学弟同学学长们组了一个三世同堂的队伍,高一的学长wzhqwq ...
- nowcoder 提高组模拟赛 选择题 解题报告
选择题 链接: https://www.nowcoder.com/acm/contest/178/B 来源:牛客网 题目描述 有一道选择题,有 \(a,b,c,d\) 四个选项. 现在有 \(n\) ...
- nowcoder 提高组模拟赛 最长路 解题报告
最长路 链接: https://www.nowcoder.com/acm/contest/178/A 来源:牛客网 题目描述 有一张 \(n\) 个点 \(m\) 条边的有向图,每条边上都带有一个字符 ...
- nowcoder提高组2题解
T1 化一下试子就ok code #include<cstdio> #include<algorithm> inline long long read() { long lon ...
- NOIP2018 游记 QAQ
写在前面: 本人初三党.NOIP前两个月不好好停课搞信竞愣是要搞文化课.于是,期中考与NOIP一起凉凉[微笑] 本人写的第一篇NOIP游记,各位大佬们随便看一看就好 Day -n 初赛71,竟然跟wx ...
随机推荐
- ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Generic timer
1.前言 2.generate timer 2.1 概述 提供了一个系统计数器,用来实时测量流逝的时间: 提供了一个虚拟计数器,用来测量某个虚拟机上流逝的虚拟时间: 定时器,每隔一段时间会触发事件,支 ...
- linux新内核的freeze框架以及意义【转】
转自:https://blog.csdn.net/dog250/article/details/5303442 linux的电源管理发展非常迅速,比如在挂起到内存的时候,系统会冻结住所有的进程,也就是 ...
- CentOS6.5运行yum报错:No module named yum
公司测试机环境不知道给我卸了什么包,导致yum运行报错状况: 报错情况: There was a problem importing one of the Python modulesrequired ...
- 005_awk案例实战
一.工作经验总结. (1)日志案例: 10.100.194.39 10.100.194.39 1019-03-16T11:01:04+08:00 www.uuwatch.com^^3FF91DE01B ...
- SPI、IIC、IIS、UART、CAN、SDIO、GPIO、USB总线协议
SPI.IIC.IIS.UART.CAN.SDIO.GPIO总线协议 SPI(Serial Peripheral Interface:串行外设接口)SPI总线由三条信号线组成:串行时钟(SCLK).串 ...
- Linux 文件查找命令详解
find命令 Linux find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将 ...
- 石头剪刀布(智能判断你是否赢了)(if判断和for)
- Windows下虚拟机安装Mac OS X —– VM12安装Mac OS X 10.11
____________________________________________________________________________________________________ ...
- JS中sort()方法原理及使用
说明 如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较. arra ...
- vue-cli 搭建的项目处理不同环境下请求不同域名的问题
使用 vue-cli 开发项目过程中, 根据开发环境和正式环境不同, 我们往往需要请求不同域名下的后台接口, 这时候, 该怎么去设置, 达到同一种写法可以根据环境不同而自动切换请求域名呢? 本文将会介 ...