NOI.AC NOIP模拟赛 第五场 游记
NOI.AC NOIP模拟赛 第五场 游记
count
题目大意:
长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出现一次。
对于每一个正整数\(k=1,..,n+1\),求出的本质不同的长度为\(k\)的子序列的数量。对\(10^9+7\)取模。
思路:
由于只会有一个数会重复,因此考虑重复的这个数取\(0\)个、\(1\)个和\(2\)个的情况,用组合数直接算即可。
源代码:
#include<cstdio>
#include<cctype>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=1e5+2,mod=1e9+7;
int a[N],fac[N],ifac[N],last[N];
void exgcd(const int &a,const int &b,int &x,int &y) {
if(!b) {
x=1,y=0;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int inv(const int &x) {
int ret,tmp;
exgcd(x,mod,ret,tmp);
return ret;
}
inline int C(const int &n,const int &m) {
if(n<m) return 0;
return (int64)fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
int main() {
const int n=getint();
for(register int i=fac[0]=1;i<=n+1;i++) {
fac[i]=(int64)fac[i-1]*i%mod;
}
ifac[n+1]=inv(fac[n+1]);
for(register int i=n+1;i>=1;i--) {
ifac[i-1]=(int64)ifac[i]*i%mod;
}
int l;
for(register int i=1;i<=n+1;i++) {
a[i]=getint();
if(last[a[i]]) l=i-last[a[i]];
last[a[i]]=i;
}
for(register int i=1;i<=n+1;i++) {
printf("%d\n",(C(n+1,i)-C(n-l,i-1)+mod)%mod);
}
return 0;
}
delete
题目大意:
长度为\(n(n\le10^6)\)的序列\(A\),从中删去恰好\(k\)个元素(右边的元素往左边移动),记\(cnt\)为新序列中\(A_i=i\)的元素个数。求\(cnt\)的最大值。
思路:
将\(A\)以\(i-A_i\)为第一关键字、\(A_i\)为第二关键字排序。就转化成了LIS问题。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e6+1;
int n,m;
std::pair<int,int> a[N];
class FenwickTree {
private:
int val[N];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) {
val[p]=std::max(val[p],x);
}
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) {
ret=std::max(ret,val[p]);
}
return ret;
}
};
FenwickTree bit;
int main() {
n=getint(),m=n-getint();
for(register int i=1;i<=n;i++) {
const int &x=getint();
a[i]=std::make_pair(i-x,x);
}
std::sort(&a[1],&a[n]+1);
int ans=0;
for(register int i=1;i<=n;i++) {
if(a[i].first<0) continue;
if(a[i].second>n) continue;
const int tmp=bit.query(a[i].second-1)+1;
if(tmp>m) continue;
bit.modify(a[i].second,tmp);
if(a[i].first<=n-m&&a[i].second<=m) ans=std::max(ans,tmp);
}
printf("%d\n",ans);
return 0;
}
power
题目大意:
一棵包含\(n(n\le10^5)\)个节点的树,对于这棵树中的一个连通块,它的能量为它拥有的节点中编号连续的最长的一段。求大小不超过\(k\)的连通块的最大能量值。
思路:
包含若干点的最小连通块的大小是这些点按照DFS序排序后,(相邻点之间距离和+首尾两点距离)/2+1。
使用尺取法枚举连续段,同时用set按照DFS序维护这些结点,同时维护每一时刻连通块的大小即可。
时间复杂度\(\mathcal O(n\log n)\)。
源代码:
#include<set>
#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,logN=17;
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int dfn[N],id[N],tmp,dep[N],anc[N][logN];
inline int lg2(const float &x) {
return ((unsigned&)x>>23&255)-127;
}
void dfs(const int &x,const int &par) {
anc[x][0]=par;
dep[x]=dep[par]+1;
id[dfn[x]=++dfn[0]]=x;
for(register int i=1;i<=lg2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-1]][i-1];
}
for(auto &y:e[x]) {
if(y==par) continue;
dfs(y,x);
}
}
inline int lca(int x,int y) {
if(dep[x]<dep[y]) std::swap(x,y);
for(register int i=lg2(dep[x]-dep[y]);i>=0;i--) {
if(dep[anc[x][i]]>=dep[y]) x=anc[x][i];
}
for(register int i=lg2(dep[x]);i>=0;i--) {
if(anc[x][i]!=anc[y][i]) {
x=anc[x][i];
y=anc[y][i];
}
}
return x==y?x:anc[x][0];
}
inline int dist(const int &x,const int &y) {
const int z=lca(x,y);
return dep[x]+dep[y]-dep[z]*2;
}
std::set<int> set;
inline void ins(const int &x) {
const auto p=--set.lower_bound(x);
const auto q=set.upper_bound(x);
if(*p!=INT_MIN) tmp+=dist(id[x],id[*p]);
if(*q!=INT_MAX) tmp+=dist(id[x],id[*q]);
if(*p!=INT_MIN&&*q!=INT_MAX) tmp-=dist(id[*p],id[*q]);
set.insert(x);
}
inline void del(const int &x) {
const auto p=--set.lower_bound(x);
const auto q=set.upper_bound(x);
if(*p!=INT_MIN) tmp-=dist(id[x],id[*p]);
if(*q!=INT_MAX) tmp-=dist(id[x],id[*q]);
if(*p!=INT_MIN&&*q!=INT_MAX) tmp+=dist(id[*p],id[*q]);
set.erase(x);
}
inline int dist2() {
const int x=*++set.begin();
const int y=*++set.rbegin();
if(x==INT_MIN||y==INT_MAX) return 0;
return dist(id[x],id[y]);
}
int main() {
const int n=getint(),k=getint();
for(register int i=1;i<n;i++) {
add_edge(getint(),getint());
}
dfs(1,0);
int ans=0;
set.insert(INT_MAX);
set.insert(INT_MIN);
ins(1);
for(register int p=1,q=1;q<=n;ins(dfn[++q])) {
while((tmp+dist2())/2+1>k) del(dfn[p++]);
ans=std::max(ans,q-p+1);
}
printf("%d\n",ans);
return 0;
}
NOI.AC NOIP模拟赛 第五场 游记的更多相关文章
- NOI.AC NOIP模拟赛 第六场 游记
NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...
- NOI.AC NOIP模拟赛 第四场 补记
NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...
- NOI.AC NOIP模拟赛 第三场 补记
NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
- NOI.AC NOIP模拟赛 第一场 补记
NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...
- [NOI.AC 2018NOIP模拟赛 第三场 ] 染色 解题报告 (DP)
题目链接:http://noi.ac/contest/12/problem/37 题目: 小W收到了一张纸带,纸带上有 n个位置.现在他想把这个纸带染色,他一共有 m 种颜色,每个位置都可以染任意颜色 ...
- NOI.AC NOIP模拟赛R3解题报告
心路历程 预计得分:\(100+100+50=250\) 实际得分:\(10 +100 +50 = 160\) 三道原题,真好.T2做过,T1写了个错误思路,T3写了写50分状压dp. 整场考试实际在 ...
- NOI.AC WC模拟赛
4C(容斥) http://noi.ac/contest/56/problem/25 同时交换一行或一列对答案显然没有影响,于是将行列均从大到小排序,每次处理限制相同的一段行列(呈一个L形). 问题变 ...
- 东北育才 NOIP模拟赛第1场
终于400了.这套题很鬼畜.两道贪心. GRACE sort过后,不能直接统计,本人毫无多想,相同的直接放在一起.结果太多人AC. SUM sigma+异或和(可使用前缀和处理),本人毫无考虑乱MOD ...
随机推荐
- bzoj千题计划264:bzoj3884: 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...
- CodeForces 1059B
Description Student Andrey has been skipping physical education lessons for the whole term, and now ...
- 第9月第15天 设计模式 adapter mvc
1. 有一道iOS面试题,iOS中都有什么设计模式?很少有答案说包括adapter. gof 书中adapter模式有以下内容: 实现: ... b ) 使 用 代 理 对 象 在这种方法中, T r ...
- rstful登陆认证并检查session是否过期
一:restful用户视图 #!/usr/bin/env python # -*- coding:UTF-8 -*- # Author:Leslie-x from users import model ...
- C# 常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...
- 在vue-cli下读取模拟数据请求服务器
写此记录时vue脚手架的webpack是3.6.0 此文章方法亦可用于vue-cli3,直接在vue.config.js里面添加 本记录使用vue-resource,先安装: cnpm install ...
- nodejs 配置服务自启动
1安装包 输入以下命令,安装需要的包 npm install node-windows -g 2编写自启动js 在目标server.js目录下新建auto_start_nodejs.js文件,将以下j ...
- ispoweroftwo 判断2的次幂【转】
转自:https://www.cnblogs.com/troublelost/p/5236391.html 首先结果是: public bool IsPowerOfTwo(int n) { if(n& ...
- MYSQL 添加外键报错
2014年6月16日 10:48:51 出错的部分提示摘录: #1452 - Cannot add or update a child row: a foreign key constraint fa ...
- Python_oldboy_自动化运维之路_面向对象(十)
面向对象编程 OOP编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率 ,另外,基于面向 ...