当前位置:网站首页>牛客手速月赛 48 C(差分都玩不明白了属于是)
牛客手速月赛 48 C(差分都玩不明白了属于是)
2022-04-23 02:22:00 【先求一个导】
不想玩辣!
好久没打牛客了,第二题连wa5发,心态有点炸。天天做天梯赛的题都有点不知道罚时,就硬莽夫,是个坏习惯。罢了,收拾收拾退役了。
题目
题意: 给定n个编号从1到n的区间,给定len,求有多少个区间满足长度为len且与区间相交的数量最多且权重最大。
权重: 所有相交区间的编号和。
思路: 稍微想想就知道,每次只移动一个格子,把信息预处理出来即可。傻了,非得用vector处理。这里和差分非常像的,但是不能在消失的地方–,因为可能区间左端点还在区间中。只需要多开一个数组记录所有给定区间的右端点,在枚举的时候减去即可。
!注意右端点最大是5e6+5e6
时间复杂度: O(n)
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e7+10;
typedef long long ll;
int n,m,k,T;
int b[N],bb[N];
ll w[N],ww[N];
ll ans = 0;
ll mx = 0;
ll quan = 0;
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;++i)
{
int x,y; cin>>x>>y;
b[x]++,bb[y]++;
w[x]+=i,ww[y]+=i;
}
ll now;ll sum;
for(int i=1;i<=m;++i) mx += b[i],quan += w[i];ans = 1;
now = mx; sum = quan;
for(int i=m+1;i<N;++i)
{
now += b[i]; sum += w[i];
now -= bb[i-m]; sum -= ww[i-m];
if(now > mx || (now==mx&&sum>quan))
{
mx = now; quan = sum; ans = 0;}
if(now==mx&&sum==quan) ans++;
}
cout<<ans;
}
signed main(void)
{
T = 1;
// cin>>T;
while(T--)
solve();
return 0;
}
版权声明
本文为[先求一个导]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xianqiuyigedao/article/details/124355798
边栏推荐
- ThinkPHP kernel development blind box mall source code v2 0 docking easy payment / Alibaba cloud SMS / qiniu cloud storage
- 【Chrome扩展程序】content_script的跨域问题
- World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
- Campus transfer second-hand market source code
- leetcode:27. Remove element [count remove]
- 【汇编语言】从最底层的角度理解“堆栈”
- 关于局域网浅谈
- MySQL C language connection
- 006_redis_SortedSet类型
- JSP page nesting
猜你喜欢
89 logistic回归用户画像用户响应度预测
Handwritten memory pool and principle code analysis [C language]
009_ Redis_ Getting started with redistemplate
So library dependency
006_redis_SortedSet类型
SO库依赖问题
[chrome extender] content_ Cross domain problem of script
Daily question (April 22, 2022) - rotation function
Common formatting problems after word writing
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
随机推荐
Data warehouse construction table 111111
006_redis_jedis快速入门
The usage and difference of * and & in C language and the meaning of keywords static and volatile
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
Dynamic batch processing and static batch processing of unity
数仓建表111111
New book recommendation - IPv6 technology and application (Ruijie version)
Arduino esp8266 network upgrade OTA
世界读书日 | 技术人不要错过的好书(IT前沿技术)
006_ redis_ Sortedset type
go语言打怪通关之 ⌈互斥锁和状态协程⌋
Dynamic memory management
002_Redis_String类型常见的操作命令
Find the largest number of two-dimensional arrays
Use Xdebug breakpoint debugging in postman
How to configure iptables to realize local port forwarding
Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
010_StringRedisTemplate
想体验HomeKit智能家居?不如来看看这款智能生态
89 logistic regression user portrait user response prediction