QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#614181 | #8831. Chemistry Class | Heshu | TL | 0ms | 0kb | C++14 | 1.1kb | 2024-10-05 15:49:53 | 2024-10-05 15:49:53 |
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10;
int p,tt,n,a[N],A,B;
int tmp[N],so[N];
int l[N],r[N];
bool st[N];
void remo(int x) {
r[l[x]]=r[x];
l[r[x]]=l[x];
}
signed main() {
//freopen("dove.in","r",stdin);
//freopen("dove.out","w",stdout);
scanf("%lld%lld",&tt,&n);
for(int i=1;i<=n*2;i++) scanf("%lld",&a[i]);
while(tt--) {
scanf("%lld",&p);
while(p--) {
int x,v;scanf("%lld%lld",&x,&v);
a[x]=v;
}
scanf("%lld%lld",&A,&B);
for(int i=1;i<=n*2;i++) tmp[i]=a[i];
int cnt=0,sum=0;
sort(tmp+1,tmp+1+n*2);
for(int i=1;i<=n*2;i++) l[i]=i-1,r[i]=i+1,st[i]=0;
for(int i=1;i<=n*2;i++) {
if(st[i]) continue;
if(abs(tmp[r[i]]-tmp[i])<=B) {
if(sum%2&&abs(tmp[l[i]]-tmp[r[r[i]]])>A){
sum++;
continue;
}
st[r[i]]=1;
st[i]=1;
remo(r[i]);
remo(i);
cnt++;
}
else sum++;
}
int top=0,fd=0;
for(int i=1;i<=n*2;i++) if(!st[i]) so[++top]=tmp[i];
for(int i=1;i<=top;i+=2) if(abs(so[i]-so[i+1])>A) fd=1;
if(fd) {
puts("-1");
continue;
}
printf("%lld\n",cnt);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
4 1 2 1 42 69 2 3 1 1 2 3 4 2 5 1 6 1 3 4 5 19 1 1 7 8 9 10 11 12 13 14 20