当前位置:网站首页>CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
2022-07-02 11:55:00 【不是张胖胖】
D.Max GEQ Sum
题意:
给你长度为 n ( 1 ≤ 2 ∗ 1 0 5 ) n(1 \leq 2*10^5) n(1≤2∗105)的数组 a a a,是否满足对于任何 i , j ( 1 ≤ i ≤ j ≤ n ) i,j(1 \leq i \leq j \leq n) i,j(1≤i≤j≤n),保证
max ( a i , a i + 1 , . . . , a j − 1 , a j ) ≥ a i + a i + 1 + . . . + a j 1 + a j \max(a_i,a_{i+1},...,a_{j-1},a_j) \geq a_i+a_{i+1}+...+a_{j_1}+a_j max(ai,ai+1,...,aj−1,aj)≥ai+ai+1+...+aj1+aj思路:
通过单调栈,我们可以找到一个数字左/右第一个比它大的数字的位置,所以我们可以知道最大值为 a i a_i ai时的左右边界的取值范围
在这个范围内,只要 ∑ a \sum a ∑a的最大值小于 max a \max a maxa即满足要求,所以记录一个前缀和,并用线段树维护区间最大值和最小值,用右侧最大的值减去左侧最小的值算出的即为最大的 ∑ a \sum a ∑a
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
int n,a[maxn],b[maxn],l[maxn],r[maxn];
struct tree
{
int l,r,mx,mn;
int mid()
{
return (l+r)/2;
}
}tre[maxn<<2];
void pushup(int num)
{
tre[num].mx=max(tre[2*num].mx,tre[2*num+1].mx);
tre[num].mn=min(tre[2*num].mn,tre[2*num+1].mn);
}
void build(int num,int l,int r)
{
tre[num].l=l,tre[num].r=r;
if(l==r)
{
tre[num].mx=tre[num].mn=a[l];return;
}
int mid=tre[num].mid();
build(2*num,l,mid);
build(2*num+1,mid+1,r);
pushup(num);
}
int query_max(int num,int l,int r)
{
if(tre[num].l==l && tre[num].r==r)
return tre[num].mx;
int mid=tre[num].mid();
if(l>mid)
return query_max(2*num+1,l,r);
else if(r<=mid)
return query_max(2*num,l,r);
else
return max(query_max(2*num,l,mid),query_max(2*num+1,mid+1,r));
}
int query_min(int num,int l,int r)
{
if(tre[num].l==l && tre[num].r==r)
return tre[num].mn;
int mid=tre[num].mid();
if(l>mid)
return query_min(2*num+1,l,r);
else if(r<=mid)
return query_min(2*num,l,r);
else
return min(query_min(2*num,l,mid),query_min(2*num+1,mid+1,r));
}
signed main()
{
int T;scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
}
stack<int>s;
for(int i=1;i<=n;i++)
{
while(!s.empty() && b[i]>=b[s.top()]) s.pop();
if(s.size()==0) l[i]=0;
else l[i]=s.top();
s.push(i);
}
while(!s.empty()) s.pop();
for(int i=n;i>=1;i--)
{
while(!s.empty() && b[s.top()]<=b[i]) s.pop();
if(s.size()==0) r[i]=n+1;
else r[i]=s.top();
s.push(i);
}
for(int i=1;i<=n;i++) a[i]=a[i-1]+b[i];
a[n+1]=a[n];
build(1,0,n+1);
int ok=1;
for(int i=1;i<=n;i++)
{
int mn=query_min(1,l[i],i-1);
int mx=query_max(1,i,r[i]-1);
if(mx-mn>b[i])
{
ok=0;break;
}
}
ok?puts("YES"):puts("NO");
}
}
E. Number of Groups
题意:
给你 n n n段区间,每个区间有颜色 0 , 1 0,1 0,1,和长度 l , r ( 0 ≤ l i ≤ r i ≤ 1 0 9 ) l,r(0≤l_i≤r_i≤10^9) l,r(0≤li≤ri≤109)
如果两段区间颜色不同,且有交点则认为可以用链子给他们连接起来,问这 n n n个区间会形成多少个集合
思路:
很明显的并查集题目,问题在于如何优化一些时间
个人的一个思路是:将所有区间左右拆开进行排序,用 s e t set set存储,如果是左端点,并将另一个颜色的 s e t set set与之连接,则放入 s e t set set,否则从中删除。
考虑到速度过慢的原因是因为反复连接了一些已经在同一个集合中的边,所以我们将已经连接在一起的同色区间删除,只保留右端点最远的一个
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
int fa[maxn],n;
int find(int x){
if(x==fa[x]) return fa[x]; return fa[x]=find(fa[x]);}
void lianjie(int x,int y){
int px=find(x),py=find(y);fa[px]=py;}
struct node
{
int l,r,id;
bool operator < (const node& n1)const
{
if(n1.r==r && n1.l==l) return n1.id>id;
if(n1.r==r) return n1.l>l;
return n1.r>r;
}
}a[maxn];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n;scanf("%d",&n);
vector<tuple<int,int,int,int,int>>v;
for(int i=1;i<=n;i++)
{
int col,l,r;
fa[i]=i;scanf("%d%d%d",&col,&l,&r);
v.push_back({
l,1,r,col,i});v.push_back({
r,2,l,col,i});
}
sort(v.begin(),v.end());
multiset<node>s[2];
for(int i=0;i<v.size();i++)
{
vector<tuple<int,int,int,int>>del;
auto [x,y,z,col,id]=v[i];
if(y==1)
{
for(auto [l,r,id2]:s[col^1])
{
if(x<=r)
{
del.push_back({
l,r,id2,col^1});
lianjie(id,id2);
}
}
if(del.size()) del.pop_back();
s[col].insert({
x,z,id});
}
else
{
del.push_back({
z,x,id,col});
// printf("---\n");
}
for(auto [x,y,z,c]:del)
{
s[c].erase({
x,y,z});
}
}
int ans=0;
for(int i=1;i<=n;i++) if(fa[i]==i) ans++;//printf("i=%d \n",i);
printf(" %d\n",ans);
}
}
边栏推荐
- Ad20 cannot select the solution of component packaging in PCB editor
- 微信小程序使用towxml显示公式
- YoloV6训练:训练自己数据集遇到的各种问题
- Mfc a dialog calls B dialog function and passes parameters
- Fabric. Usage of JS eraser (including recovery function)
- Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
- taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
- Base64 编码原来还可以这么理解
- STM32标准固件库函数名记忆(二)
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
猜你喜欢
Makefile 分隔文件名与后缀
LeetCode 2310. The number of digits is the sum of integers of K
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
Kityformula editor configure font size and spacing
Base64 编码原来还可以这么理解
【C语言】详解指针的初阶和进阶以及注意点(1)
YoloV6训练:训练自己数据集遇到的各种问题
C语言习题---(数组)
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
Advanced C language (realize simple address book)
随机推荐
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
fatal: unsafe repository is owned by someone else 的解决方法
forEach的错误用法,你都学废了吗
LeetCode_字符串_简单_412.Fizz Buzz
【C语音】详解指针进阶和注意点(2)
info [email protected] : The platform “win32“ is incompatible with this module.
STM32标准固件库函数名记忆(二)
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
Base64 编码原来还可以这么理解
Makefile 分隔文件名与后缀
【C语言】详解指针的初阶和进阶以及注意点(1)
Contrôleur pour threejs cube Space Basic Controller + Inertial Control + Flight Control
Wechat applet uses towxml to display formula
C语言中的算术运算及相关练习题
info [email protected]: The platform “win32“ is incompatible with this module.
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
广州市应急管理局发布7月高温高湿化工安全提醒
表格响应式布局小技巧
数据库内容输出有问题怎么解决