2018-2019 ACM-ICPC, Asia Seoul Regional Contest——A - Circuits
A - Circuits
不難發現x座標根本沒用,只需要儲存y座標。
題目所求的兩條直線
y
1
=
a
y_1=a
y1=a,
y
2
=
b
(
a
<
b
)
y_2=b\ (a<b)
y2=b (a<b)
我們列舉
y
2
=
b
y_2=b
y2=b這條線,這條線一定可以是矩形的邊界,於是我們掃描矩形邊界差分計算當前這條線覆蓋的矩形個數,對於這條線沒有覆蓋的矩形把它丟到線段樹中(維護區間+和區間max即可)然後區間查詢
y
1
=
a
y_1=a
y1=a覆蓋的最大矩形即可。兩種相加即是當前的情況的最大數量。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const ll mod=998244353;
const int N=200010;
int n,m;
struct line
{
int id,v,op;
bool operator<(const line &o) const
{
if(v==o.v) return op<o.op;
return v<o.v;
}
}q[N];
int y[N];
pii a[N];
int find(int x)
{
return lower_bound(y+1,y+1+m,x)-y;
}
struct node
{
int l,r;
int v,lazy;
}tree[N*4];
void pushup(int u)
{
tree[u].v=max(tree[u<<1].v,tree[u<<1|1].v);
}
void build(int u,int l,int r)
{
tree[u]={l,r};
if(l==r)
{
tree[u].v=0;
return;
}
int mid=l+r>>1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}
void pushdown(int u)
{
if(!tree[u].lazy) return;
tree[u<<1].lazy+=tree[u].lazy;
tree[u<<1|1].lazy+=tree[u].lazy;
tree[u<<1].v+=tree[u].lazy;
tree[u<<1|1].v+=tree[u].lazy;
tree[u].lazy=0;
}
void modify(int u,int l,int r,int v)
{
if(l>r) return;
if(tree[u].l>=l&&tree[u].r<=r)
{
tree[u].lazy+=v;
tree[u].v+=v;
return;
}
pushdown(u);
int mid=tree[u].l+tree[u].r>>1; l+tree[u].r>>1;
if(l<=mid) modify(u<<1,l,r,v);
if(r>mid) modify(u<<1|1,l,r,v);
pushup(u);
}
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
int t1,t2;
cin>>t1>>y[i];
cin>>t2>>y[i+n];
a[i]={y[i+n],y[i]};
y[i]++;
q[i]={i,y[i],-1};
q[i+n]={i,y[i+n],+1};
}
sort(y+1,y+1+2*n);
m=unique(y+1,y+1+2*n)-y-1;
build(1,1,m);
sort(q+1,q+1+2*n);
int pre=0;
int res=0;
for(int i=1;i<=2*n;i++)
{
pre+=q[i].op;
res=max(res,pre+tree[1].v);
if(q[i].op==-1)
{
int id=q[i].id;
modify(1,find(a[id].first),find(a[id].second+1)-1,1);
}
}
cout<<res<<'\n';
}
return 0;
}
這題有一個錯誤的貪心思路即求兩邊最大值。
先讓第一條線覆蓋最多的矩形,然後把這些矩形刪除,然後再求出第二條線覆蓋最多的矩形。
我也不知道這個貪心思路錯在哪裡(還沒舉出反例
先貼一個隊友寫的錯誤程式碼
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#define N 20000005
using namespace std;
const int m = 10000000,inf = 20000000;
int n;
struct operation{
int c1,c2,r1,r2;
}op[100005];
int x[N],y[N],cans1,cans2;
int read(){
char ch = getchar();
int re = 0,fl = 1;
while(ch<'0'||ch>'9') {if(fl == '-')fl = -1; ch = getchar();}
while(ch>='0'&&ch<='9') {re = (re<<1)+(re<<3)+ch-'0'; ch = getchar();}
return re*fl;
}
int main(){
//freopen("1.in","r",stdin);
int r1,r2,c1,c2,rf = inf,rl = 0,cf = inf,cl = 0;
int pre = 0,fd = 0;
n = read();
for(int i=1;i<=n;++i){
r1 = read()+m; c1 = read()+m;//r1<r2 c1>c2
r2 = read()+m; c2 = read()+m;
y[c1+1]--; y[c2]++;
op[i].c1 = c1; op[i].c2 = c2; op[i].r1 = r1; op[i].r2 = r2;
}
pre = 0; fd = -1;
for(int i=0;i<=2*m;++i){
pre += y[i];
if(pre >= cans1){
cans1 = pre; fd = i;
}
}
for(int i=1;i<=n;++i)
if(op[i].c1 >= fd && op[i].c2 <= fd){
y[op[i].c1+1]++; y[op[i].c2]--;
}
pre = 0;
for(int i=0;i<=2*m;++i){
pre += y[i];
cans2 = max(cans2,pre);
}
printf("%d\n",cans1+cans2);
return 0;
}
相關文章
- The 2024 ICPC Asia Nanjing Regional ContestNaN
- The 2021 ICPC Asia Shenyang Regional Contest
- The 2023 ICPC Asia Macau Regional ContestMac
- The 2022 ICPC Asia Nanjing Regional ContestNaN
- The 2022 ICPC Asia Xian Regional Contest
- The 2022 ICPC Asia Hangzhou Regional Programming Contest
- The 2022 ICPC Asia Xian Regional Contest 前六題
- The 2022 ICPC Asia Nanjing Regional Contest IGDA,和令人疑惑的MNaN
- Chayas - 2023-2024 ICPC, Asia Yokohama Regional Contest 2023, Problem E
- 2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming ContestACM
- The 2023 ICPC Asia Hangzhou Regional Contest (The 2nd Universal Cup. Stage 22: Hangzhou)
- The 2023 ICPC Asia Jinan Regional Contest (The 2nd Universal Cup. Stage 17: Jinan)補題記錄NaN
- 2017 ACM/ICPC Asia Regional Shenyang Online - 做題記錄ACM
- The 2023 ICPC Asia EC Regionals Online Contest (I)
- The 2024 ICPC Asia East Continent Online Contest (I)AST
- 2018 BACS Regional Programming Contest C. BACS, Scoundrel Shopkeeper and Contiguous Sequence (模擬)
- 2019-2020 ICPC Southeastern European Regional Programming Contest (SEERC 2019) 補題記錄AST
- The 2024 ICPC Asia EC Regionals Online Contest (II) - Problem H. Points Selection
- The 2024 ICPC Asia EC Regionals Online Contest (II) - Problem B. Mountain BookingAI
- 2024-2025 ICPC, NERC, Southern and Volga Russian Regional Contest 個人題解(A,B,C,G,J,K,L,N)
- 2020-2021 ICPC NERC (NEERC), North-Western Russia Regional Contest (Northern Subregionals) E(離散化+尤拉回路)
- Asia-Tsukuba 2017
- 生成元(Digit Generator, AMC/ICPC Seoul 2005, UVa1583)Git
- 2024 (ICPC) Jiangxi Provincial Contest -- Official Contest
- AISing Programming Contest 2021(AtCoder Beginner Contest 202)AI
- Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)
- The 2024 CCPC Shandong Invitational Contest and Provincial Collegiate Programming Contest
- 2018 Multi-University Training Contest 3 - HDU ContestAI
- Toyota Programming Contest 2024#11(AtCoder Beginner Contest 379)
- KEYENCE Programming Contest 2024(AtCoder Beginner Contest 374)題解
- Toyota Programming Contest 2024#7(AtCoder Beginner Contest 362)
- Toyota Programming Contest 2024#3(AtCoder Beginner Contest 344)
- UNIQUE VISION Programming Contest 2024 Spring(AtCoder Beginner Contest 346)Spring
- nowcoder Week Contest
- Weekly Contest 387
- ACM-ICPC 2018 徐州賽區網路預賽ACM
- Toyota Programming Contest 2024#11(AtCoder Beginner Contest 379)題解
- Hitachi Vantara Programming Contest 2024(AtCoder Beginner Contest 368)題解A~D