跳到正文

题解

题解:P9046 [PA 2021] Pandemia

ZBH2015约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年11月23日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 9 分钟
题目
P9046

版本

  1. v1当前

一道贪心的题目。

思路

根据题意可将连续未感染的城市域建模成一个块。

这里定义一个上午等价于一分钟。

定义 lenilen_i 是每个未感染城市块。(代码中方便就用的是结构体捆绑)

分类讨论一下,有以下两种情况:

  • 未感染区域块左右被两个感染区夹着。
  • 未感染区域块在两端。

手动构造几个样例算一下,可以发现:

  • 两端的块是中间块排序基准的两倍,因为两端只有一头在传染。所有我们建模的时候可以把它 ×2\times2,计算的时候再 ÷2\div2 即可。

  • 防止中间的未感染区被感染要堵两端,会消耗 22 分钟,并且这个块的贡献只有当前块当前长度的 leni−1len_i-1,而两端的只用堵一头,会消耗 11 分钟,贡献为当前块当前长度。

  • 过了 xx 分钟后,夹在中间的未感染区减少了 leni−x×2len_i-x\times2 的长度,而在两端的减少了 leni−xlen_i-x 的长度。

  • 要按照块的大小排序处理。

Tips

  • 建模的时候要细心。
  • 可以发现当在中间的块长度还剩 11 或 22 时,只能给贡献加 11,而且只会消耗 11 分钟。
  • 多测记得清空,但每组数据都 memset 会 TLE,所有每次只清空到 nn 即可。
  • 当这个块已经被传染时,其他比它小的块一定也被传染了,可以直接 break。

代码

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma g++ optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=1e6+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
struct node{
    int len;
    bool is_l_or_r;//判断是否两边
    bool operator < (const node &b)const{
        return len>b.len;
    }
};
node a[N];
int n,pos;
string s;
inline void pre(){
    pos=0;
}
inline void input(){
    n=read();
    F(i,0,n,1) a[i].len=0,a[i].is_l_or_r=0;//清空
    bool flag=0;
    s=rs();
    if(s[0]=='0') a[0].is_l_or_r=1;//判断是否为最左
    F(i,0,n-1,1){
        if(s[i]=='0') a[pos].len+=1;
        else{
            if(a[pos].len!=0){//上一个块有值再加
                pos+=1;
                if(pos-1==0&&a[0].is_l_or_r) a[pos-1].len*=2,flag=1;//如思路中所述
            }
        }
    }
    if(a[pos].len==0) pos-=1;
    if(s[n-1]=='0') a[pos].is_l_or_r=1;//判断是否为最右
    if((pos!=0&&a[pos].is_l_or_r)||(!flag&&a[pos].is_l_or_r)) a[pos].len*=2;//如思路中所述
}
inline void output(){
    int min=0,ans=0;
    sort(a,a+pos+1);
    F(i,0,pos,1){
        int nowlen;
        if(a[i].is_l_or_r) nowlen=a[i].len/2-min,min++;
        else{
            nowlen=a[i].len-min*2;
            if(nowlen==1||nowlen==2){
                min+=1;
                nowlen=1;
            }else nowlen-=1,min+=2;//如思路中所述
        }
        if(nowlen<0) break;
        ans+=nowlen;
    }
    write(max(0,n-ans));
    pc('\n');
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    //freopen("","r",stdin);
    //freopen("","w",stdout);
    //ios::sync_with_stdio(0);
    //cin.tie(0);cin.tie(0);
    int _=1;_=read();
    while(_--) solve();
    flush();
    return 0;
}
原文