10.29 正睿停课训练 Day11
2018.10.29 正睿停课训练 Day11
一场rating排名从11掉到40+ ==。掉就掉吧
状态很迷 全程写T1的。。随机算法。。(一开始就想错了,误以为它正确性很高)
T2有想法但没调出来
T3有50暴力但是直接没看==
A 线段树什么的最讨厌了(思路 DFS)
容易发现线段树上区间\([l,r]\)的父节点只有\(4\)种情况:\([l,r+len-1],[l,r+len],[l-len-1,r],[l-len,r]\)。
其实也比较好想,因为只需对父节点区间长度的奇偶性分类讨论一下。
那么我们可以直接从给定区间\([l,r]\)不断枚举父节点,直到找到一个根节点\([0,n]\)?
显然只有\(log\)层,那这样是\(4^{\log n}=n^2\)的?注意到有限制为\((\frac{l}{l-r+1})^2\leq2000\),所以\(n^2\)是可过的。(好吧我也不太明白为啥是\(\frac{l}{l-r+1}\)?)
注意要剪枝,不去搜明显不属于线段树上的区间(不然还是暴力分)。
//371ms 504kb
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
typedef long long LL;
int Ans;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
void DFS(int l,int r)
{
if(l<0||r>=Ans) return;
if(!l) {Ans=r; return;}
int len=r-l+1;
if(l-len>=2*len||!(l-len))DFS(l-len,r);
if(l-len>=2*(len+1)||!(l-len-1)) DFS(l-len-1,r);
if(l>2*len) DFS(l,r+len);
if(l>2*(len-1)) DFS(l,r+len-1);
}
int main()
{
for(int T=read(); T--; )
{
int l=read(),r=read(),lim=read();
if(l==r) {printf("%d\n",r); continue;}
Ans=lim+1, DFS(l,r), printf("%d\n",Ans>lim?-1:Ans);
}
return 0;
}
B 已经没有什么好害怕的了(差分 前缀和)
将每个配对的括号表示成\([l,r)\)的形式。
首先可以将区间\([l,r)\)加一。
然后对于左端点\(l'\)在\(r\)位置的匹配括号对,显然可以将\([l,r)\)的贡献都加到\([l',r')\)上;
对于右端点\(r'\)在\(l\)位置的匹配括号对,显然也可以将\([l,r)\)的贡献都加到\([l',r')\)上。
然后就可以神奇差分了。。然后没看懂。。学套路吧。。
//899ms 21408kb
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#define gc() getchar()
#define mod 1000000007
#define Mod(x) x>=mod&&(x-=mod)
#define Add(x,v) (x+=v)>=mod&&(x-=mod)
typedef long long LL;
const int N=1e6+5;
int sk[N],L[N],R[N],sum[N],delta[N];
LL sum2[N];
char s[N];
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
int main()
{
for(int T=read(),n; T--; )
{
memset(L,0,sizeof L);
memset(R,0,sizeof R);
memset(sum,0,sizeof sum);
memset(delta,0,sizeof delta);
scanf("%s",s+1), n=strlen(s+1);
int top=0;
for(int i=1; i<=n; ++i)
if(s[i]=='(') sk[++top]=i;
else if(top) L[i+1]=sk[top], R[sk[top--]]=i+1;
for(int i=n+1; i; --i) sum[L[i]]+=sum[i]+1;
for(int i=1; i<=n; ++i) delta[R[i]]+=delta[i]+1;
for(int i=1; i<=n; ++i) sum2[i]=sum2[i-1]+sum[i]-delta[i];
LL ans=0;
for(int i=1; i<=n; ++i) ans+=sum2[i]*i%mod;
printf("%lld\n",ans);
}
return 0;
}
C 我才不是萝莉控呢(DP 贪心 哈夫曼树)
显然可以\(n^2\)DP。我们可以从\((1,1)\)DP到\((n,1)\)这样DP就只有两种转移了(不需要考虑下取整),即\(f[i][j]=\min\{f[i-1][j+1],\ f[i][(j+1)/2]\}\)。
如果你熟悉哈夫曼树,会发现这就类似哈夫曼树的DP转移。(哈夫曼树的DP方法见这儿,也是\(n^2\)的)
不是类似,把哈夫曼树的DP过程倒过来就是这个题的走路方式了。
所以本题等价于求哈夫曼树。用堆就可以\(O(n\log n)\)解决了。
因为每次合并出的东西单调的,所以二叉哈夫曼树也可以\(O(n)\)解决(见代码)。
//48ms 1552kb
#include <queue>
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 300000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e5+5;
int n,tot,pa,pb,A[N],B[N<<1];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int Get()
{
return (pb>tot||A[pa]<=B[pb])?A[pa++]:B[pb++];
}
int main()
{
for(int T=read(); T--; )
{
n=read(),tot=0,pa=pb=1;
for(int i=n; i; --i) A[i]=read();
LL ans=0; A[n+1]=0x7fffffff;
for(int i=1,x,y; i<n; ++i)
x=Get(),y=Get(),B[++tot]=x+y,ans+=x+y;
printf("%lld\n",ans);
}
return 0;
}
考试代码
A
#include <map>
#include <cstdio>
#include <cctype>
#include <algorithm>
#define gc() getchar()
#define LIM 5000000
typedef long long LL;
const int N=5e6+5;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
bool Query(int l,int r,int L,int R)
{
if(l==L&&r==R) return 1;
if((L<l&&R>=l)||(L<=r&&R>r)||R<l||L>r||l==r) return 0;
// if(L<=l&&r<=R) return 0;
int m=(LL)l+r>>1;
return Query(l,m,L,R)||Query(m+1,r,L,R);
}
void Violence(int l,int r,int lim)
{
for(int i=r; i<=lim; ++i)
if(Query(0,i,l,r)) {printf("%d\n",i); return;}
puts("-1");
}
//inline int Getpos(int x)
//{
// return x>65536?bit[x>>16]:bit[x];
//}
inline int Rand()
{
return (rand()<<16)|rand();
}
bool Solve(int L,int R,int lim)
{
// std::map<int,bool> vis;
int ans=lim+1;
for(int suc=0,mod=lim+1-R,fail=0; suc<=50000&&ans>R; ++suc)
{
int p=rand()%(ans-R)+R;
// if(vis[p])
// {
// if(++fail>=150000) break;
// continue;
// }
// vis[p]=1;
if(Query(0,p,L,R))
{
ans=std::min(ans,p);
for(int i=1; p>>i>=R; ++i)
if(Query(0,p>>i,L,R)) ans=std::min(ans,p>>i), ++suc;
else break;
}
}
if(ans<=lim)
{
printf("%d\n",ans);
return 1;
}
return 0;
}/*
3
521 535 1196
608 624 3828
304 314 3650
1
990 1755 1080952540
540 555 2308
464 475 2833
*/
int main()
{
freopen("tree.in","r",stdin);
freopen("my.out","w",stdout);
// for(int i=1; i<=65536; ++i)
// for(int j=16; ~j; --j)
// if(i>>j&1) {bit[i]=j; break;}
int t=rand(); srand(t);
for(int T=read(),t=1; t<=T; ++t)
{
int L=read(),R=read(),lim=read();
if(R>lim) {puts("-1"); continue;}
if(lim<=1000) {Violence(L,R,lim); continue;}
if(!Solve(L,R,lim)) puts("-1");
}
return 0;
}
10.29 正睿停课训练 Day11的更多相关文章
- 10.31 正睿停课训练 Day13
目录 2018.10.31 正睿停课训练 Day13 A Poker(期望) B Label(高斯消元) C Coin(二分图染色 博弈) 考试代码 A(打表) B 2018.10.31 正睿停课训练 ...
- 10.30 正睿停课训练 Day12
目录 2018.10.30 正睿停课训练 Day12 A 强军战歌(DP 树状数组 容斥) B 当那一天来临(思路) C 假如战争今天爆发(贪心) 考试代码 B C 2018.10.30 正睿停课训练 ...
- 10.25 正睿停课训练 Day9
目录 2018.10.25 正睿停课训练 Day9 A 数独(思路 DP) B 红绿灯(最短路Dijkstra) C 轰炸(计算几何 圆并) 考试代码 B C 2018.10.25 正睿停课训练 Da ...
- 10.24 正睿停课训练 Day8 AM
目录 2018.10.24 正睿停课训练 Day8 AM A 棒棒糖(组合) B 彩虹糖(思路 博弈) C 泡泡糖(DP) 考试代码 A B C 2018.10.24 正睿停课训练 Day8 AM 期 ...
- 10.23 正睿停课训练 Day7
目录 2018.10.23 正睿停课训练 Day7 A 矩形(组合) B 翻转(思路) C 求和(思路 三元环计数) 考试代码 B1 B2 C 2018.10.23 正睿停课训练 Day7 期望得分: ...
- 11.6 正睿停课训练 Day17
目录 2018.11.6 正睿停课训练 Day17 A chinese(思路 计数) B physics(单调队列/剪枝 DP) C chemistry(期望 DP) 考试代码 A B C 2018. ...
- 11.5 正睿停课训练 Day16
目录 2018.11.5 正睿停课训练 Day16 A 道路规划(思路) B 逻辑判断(枚举 位运算/DP 高维前缀和) C 区间(贪心/树状数组) 考试代码 A B C 2018.11.5 正睿停课 ...
- 11.2 正睿停课训练 Day15
目录 2018.11.2 正睿停课训练 Day15 A 郁闷的小G(二分) B 小G的树(树形DP) C 数的距离(思路) 考试代码 B C 2018.11.2 正睿停课训练 Day15 时间:3.5 ...
- 11.1 正睿停课训练 Day14
目录 2018.11.1 正睿停课训练 Day14 A 字符串 B 取数游戏(贪心) C 魔方(模拟) 考试代码 B C 2018.11.1 正睿停课训练 Day14 时间:3.5h 期望得分:100 ...
随机推荐
- MFC_CFileDialog_选择单一文件
场景 选择单一文件 技术点 CFileDialog CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, ...
- MCS-51单片机存储地址空间划分
1.前言 MCS-51的存储器有片内RAM.片外RAM 和 ROM 三个空间. MCS-51单片机在物理结构上有四个存储空间 1.片内程序存储器(片内ROM)2.片外程序存储器(片外ROM)3.片内数 ...
- PYTHON-UDP
1.TCP 和 UDP 发送数据时的流程 ***** 解释 为何TCP是可靠的 是因为发送数据后必须收到确认包 2. UDP的模板代码 ***** 1.UDP协议: (数据报协议) 特点: 无连接 优 ...
- OCM_第十六天课程:Section7 —》GI 及 ASM 安装配置 _安装 GRID 软件/创建和管理 ASM 磁盘组/创建和管理 ASM 实例
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- HTTP2.0 简明笔记
前言 RFC2616发布以来,一直是互联网发展的基石.HTTP协议也成为了可以在任何领域使用的核心协议,基于这个协议人们设计和部署了越来越多的应用.HTTP的简单本质是其快速发展的关键,但随着越来越多 ...
- linux下的文件结构
linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令/dev 设备特殊文件/etc 系统管理和配置文件/etc/rc.d 启动的配置文件和脚本/home 用户主目录的基点,比如 ...
- poj2481树状数组解二维偏序
按区间r降序排列,r相同按照l升序排列,两个区间相同时特判一下即可 /* 给定n个闭区间[l,r],如果对区间[li,ri],[lj,rj]来说, 有区间j严格包含(两个边界不能同时重合)在区间i内, ...
- (四)CXF处理JavaBean以及复合类型
前面讲的是处理简单类型,今天这里来讲下CXF处理JavaBean以及复合类型,比如集合: 这里实例是客户端传一个JavaBean,服务器端返回集合类型: 在原来的项目实例基础上,我们先创建一个实体类U ...
- ERP发货系统的修改(四十三)
产品添加批号后相应修改产品库存表中对应批次产品的数量: /// <summary> /// 产品添加批号后相应修改产品库存表中对应批次产品的数量 /// </summary> ...
- LINQ学习之旅(二)
一:查询表达式(LINQ)简介 LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译 ...