当前位置:网站首页>【动态规划百题强化计划】11~20(持续更新中)
【动态规划百题强化计划】11~20(持续更新中)
2022-07-27 03:56:00 【筱雨丶Colicsin】
暑期强化DP学习的一个计划,每天刷点cf,每日赛过活神仙(bushi)
题目都是cf里面的带有dp标签的,也有部分是贪心等简单题
11.CF_1677A(1600)
点击此处直达
tag -> brute force , dp , math
标签:暴力优化,预处理,前缀与后缀
题目大意:
现在给出一个长度为n的排列p,分别为p1,p2,p3…并且pi≠pj(i≠j)问能有多少个组合[a,b,c,d]满足(a<b<c<d)并且( p a < p c p_a<p_c pa<pc and p b > p d p_b>p_d pb>pd)
思路:
按照常规思路,最暴力的解法就是时间复杂度为O(n4)的枚举,现在我们思考如何优化。
可以处理前缀和后缀,固定b和c,然后对于每一组(b,c)答案累计b左面小于a[c]的个数和c右面小于a[b]的个数的乘积即可
预处理代码如下:
for(int i=1;i<=n;i++)
v[i]=0;
for(int i=1;i<=n;i++)
{
int s=0;
for(int j=1;j<=n;j++)
{
f1[i][j]=s;
s+=v[j];
}
v[a[i]]=1;
}
for(int i=1;i<=n;i++)
v[i]=0;
for(int i=n;i>=1;i--)
{
int s=0;
for(int j=1;j<=n;j++)
{
f2[i][j]=s;
s+=v[j];
}
v[a[i]]=1;
}
最后进行累加求结果即可,注意开long long
12.CF_1661B(1300)
点击此处直达
tag -> mitmasks , brute force , dp , dfs and similar ,greedy
标签:贪心,位运算
题目大意:
现在给出一个数v,在一次操作中可以执行:
1.使 $ v=(v+1) mod 32768$
2.使 v = ( 2 ∗ v ) m o d 32768 v=(2*v)mod32768 v=(2∗v)mod32768
现在你有n个数字,问对于每一个数字最少需要操作多少次可以使其变为0?
思路:
32768,一个很敏感的数字,32768就恰好为2^15,也就是说,无论是什么数字,都可以在15次操作2之后使其变为0,因此最小的操作数不可能大于15。
由于操作2每一次都是乘2,我们可以从二进制角度来看,乘以2就是在后面添一个0,而215就是1000000000000000(15个0),然后对于操作1,只可以使其二进制数中末尾+1,但是操作1对于连续的1来说,贡献度比操作2大,比如100111111想要变成128的倍数,只需要+1变成1010000000,而如果利用操作2,则需要后面加7个0,进行七次操作2。
因此我们的贪心策略很明显:分别对加操作和乘操作进行枚举,取得到目标结果的最小值即可
核心代码:
int get_cal(int x)
{
int pos=15;
for(int i=0;i<=15;i++)
{
if(x&(1<<i))//判断x的后i位是不是1
{
pos=i;
break;
}
}
return 15-pos;
}
int solve(int n)
{
int ans=1e7;
for(int j=0;j<=15;j++)
{
ans=min(ans,j+get_cal((n+j)%MOD));
}
return ans;
}
在与2的倍数有关的题目中,要保持一个敏感的心,就是考虑是否位运算bitmasks更方便求解
边栏推荐
- Post analysis of Data Analyst
- [leetcode] day104 no overlapping interval
- F - Pre-order and In-order(Atcoder 255)
- Wechat applet rotation map
- STM32 serial port based on Hal library accepts interrupts and idle interrupts
- ISG指数显示,亚太区IT和商业服务市场在第二季度出现大幅下滑
- 好用的shell快捷键
- influxDB 基础了解
- 微服务化解决文库下载业务问题实践
- Using JSON type to realize array function in MySQL
猜你喜欢

【小样本分割】MSANet: Multi-Similarity and Attention Guidance for Boosting Few-Shot Segmentation

js三种遍历数组的方法:map、forEach、filter

Ribbon负载均衡策略与配置、Ribbon的懒加载和饥饿加载

【机器学习网络】BP神经网络与深度学习-6 深度神经网络(deep neural Networks DNN)

e. The difference between target and e.currenttarget

Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products

Detailed explanation of TCP protocol knowledge

JS three methods of traversing arrays: map, foreach, filter

The difference between ArrayList and LinkedList

结构型模式-适配器模式
随机推荐
Hash (hash)
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看
Understand kingbasees V9 in one picture
无有线网络下安装并配置debian
[leetcode] day104 no overlapping interval
Okaleido ecological core equity Oka, all in fusion mining mode
PX4模块设计之十二:High Resolution Timer设计
Some common instructions in JVM tuning
卷积神经网络——灰度图像的卷积
标准C语言13
Remember the major performance problems caused by a TCP packet loss
Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling
利用JSON类型在mysql中实现数组功能
js修改对象数组的key值
2022杭电多校联赛第三场 题解
Sed output specified line
The project parameters are made into configurable items, and the @configurationproperties annotation is used
ROS camera calibration sensor_ Msgs/camerainfo message data type and meaning
P1438 无聊的数列 线段树+差分