当前位置:网站首页>Lexical Sign Sequence
Lexical Sign Sequence
2022-06-23 01:23:00 【Lupin123123】
Topic link
greedy + Tree array
The main idea of the topic : Construct a signal sequence with the smallest lexicographic order ( It only includes 1,-1) bring k The sum of the specified intervals is greater than or equal to the given value .
Ideas : You can start by letting all empty All the parts become -1, And then k The intervals are sorted from small to large at the right end , To satisfy each interval, turn from the back to the front -1 Change to 1.
Then let's talk about why we should sort the intervals from small to large according to the right endpoint , At first, my greedy idea was to give priority to satisfying the large range at the right endpoint , At that time, I felt that 1 Try to fill in the dictionary order later . However, this greed is wrong .
Complete code :
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e5+5;
using namespace std;
struct Query
{
int ai,bi;
int ci;
}q[maxn];
int n,k,ai,bi,ci;
int a[maxn],mark[maxn];
ll c[maxn];
bool cmp(Query x, Query y) {
return x.bi<y.bi; }
ll lowbit(ll x) {
return x&(-x); }
void add(int x, ll val)
{
while(x<=n)
{
c[x]+=val;
x+=lowbit(x);
}
}
ll query(int x)
{
ll ans=0;
while(x)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
void solve()
{
memset(mark,0,sizeof(mark));
memset(c,0,sizeof(c));
for (int i=1; i<=n; i++)
{
cin>>a[i];
if (a[i]!=0) {
mark[i]=1; add(i,a[i]); }
else {
a[i]=-1; add(i,a[i]); } // The first a Set to -1
}
for (int i=1; i<=k; i++)
cin>>q[i].ai>>q[i].bi>>q[i].ci;
sort(q+1,q+k+1,cmp);
for (int i=1; i<=k; i++)
{
ai=q[i].ai, bi=q[i].bi, ci=q[i].ci;
int pos=bi;
while(query(bi)-query(ai-1)<ci && pos>=ai)
{
if (mark[pos]) {
pos--; continue; }
add(pos,2);
a[pos]=1; mark[pos]=1; pos--;
}
if (pos<ai && query(bi)-query(ai-1)<ci )
{
cout<<"Impossible"<<endl;
return;
}
}
for (int i=1; i<=n; i++) cout<<a[i]<<' ';
cout<<endl;
return;
}
int main()
{
FAST;
while(cin>>n>>k)
{
solve();
}
return 0;
}
边栏推荐
- JS prevent the PC side from copying correct links
- What aspects of language and database knowledge are needed to build a web Kanban!
- Const defined variables and for of and for in in JS
- Node fetch download file
- Installing MySQL for Linux
- Psychological analysis of the safest spot Silver
- New progress in the construction of meituan's Flink based real-time data warehouse platform
- Webdriver and selenium Usage Summary
- MySQL - SQL execution process
- Sélecteur de hiérarchie
猜你喜欢

Read Amazon memorydb database based on redis
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

Have you stepped on these pits? Use caution when creating indexes on time type columns

SAP ui5 application development tutorial 102 - detailed explanation of the print function of SAP ui5 applications

Development status of full color LED display

LeetCode 206. Reverse linked list (iteration + recursion)

Hierarchy selector

SFOD:无源域适配升级优化,让检测模型更容易适应新数据

Webdriver and selenium Usage Summary

SAP mm transaction code vl04 create outbound delivery for sto
随机推荐
Day500: keyboard line
Pat a - 1010 radical (thinking + two points)
【机器学习-西瓜书】更文挑战【Day1】:1.1 引言
层次选择器
The longest child sequence of the 2019 Blue Bridge Cup
OSPF experiment in mGRE environment
[Title Fine brushing] 2023 Hesai FPGA
You can also do NLP (classification)
最安全的现货白银的心理分析
Add / get and delete cookies
Installing MySQL for Linux
3D打印微组织
Dual cross domain: access allow origin header contains multiple values "*, *", but only one is allowed
cadence SPB17.4 - 中文UI设置
MGRE环境下的OSPF实验
C serializabledictionary serialization / deserialization
SAP ui5 application development tutorial 103 - how to consume third-party libraries in SAP ui5 applications
C# SerializableDictionary序列化/反序列化
Development status of full color LED display
Ansible learning summary (7) -- ansible state management related knowledge summary