跳到正文

个人记录

2023 1月 做题计划

MatchaNeko_nya约 115 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月9日
篇幅
约 115 分钟
获赞
3

版本

  1. v1当前

不开 long long 见祖宗,不开 unsigned long long 见太祖宗,单调结构不 push_back() 自己去当祖宗

目前战绩:

22 橙\color{#f39c11}\textbf{橙}

1313 黄\color{#ffc116}\textbf{黄}

99 绿\color{#52c41a}\textbf{绿}

1515 蓝\color{#3498db}\textbf{蓝}

5[1]5^{[1]} 紫\color{#9d3dcf}\textbf{紫}

之所以打个 [1] 是因为有道题告诉 cz 暴力能过后 cz 改时限了 QWQ\tiny \text{之所以打个 [1] 是因为有道题告诉 cz 暴力能过后 cz 改时限了 QWQ}

1 月 17 日

P5925 [POI2007] 天然气管道Gaz\color{#f39c11}\bf{P5925\,}\bf{[POI2007]}\,\text{天然气管道Gaz}

嗬,一眼以为橙题出二分图匹配,唏嘘不已。

设天然气井坐标集合 A={(x1′,y1′),(x2′,y2′)...(xn−1′,yn−1′),(xn′,yn′)}A=\{(x'_1,y'_1),(x_2',y'_2)...(x'_{n-1},y'_{n-1}),(x'_n,y'_n)\},中转站坐标集合 B={(x1,y1),(x2,y2)...(xn−1,yn−1),(xn,yn)}B=\{(x_1,y_1),(x_2,y_2)...(x_{n-1},y_{n-1}),(x_n,y_n)\}

显然只能往右下走“直角”建,所以一个天然气井和中转站距离为 xi′−xi+yi−yi′x'_i-x_i+y_i-y'_i,注意到不用考虑是哪一种方案,所以显然答案就为:

∑i=1n(xi′−xi+yi−yi′)\sum^{n}_{i=1}(x'_{i}-x_{i}+y_{i}-y'_{i})

思考:如果要输出方案呢?

C++
#include<bits/stdc++.h>
using namespace std;

int n;
long long ans=0;
int x;

int main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cin>>n;
    for(int i = 1; i<=n; i++){
        cin>>x;
        ans-=x;
        cin>>x;
        ans+=x;
    }
    for(int i = 1; i<=n; i++){
        cin>>x;
        ans+=x;
        cin>>x;
        ans-=x;
    }
    cout<<ans;
    return 0;
}

P3496 [POI2010] GIL-Gulids\color{#52c41a}\bf{P3496\,}\bf{[POI2010]}\,\text{GIL-Gulids}

水绿。

为什么一看到染色就想到二分图awa

不过真的和二分图差不多

C++
#include<bits/stdc++.h>
using namespace std;


#define link _link

long long n,m;
vector<int> link[200010];
short color[200010];

bool have[200010];
#define vis have

inline void add(int u,int v){
    link[u].push_back(v);
    link[v].push_back(u);
    return ;
}

void dfs(int u){
    for(auto it:link[u]){
        color[it]=color[u]%2+1;
    }
    return ;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i = 1; i<=m; i++){
        int u,v;
        cin>>u>>v;
        add(u,v);
        have[u]=1;
        have[v]=1;
    }
    for(int i = 1; i<=n; i++)
        if(!have[i]){
            cout<<"NIE\n";
            return 0;
        }
    cout<<"TAK\n";
    for(int i = 1; i<=n; i++){ //不保证连通图awa
        if(color[i]) continue;
        color[i]=1;
        dfs(i);
    }
    for(int i = 1; i<=n; i++){
        cout<<(color[i]==1?"S\n":"K\n");
    }
    return 0;
}

1 月 18 日

P4168 [Violet] 蒲公英\color{#9d3dcf}\bf{P4168\,}\bf{[Violet]}\,\text{蒲公英}

推荐:by A_Passing_Creeper

wc,暴力离散化加暴力桶就过了?!谔谔

过个 cz 啊。

早知道不告诉 cz 了。

快读快写写错了 MLE 找半天……(没屁用)

C++
#pragma GCC target("sse,sse2,sse3")
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int APC[50001];
int LHC[50001];
int Lemon[50001];
#define reg register
struct control{
    int ct,val;
    control(int Ct,int Val=-1):ct(Ct),val(Val){}
    inline control operator()(int Val){
        return control(ct,Val);
    }
}_endl(0),_prs(1),_setprecision(2);
struct FastIO{
    #define IOSIZE 1000000
    char in[IOSIZE],*p,*pp,out[IOSIZE],*q,*qq,ch[20],*t,b,K,prs;
    FastIO():p(in),pp(in),q(out),qq(out+IOSIZE),t(ch),b(1),K(6){}
    ~FastIO(){fwrite(out,1,q-out,stdout);}
    inline char getch(){
        return p==pp&&(pp=(p=in)+fread(in,1,IOSIZE,stdin),p==pp)?b=0,EOF:*p++;
    }
    inline void putch(char x){
        q==qq&&(fwrite(out,1,q-out,stdout),q=out),*q++=x;
    }
    inline void puts(const char str[]){fwrite(out,1,q-out,stdout),fwrite(str,1,strlen(str),stdout),q=out;}
    inline void getline(string& s){
        s="";
        for(reg char ch;(ch=getch())!='\n'&&b;)s+=ch;
    }
    #define indef(T) inline FastIO& operator>>(T& x){\
        x=0;reg char f=0,ch;\
        while(!isdigit(ch=getch())&&b)f|=ch=='-';\
        while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getch();\
        return x=f?-x:x,*this;\
    }
    indef(int)
    indef(long long)
    inline FastIO& operator>>(char& ch){return ch=getch(),*this;}
    inline FastIO& operator>>(string& s){
        s="";reg char ch;
        while(isspace(ch=getch())&&b);
        while(!isspace(ch)&&b)s+=ch,ch=getch();
        return *this;
    }
    inline FastIO& operator>>(double& x){
        x=0;reg char f=0,ch;
        double d=0.1;
        while(!isdigit(ch=getch())&&b)f|=(ch=='-');
        while(isdigit(ch))x=x*10+(ch^48),ch=getch();
        if(ch=='.')while(isdigit(ch=getch()))x+=d*(ch^48),d*=0.1;
        return x=f?-x:x,*this;
    }
    #define outdef(_T) inline FastIO& operator<<(_T x){\
        !x&&(putch('0'),0),x<0&&(putch('-'),x=-x);\
        while(x)*t++=x%10+48,x/=10;\
        while(t!=ch)*q++=*--t;\
        return *this;\
    }
    outdef(int)
    outdef(long long)
    inline FastIO& operator<<(char ch){return putch(ch),*this;}
    inline FastIO& operator<<(const char str[]){return puts(str),*this;}
    inline FastIO& operator<<(const string& s){return puts(s.c_str()),*this;}
    inline FastIO& operator<<(double x){
        reg int k=0;
        this->operator<<(int(x));
        putch('.');
        x-=int(x);
        prs&&(x+=5*pow(10,-K-1));
        while(k<K)putch(int(x*=10)^48),x-=int(x),++k;
        return *this;
    }
    inline FastIO& operator<<(const control& cl){
        switch(cl.ct){
            case 0:putch('\n');break;
            case 1:prs=cl.val;break;
            case 2:K=cl.val;break;
        }
    }
    inline operator bool(){return b;}
}io;
#undef reg

int x=0;
int l0,r0;
int l,r;
int n,m;

int main() {
    io>>n>>m;
    register int i;
    for (i=1;i<=n;i++) {
        io>>APC[i];
        LHC[i]=APC[i];
    }
    stable_sort(LHC+1,LHC+n+1);
    register int sum=unique(LHC+1,LHC+n+1)-LHC-1;
    for (i=1;i<=n;i++)
        APC[i]=lower_bound(LHC+1,LHC+sum+1,APC[i])-LHC;
    while(m--){
        io>>l0>>r0;
        l=(l0+x-1)%n+1;
        r=(r0+x-1)%n+1;
        if(l>r) {l^=r;r^=l,l^=r;}
        for(i=l;i<=r;i++)
            Lemon[APC[i]]++;
        register int maxx=0;
        register int maxi=0;
        for(i=1;i<=sum;i++){
            if(maxx<Lemon[i]){
                maxx=Lemon[i];
                maxi=i;
            }
        }
        io<<(x=LHC[maxi])<<'\n';
        memset(Lemon,false,sizeof(Lemon));
    }
    return 0;
}

CF600B  Queries about less or equal elements\color{#52c41a}\bf{CF600B\,}\,\text{Queries about less or equal elements}

什么二分水题。

C++
#include<bits/stdc++.h>
using namespace std;

int n,m;
vector<int> a;
int b;

int main(){
    cin>>n>>m;
    for(int i = 1; i<=n; i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    while(m--){
        int x;
        cin>>x;
        cout<< upper_bound(a.begin(),a.end(),x)-a.begin() << " ";
    }
    return 0;
}

B3695  集合运算\color{#ffc116}\bf{B3695\,}\,\text{集合运算}

扶苏苏珂爱捏。

C++
#include <bitset>
#include <vector>
#include <iostream>

const int maxn=30005;
int n,m,q;
std::bitset<maxn> s[maxn];

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin>>n>>m>>q;
    for(int i = 1,c,x;i<=n; i++){
        for(std::cin>>c;c;--c){
            std::cin>>x;
            s[i].set(x-1,1);
        }
    }
    for(int i = 0; i<m; i++) s[0].set(i,1);
    for(int o,x,y;q;--q){
        std::cin>>o>>x>>y;
        if(o==1){
            s[x]<<=y;
            s[x]&=s[0];
        }else if(o==2){
            s[x]>>=y;
        }else if(o==3){
            std::cout<<(s[x]&s[y]).count()<<'\n';
        }else if(o==4){
            std::cout<<(s[x]|s[y]).count()<<'\n';
        }else if(o==5){
            std::cout<<(s[x]^s[y]).count()<<'\n';
        }
    }
    return 0;
}

B3666  求数列所有后缀最大值的位置 \color{#ffc116}\bf{B3666\,}\,\text{求数列所有后缀最大值的位置 }

C++
#include <vector>
#include <array>
#include <iostream>

int n, ans;
std::vector<int> stk;
unsigned long long a[1000005];

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    while (stk.size() && (a[i] >= a[stk.back()])) {
      ans ^= stk.back();
      stk.pop_back();
    }
    stk.push_back(i);
    ans ^= i;
    std::cout << ans << '\n';
  }
}

P3865  【模板】ST 表\color{#ffc116}\bf{P3865\,}\,\text{【模板】ST 表}

找模板,A。

C++
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=1e6+10;
inline int read()
{
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int Max[MAXN][21];
int Query(int l,int r)
{
    int k=log2(r-l+1);
    return max(Max[l][k],Max[r-(1<<k)+1][k]);
}
int main()
{
    int N=read(),M=read();
    for(int i=1;i<=N;i++) Max[i][0]=read();
    for(int j=1;j<=21;j++)
        for(int i=1;i+(1<<j)-1<=N;i++)
            Max[i][j]=max(Max[i][j-1],Max[i+(1<<(j-1))][j-1]);
    for(int i=1;i<=M;i++)
    {
        int l=read(),r=read();
        printf("%d\n",Query(l,r));
    }
    return 0;
}

P2216 [HAOI2007] 理想的正方形\color{#3498db}\bf{P2216\,}\bf{[HAOI2007]}\,\text{理想的正方形}

%%% sto orz Ruiqun2009

什么诈骗题。

二维离线 RMQ 问题,maxxi,j,k/minni,j,kmaxx_{i,j,k}/minn_{i,j,k} 表示以 (i,j)(i,j) 为左上顶点,边长为 2k2^k 的正方形中的最大值/最小值。

直接 ST 上。

C++
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define INF (int)((1<<30)+1)
using namespace std;

int a, b, n;
int logn;
int Rq[2000][2000];
int maxx[2000][2000], minn[2000][2000];

inline int query (int x, int y){
    return max(maxx[x][y], max(maxx[x+n-(1<<logn)][y+n-(1<<logn)], max(maxx[x+n-(1<<logn)][y], maxx[x][y+n-(1<<logn)]))) -
    min(minn[x][y], min(minn[x+n-(1<<logn)][y+n-(1<<logn)], min(minn[x+n-(1<<logn)][y], minn[x][y+n-(1<<logn)])));
}

int main ()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> a >> b >> n;
    logn=log2(n);
    for (int i = 0; i < a; i++)
        for (int j = 0; j < b; j++) {
            cin>>Rq[i][j];
            maxx[i][j]=Rq[i][j];
            minn[i][j]=Rq[i][j];
        }

    for (int k = 0; k < logn; k++)
        for (int i = 0; i+(1<<k) < a; i++)
            for (int j = 0; j+(1<<k) < b; j++) {
                maxx[i][j] = max(maxx[i][j], max(maxx[i+(1<<k)][j+(1<<k)], max(maxx[i+(1<<k)][j], maxx[i][j+(1<<k)])));
                minn[i][j] = min(minn[i][j], min(minn[i+(1<<k)][j+(1<<k)], min(minn[i+(1<<k)][j], minn[i][j+(1<<k)])));
            }
    int ans = INF;
    for (int i = 0; i <= a-n; i++)
        for (int j = 0; j <= b-n; j++)
            ans = min(ans, query(i, j));

    cout << ans;
    return 0;
}

B3667  求区间所有后缀最大值的位置\color{#ffc116}\bf{B3667\,}\,\text{求区间所有后缀最大值的位置}

单调队列针不戳。

C++
#include <iostream>
#include <deque>
#include <queue>

typedef unsigned long long ull;

int n,k;
ull arr[1000005];
std::deque<int> Rq;

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin>>n>>k;
    for(int i = 1; i<=n; i++){
        std::cin>>arr[i];
        if(Rq.size()){
            if(Rq.front()+k-1<i) Rq.pop_front();
        }
        while(Rq.size()&&(arr[i]>=arr[Rq.back()])){
            Rq.pop_back();
        }
        Rq.push_back(i);
        if(i>=k){
            std::cout<<Rq.size()<<'\n';
        }
    }
    return 0;
}

P6510 奶牛排队\color{#52c41a}\bf{P6510\,}\text{奶牛排队}

C++
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>

const int maxn=1000006;

typedef unsigned long long ull;

int n;
int Rq[maxn];
std::vector<int> sx,sn;
int ans;

int main(){
    std::cin>>n;
    for(int i = 1; i<=n; i++){
        std::cin>>Rq[i];
    }
    for(int i = 1; i<=n; i++){
        while(sn.size() && Rq[sn.back()] >= Rq[i]) sn.pop_back();
        while(sx.size() && Rq[sx.back()] <  Rq[i]) sx.pop_back();
        int k = std::upper_bound(sn.begin(),sn.end(),(sx.size()?sx.back():0)) - sn.begin();
        if(k!=(sn.size())){
            ans=std::max(ans,i-sn[k]+1);
        }
        sn.push_back(i);
        sx.push_back(i);
    }
    std::cout<<ans;
    return 0;
}

P1908 逆序对\color{#ffc116}\bf{P1908}\,\text{逆序对}

C++
#include <iostream>
using namespace std;

int n,a[500010],c[500010];
long long ans;

void msort(int b,int e){
    if(b==e) return ;
    int mid=(b+e)/2,i=b,j=mid+1,k=b;
    msort(b,mid),msort(mid+1,e);
    while(i<=mid&&j<=e){
        if(a[i]<=a[j]) c[k++]=a[i++];
        else{
            c[k++]=a[j++],ans+=mid-i+1;
        }
    }
    while(i<=mid) c[k++]=a[i++];
    while(j<=e) c[k++]=a[j++];
    for(int l=b;l<=e;l++) a[l]=c[l];
    return ;
}

int main(){
    cin>>n;
    for(int i = 1; i<=n; i++) cin>>a[i];
    msort(1,n);
    cout<<ans;
    return 0;
}

P3374 【模板】树状数组\color{#ffc116}\bf{P3374}\,\text{【模板】树状数组}

分块好用捏。

但是被卡了,评测姬波动有点大捏。

C++
#include <iostream>
#include <cmath>
using namespace std;

int n,m;
int arr[500010];
int belong[500010];
int C;
int S;
int st[5050],ed[5050];
int sum[5050];

namespace sto_Rq{
    inline void upRq_p(int i,int d){
        sum[belong[i]]+=d;
        arr[i]+=d;
        return ;
        //Ruiqun2009 赐予我力量!
    }
    inline int qsRq_xy(int x,int y){
        int ans=0;
        if(belong[x]==belong[y]){
            for(int i = x; i<=y; i++) ans+=arr[i];
        }else{
            for(int i = x; i<=ed[belong[x]]; i++) ans+=arr[i];
            for(int i = belong[x]+1;i<=belong[y]-1;i++){
                ans+=sum[i];
            }
            for(int i = st[belong[y]]; i<=y; i++) ans+=arr[i];
        }
        return ans;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    cin>>n>>m;
    for(int i = 1; i<=n; i++) cin>>arr[i];
    S=int(sqrt(double(n)));
    for(int i = 1; i<=n; i+=S){
        st[++C]=i;
        ed[C]=min(i+S-1,n);
    }
    for(int i = 1; i<=C; i++){
        for(int j = st[i];j<=ed[i];j++){
            belong[j]=i;
            sum[i]+=arr[j];
        }
    }
    while(m--){
        int opt,x,y;
        cin>>opt>>x>>y;
        if(opt&1){
            sto_Rq::upRq_p(x,y);
        }else{
            cout<<sto_Rq::qsRq_xy(x,y)<<'\n';
        }
    }
    return 0;
}

1 月 19 日

P1228 地毯填补问题\color{#ffc116}\bf{P1228}\,\text{地毯填补问题}

什么诈骗题。

C++
#include <iostream>
#include <cstdio>
using namespace std;

void sto_Rq(int x1, int y1, int x2, int y2, int n) {
    if(n == 1) return ;
    if(x1 - x2 < (n >> 1)) {
        if(y1 - y2 < (n >> 1)) {
            printf("%d %d 1\n",x2+(n>>1),(y2+(n>>1)));
            sto_Rq(x1, y1, x2, y2, (n >> 1));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1), x2, y2 + (n >> 1), (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1) - 1, x2 + (n >> 1), y2, (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1), x2 + (n >> 1), y2 + (n >> 1), (n >> 1));
        } else {
            printf("%d %d 2\n",x2+(n>>1),(y2+(n>>1)-1));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1) - 1, x2, y2, (n >> 1));
            sto_Rq(x1, y1, x2, y2 + (n >> 1), (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1) - 1, x2 + (n >> 1), y2, (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1), x2 + (n >> 1), y2 + (n >> 1), (n >> 1));
        }
    } else {
        if(y1 - y2 < (n >> 1)) {
            printf("%d %d 3\n",x2+(n>>1)-1,(y2+(n>>1)));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1) - 1, x2, y2, (n >> 1));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1), x2, y2 + (n >> 1), (n >> 1));
            sto_Rq(x1, y1, x2 + (n >> 1), y2, (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1), x2 + (n >> 1), y2 + (n >> 1), (n >> 1));
        } else {
            printf("%d %d 4\n",x2+(n>>1)-1,(y2+(n>>1)-1));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1) - 1, x2, y2, (n >> 1));
            sto_Rq(x2 + (n >> 1) - 1, y2 + (n >> 1), x2, y2 + (n >> 1), (n >> 1));
            sto_Rq(x2 + (n >> 1), y2 + (n >> 1) - 1, x2 + (n >> 1), y2, (n >> 1));
            sto_Rq(x1, y1, x2 + (n >> 1), y2 + (n >> 1), (n >> 1));
        }
    }
}

int main() {
    int k, x, y;
    cin >> k >> x >> y;
    sto_Rq(x, y, 1, 1, 1 << k);
    return 0;
}

P1440 求 m 区间内的最小值\color{#ffc116}\bf{P1440}\,\text{求 m 区间内的最小值}

线段树真【】优雅。

C++
#include <iostream>
#include <cstdio>
#define N 2000010
using namespace std;

struct node
{
    int l, r, w, f;
} tree[N << 1 | 1];

int x, y, z, i, a[N], n, m;

void build(int k, int t, int w)
{
    int mid;
    if (t > w)
        return;
    if (t == w)
    {
        tree[k].l = t;
        tree[k].r = w;
        tree[k].w = a[t];
        return;
    }
    mid = (t + w) >> 1;
    build(k << 1, t, mid);
    build(k << 1 | 1, mid + 1, w);
    tree[k].l = t;
    tree[k].r = w;
    tree[k].w = min(tree[k << 1].w, tree[k << 1 | 1].w);
}
int query(int k, int t, int w)
{
    int mid;
    if (t > w)
        return 0;
    if (x <= t && w <= y)
    {
        return tree[k].w;
    }
    mid = (t + w) >> 1;
    int sum = 2147483647;
    if (x <= mid)
        sum = min(sum, query(k << 1, t, mid));
    if (y > mid)
        sum = min(sum, query(k << 1 | 1, mid + 1, w));
    return sum;
}
int read(int &x)
{
    char c = getchar();
    int f = 1;
    x = 0;
    while (c < '0' || c > '9')
    {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        x = (x<<1)+(x<<3)+(c^48);
        c = getchar();
    }
    x *= f;
}
signed main()
{
    read(n);
    read(m);
    for (int i = 1; i <= n; i++)
        read(a[i]);
    build(1, 1, n);
    for (int i = 1; i <= n; i++)
    {
        int c;
        x = i - m;
        y = i - 1;
        if (x < 1)
            x = 1;
        if (x > y)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n", query(1, 1, n));
    }
}

P6503  [COCI2010−2011#3] DIFERENCIJA\color{#52c41a}\bf{P6503\,}\,\bf{[COCI2010-2011\#3]}\,\text{DIFERENCIJA}

怎么说。。。

安利一篇blog

什么只因题。

C++
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

#define int long long

int arr[300010];
vector<int> v;
long long ans=0;
int n;
int maxx[300010][3];
int minn[300010][3];

signed main(){
    scanf("%lld",&n);
    for(int i = 1; i<=n; i++) scanf("%lld",arr+i);
    /*Lmax*/
    for(int i = 1; i<=n; i++){
        while(v.size()&&arr[v.back()]<=arr[i]) v.pop_back();
        if(v.empty()) maxx[i][1]=0;
        else maxx[i][1]=v.back();
        v.push_back(i);
    }
    v.clear();
    /*Rmax*/
    //倒着扫
    for(int i = n; i>=1; i--){
        while(v.size()&&arr[v.back()]<arr[i]) v.pop_back();
        if(v.empty()) maxx[i][2]=n+1;
        else maxx[i][2]=v.back();
        v.push_back(i);
    }
    v.clear();
    /*Lmin*/
    for(int i = 1; i<=n; i++){
        while(v.size()&&arr[v.back()]>=arr[i]) v.pop_back();
        if(v.empty()) minn[i][1]=0;
        else minn[i][1]=v.back();
        v.push_back(i);
    }
    v.clear();
    /*Rmin*/
    //倒着扫
    for(int i = n; i>=1; i--){
        while(v.size()&&arr[v.back()]>arr[i]) v.pop_back();
        if(v.empty()) minn[i][2]=n+1;
        else minn[i][2]=v.back();
        v.push_back(i);
    }
    v.clear();
    /*-------------------------------------*/
    for(int i = 1; i<=n; i++){
        ans+=(arr[i]*(i-maxx[i][1])*(maxx[i][2]-i));
        ans-=(arr[i]*(i-minn[i][1])*(minn[i][2]-i));
    }
    printf("%lld",ans);
    return 0;
}

P2627 [USACO11OPEN] Moving The Lawn G\color{#3498db}\bf{P2627\,}\bf{[USACO11OPEN]}\,\text{Moving The Lawn G}

单调队列优化 dp 好题。

C++
#include <cstdio>
#include <iostream>
#include <deque>
#define ll long long
#define int long long
using namespace std;
ll n,k,a[100001],s,Rq[100001],sum[100001];

typedef struct edge{ int v,pos; } node;

deque<node> dq;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register ll i,j;
	cin>>n>>k;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	dq.push_back((node){0,0});
	for(i=1;i<=n;i++)
	{
		ll rq=Rq[i-1]-sum[i];
        while(dq.size()&&dq.back().v<=rq) dq.pop_back();
        dq.push_back((node){rq,i});
        while(dq.size()&&dq.front().pos+((ll)(k))<i) dq.pop_front();
		Rq[i]=dq.front().v+sum[i];
	}
	cout<<Rq[n];
	return 0;
}

P2947  [USACO09MAR] Look Up S\color{#ffc116}\bf{P2947\,}\,\bf{[USACO09MAR]}\,\text{Look Up S}

还是单调栈板子……

但是终于第一次写完没错过了。

C++
#include <bits/stdc++.h>
using namespace std;

vector<int> v;
int arr[1000100];
int n;
int ans[1000100];

int main(){
    cin>>n;
    for(int i = 1; i<=n; i++){
        cin>>arr[i];
        while(v.size()&&arr[i]>arr[v.back()]){
            ans[v.back()]=i;
            v.pop_back();
        }
        v.push_back(i);
    }
    for(int i = 1; i<=n; i++){
        cout<<ans[i]<<'\n';
    }
    return 0;
}

P1081 [NOIP 2012 提高组] 开车旅行\color{#3498db}\bf{P1081\,}\text{[NOIP 2012\,提高组]}\,\text{开车旅行}

什么毒瘤题啊,dill 就卡我空间是吧。

调了 2 小时。

C++
#include <cstdio>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

#define INT_MAX 2147483647
#define INT_MIN -2147483647

//#define int long long
constexpr auto N = 100002;

typedef struct Rq
{
	int id, high;
	friend bool operator<(Rq AK, Rq IOI)
	{
		return AK.high < IOI.high;
	}
} city;

long long h[N], s[N], x[N];
long long n, m, x0;

multiset<city> _set;

void init_read()
{
	cin>>n;
	for (signed i = 1; i <= n; i++)
	{
		cin>>h[i];
	}
	cin>>x0>>m;
	for (signed i = 1; i <= m; i++)
	{
		cin>>s[i]>>x[i];
	}
}

int dist(int x, int y) { return fabs(h[x] - h[y]); }

int dp[20][5][N], dpa[20][5][N], dpb[20][5][N];

void func1()
{
	h[0] = INT_MAX, h[n+1] = INT_MIN;
	_set.insert((city){0, INT_MAX});
	_set.insert((city){0, INT_MAX});
	_set.insert((city){n + 1, INT_MIN});
	_set.insert((city){n + 1, INT_MIN});
	for (signed i = n; i >= 1; i--)
	{
		int ga,gb;
		city now;
		now.id=i,now.high=h[i];
		_set.insert(now);
		multiset<city>::iterator p=_set.lower_bound(now);
		p--;
		int lt=(*p).id,lh=(*p).high;
		p++,p++;
		int ne=(*p).id,nh=(*p).high;
		p--;
		if(abs(nh-h[i])>=abs(h[i]-lh))
		{
			gb=lt;
			p--,p--;
			if(abs(nh-h[i])>=abs(h[i]-(*p).high))
				ga=(*p).id;
			else
				ga=ne;
		}
		else
		{
			gb=ne;
			p++,p++;
			if(abs((*p).high-h[i])>=abs(h[i]-lh))
				ga=lt;
			else
				ga=(*p).id;
		}
		dp[0][0][i] = ga, dp[0][1][i] = gb;
		dpa[0][0][i] = dist(i, ga);
		dpb[0][1][i] = dist(i, gb);
	}
	return;
}

void func2()
{
	for (int i = 1; i <= 18; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			for (int k = 0; k < 2; k++)
			{
				if(i==1)
				{
					dp[1][k][j]=dp[0][1-k][dp[0][k][j]];
					dpa[1][k][j]=dpa[0][k][j]+dpa[0][1-k][dp[0][k][j]];
					dpb[1][k][j]=dpb[0][k][j]+dpb[0][1-k][dp[0][k][j]];
				}
				else
				{
					dp[i][k][j]=dp[i-1][k][dp[i-1][k][j]];
					dpa[i][k][j]=dpa[i-1][k][j]+dpa[i-1][k][dp[i-1][k][j]];
					dpb[i][k][j]=dpb[i-1][k][j]+dpb[i-1][k][dp[i-1][k][j]];
				}
			}
		}
	}
}

long long qa, qb;
double ans=2000000000.00;
int aid;

void query(long long c, long long dis)
{
	long long u = c;
	qa = 0,qb = 0;
	for (int i = 18; i >= 0; i--)
	{
		if (dp[i][0][u] &&  qa + qb + dpa[i][0][u] + dpb[i][0][u]<=dis)
		{
			qa += dpa[i][0][u], qb += dpb[i][0][u], u = dp[i][0][u];
		}
	}
}

void func3()
{
	for (int i = 1; i <= n; i++)
	{
		query(i, x0);
		double now = ((long double)qa / (long double)qb);
		if (ans > now)
		{
			ans = now;
			aid = i;
		}
		else
		{
			if (now==ans && h[aid] < h[i])
			{
				aid = i;
				//ans = now;
			} // 注意
		}
	}
	cout << aid << '\n';
	for (int i = 1; i <= m; i++)
	{
		query(s[i], x[i]);
		printf("%lld %lld\n", qa, qb);
	}
}

signed main()
{
	init_read();
	func1();
	func2();
	func3();
	return 0;
}

P3810 【模板】三维偏序 (陌上花开)\color{#9d3dcf}\bf{P3810\,}\textbf{【模板】三维偏序}\,\textbf{(陌上花开)}

AC code\color{green}\tt{AC\,code}

终于搞懂 CDQ 套树状数组了。

到时候研究一下 CDQ 套 CDQ。

C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

typedef struct RQ
{
	int a, b, c;
	int cnt, ans;
	RQ() {}
	RQ(int a, int b, int c)
	{
		this->a = a;
		this->b = b;
		this->c = c;
		this->cnt = 0;
		this->ans = 0;
	}
	RQ(int a, int b, int c, int top)
	{
		this->a = a;
		this->b = b;
		this->c = c;
		this->cnt = top;
		this->ans = 0;
	}
} edge;

edge arr1[1900080], arr2[1900080];
int n, k;
int reada, readb, readc;
int tmax;
int c[1900080];
int top;
int ans[1900080];
int m = 0;

inline int lowbit(int x) { return x & (-x); }
inline void add(int x, int y)
{
	while (x <= tmax)
		c[x] += y, x += lowbit(x);
}
inline int query(int x)
{
	int sum = 0;
	while (x)
		sum += c[x], x -= lowbit(x);
	return sum;
}

void CDQ(int l, int r)
{
	if (l == r)
		return;
	int mid = (l + r) >> 1;
	CDQ(l, mid);
	CDQ(mid + 1, r);
	stable_sort(arr2 + l, arr2 + mid + 1,
	[](edge a,edge b){
		if(a.b==b.b) return a.c<b.c;
		else return a.b<b.b;
	}
	);
	stable_sort(arr2 + mid + 1, arr2 + r + 1,
	[](edge a,edge b){
		if(a.b==b.b) return a.c<b.c;
		else return a.b<b.b;
	}
	);
	int i /*= mid + 1*/;
	int j = l;
	for (i = mid+1; i <= r; i++)
	{
		while (arr2[i].b >= arr2[j].b && j <= mid)
		{
			add(arr2[j].c, arr2[j].cnt);
			j++;
		}
		arr2[i].ans += query(arr2[i].c);
	}
	for(i=l;i<j;i++) add(arr2[i].c,-arr2[i].cnt);
}

int main()
{
	scanf("%d %d", &n, &k);
	tmax = k;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d %d %d", &reada, &readb, &readc);
		arr1[i] = ((edge){reada, readb, readc});
	}
	stable_sort(arr1 + 1, arr1 + 1 + n, [](edge a, edge b)
				{
			if(a.a==b.a){
				if(a.b==b.b)
					return a.c<b.c;
				else
					return a.b<b.b;
			}
			else return a.a<b.a; });
	for (int i = 1; i <= n; i++)
	{
		++top;
		if (arr1[i].a != arr1[i + 1].a || arr1[i].b != arr1[i + 1].b || arr1[i].c != arr1[i + 1].c)
		{
			++m;
			arr2[m].a = arr1[i].a;
			arr2[m].b = arr1[i].b;
			arr2[m].c = arr1[i].c;
			arr2[m].cnt = top;
			top = 0;
		}
	}
	CDQ(1, m);
	for (int i = 1; i <= m; i++)
	{
		ans[arr2[i].ans + arr2[i].cnt - 1] += arr2[i].cnt;
	}
	for (int i = 0; i < n; i++)
	{
		printf("%d\n", ans[i]);
	}
	return 0;
}

1 月 21 日

P6577 【模板】二分图最大权完美匹配 (KM 算法)\color{#9d3dcf}\bf{P6577\,}\textbf{【模板】二分图最大权完美匹配}\,\textbf{(KM 算法)}

二分图啊,我专业(/bushi

大号搬过来的。

C++
//P6577 【模板】二分图最大权完美匹配——KM算法 55分 by dfs
/*#include<bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 510;
const int INF = 99999999999;

int love[MAXN][MAXN]; //love_i,j存左i值至右j的权值
int ex_girl[MAXN]; //女生期望值
int ex_boy[MAXN]; //男生
bool vis_girl[MAXN]; //记录已匹配女生
bool vis_boy[MAXN]; //记录已匹配男生
int match[MAXN]; //记录对象
int slack[MAXN]; //最小减少值
int n,m;

bool dfs(int girl){
	vis_girl[girl] = true;
	for(int boy = 1; boy<=n; boy++){
		if(vis_boy[boy]) continue;
		int gap=ex_girl[girl]+ex_boy[boy]-love[girl][boy];
		if(!gap){
			vis_boy[boy] = true;
			if(!match[boy] || dfs(match[boy])){
				match[boy]=girl;
				return true;
			}
		}else{
			slack[boy]=min(slack[boy],gap);
		}
	}
	return false;
}

int KM(){
	memset(match,0,sizeof match);
	memset(ex_boy,0,sizeof ex_boy);

	for(int i = 1; i<=n; i++){
		ex_girl[i]=love[i][1];
		for(int j = 2; j<=n; j++){
			ex_girl[i]=max(ex_girl[i],love[i][j]);
		}
	}
	for(int i = 1; i<=n; i++){
		memset(slack,0x7f,sizeof slack);
		while(true){
			memset(vis_girl,0,sizeof vis_girl);
			memset(vis_boy,0,sizeof vis_boy);
			if(dfs(i)) break;
			int d=INF;
			for(int j = 1; j<=n; j++){
				if(!vis_boy[j]) d=min(d,slack[j]);
			}
			for(int j = 1; j<=n; j++){
				if(vis_girl[j]) ex_girl[j]-=d;
				if(vis_boy[j]) ex_boy[j]+=d;
				else slack[j]-=d;
			}
		}
	}
	int res = 0;
	for(int i = 1; i<=n; i++) res+=love[match[i]][i];
	return res;
}

signed main(){
    memset(love,-127,sizeof love);
	cin>>n>>m;
	while(m--){
		register int u,v,t;
		cin>>u>>v>>t;
		love[u][v]=t;
	}
	cout<<KM()<<'\n';
	for(int i = 1; i<=n; i++){
		cout<<match[i]<<" ";
	}
	return 0;
}*/
//BFS
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
const int maxn=509;
int n,m,cost[maxn][maxn],mat[maxn],pre[maxn];
int wisha[maxn],wishb[maxn],va[maxn],vb[maxn],slack[maxn];
void init(int a[],int b)
{
	for(int i=0;i<=n;i++)	a[i]=b;
}
void bfs(int u)
{
	init( pre,0 );
	init( slack,inf ); slack[0]=0;
	int x=0,y=0,yy=0,delta=0;
	mat[y]=u;//最开始0匹配u,为什么这么写,无关紧要,总要让一个开头嘛
	while( 1 )
	{
		x=mat[y], delta=inf, vb[y]=1;//现在是帮x找完美匹配
		for(int i=1;i<=n;i++)
		{
			if( vb[i] )	continue;//本次访问过,不用管了
			if( slack[i]>wisha[x]+wishb[i]-cost[x][i] )
			{
				slack[i]=wisha[x]+wishb[i]-cost[x][i];//更新最小误差期望
				pre[i]=y;
			}
			if( slack[i]<delta )	delta=slack[i],yy=i;//yy记录谁是最小误差
		}
		//delta是最小的误差,用于调整期望
		for(int i=0;i<=n;i++)
		{
			if( vb[i] )	wisha[mat[i]]-=delta,wishb[i]+=delta;
			else	slack[i]-=delta;
		}
		y=yy;//啥意思?yy记录的是最小误差,那么现在修改了期望值,所以可以直接匹配y
		if( mat[y]==-1 )	break;//直到找到一个人没有匹配
	}
	while( y )	mat[y]=mat[ pre[y] ],y=pre[y];
}
int km()
{
	init(wisha,0 ); init(wishb,0 ); init(mat,-1 );
	for(int i=1;i<=n;i++)
	{
		init(vb,0 );
		bfs(i);
	}
	int res=0;
	for(int i=1;i<=n;i++)	res+=cost[mat[i]][i];
	return res;
}
signed main()
{
	cin >> n >> m;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
		cost[i][j]=-inf;
	for(int i=1;i<=m;i++)
	{
		int l,r,w;
		cin >> l >> r >> w;
		cost[l][r]=max( cost[l][r],w );
	}
	cout << km() << '\n';
	for(int i=1;i<=n;i++)
		cout << mat[i] << " ";
}

P1886 滑动窗口/【模板】单调队列\color{#ffc116}\bf{P1886}\,\text{滑动窗口/【模板】单调队列}

漏网之鱼。

但是我竟然能把 50% 的数据看成 100% 的数据。

C++
#include <bits/stdc++.h>
using namespace std;

deque<int> maxx;
deque<int> minn;
int arr[1000010];
int n,k;
int ans[1000010][3];

int main(){
    cin>>n>>k;
    int rq=0;
    for(int i = 1; i<=n; i++){
        cin>>arr[i];
        while(maxx.size()&&maxx.front()<=i-k) maxx.pop_front();
        while(minn.size()&&minn.front()<=i-k) minn.pop_front();
        while(maxx.size()&&arr[i]>=arr[maxx.back()]){
            maxx.pop_back();
        }
        maxx.push_back(i);
        while(minn.size()&&arr[i]<=arr[minn.back()]){
            minn.pop_back();
        }
        minn.push_back(i);
        if(i>=k){
            ans[++rq][0]=arr[minn.front()];
            ans[rq][1]=arr[maxx.front()];
        }
    }
    for(int i = 1; i<=rq; i++){
        cout<<ans[i][0]<<" ";
    }
    cout<<'\n';
    for(int i = 1; i<=rq; i++){
        cout<<ans[i][1]<<" ";
    }
    return 0;
}

P5788 【模板】单调栈\color{#ffc116}\bf{P5788}\,\text{【模板】单调栈}

不关流同步就卡我 cin 是吧。

C++
#include<bits/stdc++.h>
using namespace std;

vector<int> amx;
int arr[3000010];
int n;
int ans[3000010];

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i = 1; i<=n; i++){
        cin>>arr[i];
        while(amx.size()&&arr[i]>arr[amx.back()]) {
            ans[amx.back()]=i;
            amx.pop_back();
        }
        amx.push_back(i);
    }
    for(int i = 1; i<=n; i++) cout<<ans[i]<<" ";
    return 0;
}

P4014 分配问题\color{#3498DB}\textbf{P4014 分配问题}

用二分图水网络瘤 24 题,开心。(话说这数据好水)

Θ(n4)\Theta(n^4) 的 dfs 版 KM 也能过,随便吧。

dfs

bfs

C++
//BFS
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
const int maxn=509;
int n,m,cost[maxn][maxn],mat[maxn],pre[maxn];
int wisha[maxn],wishb[maxn],va[maxn],vb[maxn],slack[maxn];
void init(int a[],int b)
{
	for(int i=0;i<=n;i++)	a[i]=b;
}
void bfs(int u)
{
	init( pre,0 );
	init( slack,inf ); slack[0]=0;
	int x=0,y=0,yy=0,delta=0;
	mat[y]=u;//最开始0匹配u,为什么这么写,无关紧要,总要让一个开头嘛
	while( 1 )
	{
		x=mat[y], delta=inf, vb[y]=1;//现在是帮x找完美匹配
		for(int i=1;i<=n;i++)
		{
			if( vb[i] )	continue;//本次访问过,不用管了
			if( slack[i]>wisha[x]+wishb[i]-cost[x][i] )
			{
				slack[i]=wisha[x]+wishb[i]-cost[x][i];//更新最小误差期望
				pre[i]=y;
			}
			if( slack[i]<delta )	delta=slack[i],yy=i;//yy记录谁是最小误差
		}
		//delta是最小的误差,用于调整期望
		for(int i=0;i<=n;i++)
		{
			if( vb[i] )	wisha[mat[i]]-=delta,wishb[i]+=delta;
			else	slack[i]-=delta;
		}
		y=yy;//啥意思?yy记录的是最小误差,那么现在修改了期望值,所以可以直接匹配y
		if( mat[y]==-1 )	break;//直到找到一个人没有匹配
	}
	while( y )	mat[y]=mat[ pre[y] ],y=pre[y];
}
int km()
{
	init(wisha,0 ); init(wishb,0 ); init(mat,-1 );
	for(int i=1;i<=n;i++)
	{
		init(vb,0 );
		bfs(i);
	}
	int res=0;
	for(int i=1;i<=n;i++)	res+=cost[mat[i]][i];
	return res;
}
signed main()
{
    int ans1,ans2;
	cin >> n ;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
		cost[i][j]=-inf;
	for(int i = 1; i<=n; i++){
        for(int j = 1; j<=n; j++){
            cin>>cost[i][j];
        }
    }
	ans1=km();
	for(int i = 1; i<=n; i++){
        for(int j = 1; j<=n; j++){
            cost[i][j]=-cost[i][j];
        }
    }
    ans2=-km();
    cout<<ans2<<'\n'<<ans1;
    return 0;
}

P1559 运动员最佳匹配问题\color{#52c41a}\textbf{P1559 运动员最佳匹配问题}

嗨,还以为又是网络瘤 24 题。

照样魔改二分图最大权完美匹配。

AC

C++
//BFS
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
const int maxn=509;
int n,m,cost[maxn][maxn],mat[maxn],pre[maxn];
int wisha[maxn],wishb[maxn],va[maxn],vb[maxn],slack[maxn];
void init(int a[],int b)
{
    for(int i=0;i<=n;i++)   a[i]=b;
}
void bfs(int u)
{
    init( pre,0 );
    init( slack,inf ); slack[0]=0;
    int x=0,y=0,yy=0,delta=0;
    mat[y]=u;//最开始0匹配u,为什么这么写,无关紧要,总要让一个开头嘛
    while( 1 )
    {
        x=mat[y], delta=inf, vb[y]=1;//现在是帮x找完美匹配
        for(int i=1;i<=n;i++)
        {
            if( vb[i] ) continue;//本次访问过,不用管了
            if( slack[i]>wisha[x]+wishb[i]-cost[x][i] )
            {
                slack[i]=wisha[x]+wishb[i]-cost[x][i];//更新最小误差期望
                pre[i]=y;
            }
            if( slack[i]<delta )    delta=slack[i],yy=i;//yy记录谁是最小误差
        }
        //delta是最小的误差,用于调整期望
        for(int i=0;i<=n;i++)
        {
            if( vb[i] ) wisha[mat[i]]-=delta,wishb[i]+=delta;
            else    slack[i]-=delta;
        }
        y=yy;//啥意思?yy记录的是最小误差,那么现在修改了期望值,所以可以直接匹配y
        if( mat[y]==-1 )    break;//直到找到一个人没有匹配
    }
    while( y )  mat[y]=mat[ pre[y] ],y=pre[y];
}
int km()
{
    init(wisha,0 ); init(wishb,0 ); init(mat,-1 );
    for(int i=1;i<=n;i++)
    {
        init(vb,0 );
        bfs(i);
    }
    int res=0;
    for(int i=1;i<=n;i++)   res+=cost[mat[i]][i];
    return res;
}
signed main()
{
    int ans1,ans2;
    cin >> n ;
    for(int i = 1; i<=n; i++){
        for(int j = 1; j<=n; j++){
            cin>>cost[i][j];
        }
    }
    for(int i = 1; i<=n; i++){
        for(int j = 1; j<=n; j++){
            int x;
            cin>>x;
            cost[j][i]*=x;
        }
    }
    ans1=km();
    cout<<ans1;
    return 0;
}

P2756 飞行员配对问题\color{#3498DB}\textbf{P2756 飞行员配对问题}

好吧,这次连 KM 都用不上了。

一发过。

魔改匈牙利即可。(就把我叫模板的调试的函数输出就得了)

似乎跑的还挺快……

C++
#include<iostream>
#include<cstring>
using namespace std;

int n,m,t,graph[610][610],link[610],vis[610];

void test_link(){
	for(int i = 1; i<=m; i++) if(link[i]) cout<<link[i]<<" "<<i<<'\n';
}

bool hungary_dfs(int x){
	for(int i = 1; i<=m; i++){
		if(graph[x][i]&&!vis[i]){
			vis[i]=1;
			if(!link[i]||hungary_dfs(link[i])){
				link[i]=x;
				return true;
			}
		}
	}
	return false;
}

int main(){
	cin>>n>>m;
	int u,v;
	cin>>u>>v;
	while(u!=-1&&v!=-1){
		graph[u][v]=1;
		cin>>u>>v;
	}
	int ans=0;
	for(int i = 1; i<=n; i++){
		memset(vis,0,sizeof vis);
		if(hungary_dfs(i)) ans++;
	}
    cout<<ans<<'\n';
	test_link();
	return 0;
}

P2071 座位安排\color{#3498DB}\textbf{P2071 座位安排}

还是匈牙利,只不过开个二维的 link[][] 而不是一维的(什么乱七八糟的 n×2n\times 2),但是邻接矩阵被卡了,vector 邻接表存即可。

又水一道蓝题。

C++
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;

int n,m,t,link[4010][2],vis[4010];
vector<int> vec[4010];

void test_link(){
	for(int i = 1; i<=n; i++) cout<<link[i][0]<<" "<<link[i][1]<<'\n';
	cout<<'\n';
}

bool _find(int x){
	for(auto vvv:vec[x]){
		if(!vis[vvv]){
			vis[vvv]=1;
			if(!link[vvv][0]||_find(link[vvv][0])){
				link[vvv][0]=x;
				return true;
			}
            if(!link[vvv][1]||_find(link[vvv][1])){
				link[vvv][1]=x;
				return true;
			}
		}
	}
	return false;
}

int main(){
	cin>>n;
	for(int i = 1; i<=n*2; i++){
		int u,v;
		cin>>u>>v;
		vec[i].push_back(u);
		vec[i].push_back(v);
	}
	int ans=0;
	for(int i = 1; i<=n*2; i++){
		memset(vis,0,sizeof vis);
		if(_find(i)) ans++;
	}
	//test_link();
	cout<<ans;
	return 0;
}

P3967 [TJOI2014] 匹配\color{#9d3dcf}\bf{P3967\,}\bf{[TJOI2014]}\,\text{匹配}

跑第一次 KM,将匹配的边删去,再跑 KM,如果值改变,这条边必须保存。

没sort边的顺序找半天……

link

C++
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int inf = 1e18;
const int maxn = 105;
int n, m, cost[maxn][maxn], mat[maxn], pre[maxn];
int wisha[maxn], wishb[maxn], va[maxn], vb[maxn], slack[maxn];
void init(int a[], int b)
{
	for (int i = 0; i <= n; i++)
		a[i] = b;
}
void bfs(int u)
{
	init(pre, 0);
	init(slack, inf);
	slack[0] = 0;
	int x = 0, y = 0, yy = 0, delta = 0;
	mat[y] = u; // 最开始0匹配u,为什么这么写,无关紧要,总要让一个开头嘛
	while (1)
	{
		x = mat[y], delta = inf, vb[y] = 1; // 现在是帮x找完美匹配
		for (int i = 1; i <= n; i++)
		{
			if (vb[i])
				continue; // 本次访问过,不用管了
			if (slack[i] > wisha[x] + wishb[i] - cost[x][i])
			{
				slack[i] = wisha[x] + wishb[i] - cost[x][i]; // 更新最小误差期望
				pre[i] = y;
			}
			if (slack[i] < delta)
				delta = slack[i], yy = i; // yy记录谁是最小误差
		}
		// delta是最小的误差,用于调整期望
		for (int i = 0; i <= n; i++)
		{
			if (vb[i])
				wisha[mat[i]] -= delta, wishb[i] += delta;
			else
				slack[i] -= delta;
		}
		y = yy; // 啥意思?yy记录的是最小误差,那么现在修改了期望值,所以可以直接匹配y
		if (mat[y] == -1)
			break; // 直到找到一个人没有匹配
	}
	while (y)
		mat[y] = mat[pre[y]], y = pre[y];
}
int Rq()
{
	init(wisha, 0);
	init(wishb, 0);
	init(mat, -1);
	for (int i = 1; i <= n; i++)
	{
		init(vb, 0);
		bfs(i);
	}
	int res = 0;
	for (int i = 1; i <= n; i++)
		res += cost[mat[i]][i];
	return res;
}

pair<int,int> test(int i)
{
	return (make_pair(mat[i],i));
}

int ans;

signed main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cost[i][j] = -inf;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> cost[i][j];
	ans=Rq();
	cout << ans << '\n';
	pair<int,int> bk[maxn];
	for(int i = 1; i<=n; i++)
		bk[i]=test(i);
	stable_sort(bk+1,bk+1+n);
	for(int i = 1; i<=n; i++){
		int RQ=cost[bk[i].first][bk[i].second];
		cost[bk[i].first][bk[i].second]=-inf;
		if(Rq()<ans){
			cout<<bk[i].first<<' '<<bk[i].second<<'\n';
		}
		cost[bk[i].first][bk[i].second]=RQ;
	}
	return 0;
}

P4329 [COICI2006−2007#1] Bond\color{#9d3dcf}\bf{P4329\,}\bf{[COICI2006-2007\#1]}\,\text{Bond}

emo 了,KM 被卡精度卡死了。

u1s1,题解有 KM 可以过去,但是我懒得看了。

状压香。

C++
#include<bits/stdc++.h>
using namespace std;
int n;
long double arr[150][150];
long double f[(1<<20)+2]={1};
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			scanf("%Lf",&arr[i][j]),arr[i][j]*=0.01;
	for(int i=0;i<(1<<n);i++)
	{
		int x=i,cnt=0;
		cnt=__builtin_popcount(x);
		for(int j=1;j<=n;j++)
			if(i&(1<<(j-1)))
				f[i]=max(f[i],f[i xor (1<<(j-1))]*arr[cnt][j]);
	}
	printf("%.6Lf",f[(1<<n)-1]*100);
	return 0;
}

P1894 [USACO4.2] 完美的牛栏 The Perfect Stall\color{#52c41a}\bf{P1894\,}\bf{[USACO4.2]}\,\text{完美的牛栏 The Perfect Stall}

被上面那道折磨完之后想找一道蓝~紫的魔改二分图回复一下状态,没找到QWQ,那就只能用这题了……

C++
#include<iostream>
#include<cstring>
using namespace std;

int n,m,graph[610][610],link[610],vis[610];

void test_link(){
	for(int i = 1; i<=n; i++) cout<<link[i]<<" ";
	cout<<'\n';
}

bool RQ(int x){
	for(int i = 1; i<=m; i++){
		if(graph[x][i]&&!vis[i]){
			vis[i]=1;
			if(!link[i]||RQ(link[i])){
				link[i]=x;
				return true;
			}
		}
	}
	return false;
}

int main(){
	cin>>n>>m;
	for(int i = 1; i<=n; i++){
		int x;
		cin>>x;
		for(int j = 1; j<=x; j++){
			int u;
			cin>>u;
			graph[i][u]=1;
		}
	}
	int ans=0;
	for(int i = 1; i<=n; i++){
		memset(vis,0,sizeof vis);
		if(RQ(i)) ans++;
	}
	//test_link();
	cout<<ans;
	return 0;
}

1 月 22 日

P3033 [USACO11NOV]  Cow Steeplechase G\color{#3498DB}\bf{P3033\,}\bf{[USACO11NOV]}\,\text{ Cow Steeplechase G}

link

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iomanip>
#define ll long long
using namespace std;

const int N = 1e5 + 5;

int n, n1, tot, nd[N], cnt;
int x1[N], x2[N], y1[N], y2[N];
int vis[N];
int type[N];
int cp[N];

vector<int> link[N];

inline bool check(int i,int j){
	return x1[i] >= x1[j] && x2[i] <= x2[j] && y1[j] >= y1[i] && y2[j] <= y2[i];
}

inline void add(int u, int v)
{
	link[u].push_back(v);
	return;
}

int dfs(int u)
{
    //cout<<"QWQ\n";
	vis[u] = 1;
	for (auto v : link[u])
	{
        //cout<<"AK IOI\n";
		if (vis[cp[v]])
			continue;
		if (cp[v]==0 || dfs(cp[v]))
			return (cp[v] = u), true;
	}
	return 0;
}

inline void read(int i)
{
	cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie();
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		read(i);
		if (x1[i] > x2[i])
			swap(x1[i], x2[i]);
		if (y1[i] > y2[i])
			swap(y1[i], y2[i]);
		type[i] = (x1[i] == x2[i] ? 1 : 2);
		n1 += (x1[i] == x2[i] ? 1 : 0);
	}
	for (int i = 1; i < n; i++)
		for (int j = i + 1; j <= n; j++)
		{
			if ((type[i] ^ type[j]) && (type[i] == 1)){
                //cout<<"kkk\n";
				if (check(i,j)){
                    //cout<<"kkk\n";
					add(i, j + n1);
                }
            }
			if ((type[i] ^ type[j]) && (type[i] == 2)){
                //cout<<"kkk\n";
				if (check(j,i)){
                    //cout<<"kkk\n";
					add(j, i + n1);
                }
            }
		}
	for (int i = 1; i <= n; i++)
	{
		if (type[i] == 2)
			continue;
		memset(vis, 0, sizeof(vis));
        //cout<<dfs(i);
        //cout<<"qwq\n";
		if (dfs(i))
			cnt++;
	}
    //for(int i = 1; i<=n; i++) cout<<type[i]<<endl;
    //for(int i = 1; i<=n; i++) cout<<link[i].size()<<endl;
	cout << n - cnt;
	return 0;
}

找到了,平复一下昨天心情。

1 月 24 日

P3376【模板】网络流最大流\color{#3498db}\textbf{P3376【模板】网络流最大流}

tot = 1不是 0 啊!

C++
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#define int long long
using namespace std;

const int inf = 1<<29;
const int N=5510,M=25510;
int head[N],ver[M],edge[M],Next[M],v[N],incf[N],pre[N];
int n,m,t,s,tot=1,maxflow;

inline void add(int x,int y,int z) {
	ver[++tot]=y;
	edge[tot]=z;
	Next[tot]=head[x];head[x]=tot;
	ver[++tot]=x;
	edge[tot]=0;
	Next[tot]=head[y];head[y]=tot;
	return ;
}

bool bfs() {
	memset(v, 0, sizeof(v));
	queue<int> q;
	q.push(s);
	v[s]=1;
	incf[s]=inf;
	while(q.size()) {
		int x = q.front();
		q.pop();
		for(int i = head[x]; i; i = Next[i]) {
			if(edge[i]) {
				int y = ver[i];
				if(v[y]) continue;
				incf[y]=min(incf[x],edge[i]);
				pre[y]=i;
				q.push(y);
                v[y]=1;
				if(y==t) return 1;
			}
		}
	}
	return 0;
}

inline void update() {
	int x=t;
	while(x!=s) {
		int i = pre[x];
		edge[i]-=incf[t];
		edge[i^1]+=incf[t];
		x=ver[i^1];
	}
	maxflow+=incf[t];
	return ;
}

int flag[N][N];

signed main() {
	cin >> n >> m >> s >> t;
	for(int i = 1; i<=m; i++) {
		int u,v,c;
		cin>>u>>v>>c;
		//add(u,v,c);
		if(!flag[u][v]){
			add(u,v,c);
			flag[u][v]=tot;
		}else{
			edge[flag[u][v]-1]+=c;
		}
	}
	while(bfs()) update();
	cout << maxflow;
	return 0;
}

1 月 26 日

P6268 [SHOI2002] 舞会\color{#3498db}\textbf{P6268 [SHOI2002] 舞会}

水题,柯尼希定理了解一下。

最大点覆盖=最大匹配数

最大点独立集=总点数-最大点覆盖=总点数-最大匹配数

C++
#include<bits/stdc++.h>
using namespace std;

int n,m,graph[2010][2010],link[2010],vis[2010];

bool _find(int x){
	for(int i =0; i<n; i++){
		if(graph[x][i]&&!vis[i]){
			vis[i]=1;
			if(!link[i]||_find(link[i])){
				link[i]=x;
				return true;
			}
		}
	}
	return false;
}

int main(){
	cin>>n>>m;
	for(int i = 1; i<=m; i++){
		int u,v;
		cin>>u>>v;
		graph[u][v]=1;
        graph[v][u]=1;
	}
	int ans=0;
	for(int i = 0; i<n; i++){
		memset(vis,0,sizeof vis);
		if(_find(i)) ans++;
	}
	//test_link();
	cout<<n-ans/2;
	return 0;
}

1 月 27 日

P1032 [NOIP2002 提高组]字串变换\color{#52c41a}\textbf{P1032 [NOIP2002 提高组]字串变换}

C++
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;

typedef long long i64;
const int INF=2147483647;
const int MAXN = 6+3;
string X[MAXN],Y[MAXN];
int n = 1;
int step=0;

int main(){
	string A,B;
	cin >> A >> B;
	while(cin>>X[n]>>Y[n]) ++n;
	--n;
	queue<string> Q;
	map<string,int> M;
	Q.push(A),M[A]=0;
	while(Q.size()){
		string u = Q.front();
		Q.pop();
		if(u==B){
			cout<<M[u];
			return 0;
		}
		if(M[u]==10) continue;
		int l = u.size();
		for(int i = 0; i<l; i++){
			for(int j = 1; j<=n; j++){
				if(u.substr(i,X[j].length()) == X[j]){
					string v=u.substr(0,i) + Y[j] + u.substr(i + X[j].length());
					if(!M.count(v)){
						M[v]=M[u]+1;
						Q.push(v);
					}
				}
			}
		}
		step++;
	}
	cout<<"NO ANSWER!";
	return 0;
}

P1120 小木棍\color{#3498db}\textbf{P1120 小木棍}

什么【】搜索题。

C++
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long i64;
const int INF = 2147483647;
const int MAXN = 80;
int n, A[MAXN];
bool V[MAXN];
int B[MAXN];
int maxl, totl;

bool dfs(int ans, int step, int rest, int last)
{
	if (step == n)
		return true;
	bool f = false;
	for (int i = min(rest,min(maxl,last)); i >= 1; i--)
	{
		if (B[i])
		{
			--B[i];
			if (i == rest)
			{
				f |= dfs(ans, step + 1, ans, ans);
				++B[i];
				return f;
			}
			else if (rest == ans)
			{
				f |= dfs(ans, step + 1, rest - i, i);
				++B[i];
				return f;
			}
			else
			{
				f |= dfs(ans, step + 1, rest - i, i);
				++B[i];
				if (f)
					return f;
			}
		}
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> A[i];
		maxl = max(maxl, A[i]);
		totl += A[i];
		B[A[i]]++;
	}
	for (int l = maxl;; ++l)
	{
		if (totl % l)
			continue;
		if (dfs(l, 0, l, l))
		{
			cout << l;
			return 0;
		}
	}
	return 0;
}

P1092 [NOIP2004 提高组] 虫食算\color{#3498db}\textbf{P1092 [NOIP2004 提高组] 虫食算}

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long i64;
const int INF = 2147483647;
const int MAXN = 50;
char A[MAXN], B[MAXN], C[MAXN];
bool V[MAXN];
int n, X[MAXN];

bool check(){
	for(int i = 1 ;i<=n; i++){
		if(X[A[i]]!=-1&&X[B[i]]!=-1&&X[C[i]]!=-1){
			if((X[A[i]]+X[B[i]])%n==X[C[i]]) continue;
			if((X[A[i]]+X[B[i]]+1)%n==X[C[i]]) continue;
			return false;
		}
	}
	return true;
}

void dfs(int step, int inc)
{
	if (!step)
	{
		if (!inc)
		{
			for (int i = 1; i <= n; i++)
			{
				cout << X[i] << " \n"[i == n];
			}
            return ;
		}
	}
	else
	{
		for (int i = 0; i < n; i++)
		{
			bool fi = X[A[step]] == -1;
			if (fi == false)
				i = X[A[step]];
			else{
				if(!V[i]) X[A[step]] = i, V[i]=true;
				else continue;
			}

			for (int j = 0; j < n; j++)
			{
				bool fj = X[B[step]] == -1;
				if (fj == false)
					j = X[B[step]];
				else{
					if(!V[j]) X[B[step]]=j,V[j]=true;
					else continue;
				}
				for (int k = 0; k < n; k++)
				{
					bool fk = X[C[step]] == -1;
					if (fk == false)
						k = X[C[step]];
					else
						if(!V[k]) X[C[step]]=k,V[k]=true;
						else continue;
					if((i+j+inc)%n==k&&check()) dfs(step-1,(i+j+inc)/n);
					if (fk == false)
						break;
					else
						X[C[step]] = -1,V[k]=false;
				}
				if (fj == false)
					break;
				else
					X[B[step]] = -1,V[j]=false;
			}
			if (fi == false)
				break;
			else
				X[A[step]] = -1,V[i]=false;
		}
	}
}

int main()
{
	cin>>n;
	for(int i = 1; i<=n; i++) X[i]=-1;
	scanf("%s%s%s",A+1,B+1,C+1);
	for(int i = 1; i<=n;i ++){
		A[i] = A[i] - 'A' + 1,
        B[i] = B[i] - 'A' + 1,
        C[i] = C[i] - 'A' + 1;
	}
	dfs(n,0);
	return 0;
}

P1763 埃及分数\color{#3498db}\textbf{P1763 埃及分数}

奶奶滴,降难度是吧,痛失水紫。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a,b,depth,temp[2021],ans[2021];
bool flag=false;

inline int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}

void dfs(int newa,int newb,int step){
    if(step+1==depth){
        if(newa==1){
            temp[step+1]=newb;flag=true;
            if(temp[step+1]<ans[step+1]||ans[step+1]==0){
                memcpy(ans,temp,sizeof(temp));
            }
        }
        return;
    }
    int first=newb%newa?newb/newa+1:newb/newa;
    for(int i = max(temp[step]+1,first);i<=ceil(newb/newa)*(depth-step);++i){
        temp[step+1]=i;
        int nexta,nextb,g;
        nexta=newa*i-newb,nextb=newb*i;
        g=gcd(nexta,nextb);
        dfs(nexta/g,nextb/g,step+1);
    }
    return;
}

signed main(){
    cin>>a>>b;
    int g=gcd(a,b);
    temp[0]=1;
    for(depth=1;;++depth){
        dfs(a/g,b/g,0);
        if(flag) break;
    }
    for(int i = 1; i<=depth; ++i){
        cout<<ans[i]<<" ";
    }
    return 0;
}

P4799 [CEOI2015 Day2] 世界冰球锦标赛\color{#3498db}\textbf{P4799 [CEOI2015 Day2] 世界冰球锦标赛}

C++
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
#define aaa (1<<20)+3
int n, m;
int W[aaa];
int w;
int a[51000];
int tot = 0;

signed main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	int half = n >> 1;
	int u = 1 << half, v = 1 << (n - half);
	for (int i = 0; i < u; i++)
	{
		w = 0;
		for (int j = 1; j <= half; j++)
		{
			if (i & (1 << (j - 1)))
				w += a[j];
		}
		W[++tot] = w;
	}
	stable_sort(W + 1, W + 1 + tot);
	int ans = 0;
	for (int i = 0; i < v; ++i)
	{
		w = 0;
		for (int j = 1; j <= n - half; ++j)
		{
			if (i & (1 << j - 1))
				w += a[half + j];
		}
		ans += upper_bound(W + 1, W + 1 + tot, m - w) - W - 1;
	}
	cout << ans;
	return 0;
}

P2324 [SCOI2005]骑士精神\color{#3498db}\textbf{P2324 [SCOI2005]骑士精神}

IDA*

C++
#include <iostream>
#include <queue>
#include <map>
using namespace std;

typedef long long i64;
const int INF = 2147483647;
const int E[6][6] = {
	{},
	{0, 1, 1, 1, 1, 1},
	{0, 0, 1, 1, 1, 1},
	{0, 0, 0, 2, 1, 1},
	{0, 0, 0, 0, 0, 1},
	{0, 0, 0, 0, 0, 0}};

const int D[8][2] = {
	{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2}};

struct Node
{
	int W[6][6], x, y, step;
	int to_int()
	{
		int h = 0;
		for (int i = 1; i <= 5; ++i)
			for (int j = 1; j <= 5; ++j)
				h = h * 2 + W[i][j];
		h = h * 6 + x;
		h = h * 6 + y;
		return h;
	}
	int estimate()
	{
		int h = 0;
		for (int i = 1; i <= 5; ++i)
			for (int j = 1; j <= 5; ++j)
				if (!(i == x && j == y))
					h += E[i][j] != W[i][j];
		return h;
	}
	bool operator <(const Node x) const { return false; }
};

using Pair = pair<int, Node>;

char readc(){
    char c; while(!isgraph(c = getchar())); return c;
}

bool ida(int limit, Node s)
{
	int e = s.estimate();
	if (e == 0)
	{
		cout << s.step << '\n';
		return true;
	}
	else
	{
		if (s.step + e > limit)
			return false;
		else
		{
			int x = s.x;
			int y = s.y;
			for (int d = 0; d < 8; ++d)
			{
				int nx = x + D[d][0];
				int ny = y + D[d][1];
				if (1 <= nx && nx <= 5 && 1 <= ny && ny <= 5)
				{
					Node v = s;
					swap(v.W[x][y], v.W[nx][ny]);
					v.x = nx;
					v.y = ny;
					v.step = s.step + 1;
					if (ida(limit, v))
						return true;
				}
			}
		}
	}
	return false;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		Node s;
		s.step=0;
		for (int i = 1; i <= 5; i++)
		{
			for (int j = 1; j <= 5; j++)
			{
				char c = readc();
				if (c == '0')
					s.W[i][j] = 0;
				if (c == '1')
					s.W[i][j] = 1;
				if (c == '*')
					s.W[i][j] = 0, s.x = i, s.y = j;
			}
		}
		int ans = 0;
		for (; ans <= 15; ans++)
		{
			if (ida(ans, s))
				break;
		}
		if (ans == 16)
			puts("-1");
	}
	return 0;
}

P1379 八数码难题\color{#52c41a}\textbf{P1379 八数码难题}

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
#include <queue>
using namespace std;

int n;
map<int, int> M;
queue<int> BFS;
int mapp[5][5];
int dis[5][3]={
	{0,-1},{-1,0},{0,1},{1,0}
};

int main()
{
	cin >> n;
	M[n] = 0;
	BFS.push(n);
	while (BFS.size())
	{
		int u = BFS.front();
		BFS.pop();
        int n=u;
		if (u == 123804765)
			break;
		memset(mapp, 0, sizeof(mapp));
		int x, y;
		for (int i = 3; i >= 1; i--)
		{
			for (int j = 3; j >= 1; j--)
			{
				mapp[i][j] = n % 10, n /= 10;
				if (!mapp[i][j])
					x = i, y = j;
			}
		}
		for (int i = 0; i < 4; i++)
		{
			int nx=x+dis[i][0];
			int ny=y+dis[i][1];
			if(nx>=1&&nx<=3&&ny>=1&&ny<=3){
				swap(mapp[x][y],mapp[nx][ny]);
				int v=0;
				for(int i = 1; i<=3; i++){
					for(int j = 1; j<=3; j++){
						v=(v<<1)+(v<<3)+mapp[i][j];
					}
				}
				if(!M.count(v)){
					M[v]=M[u]+1;
					BFS.push(v);
				}
				swap(mapp[x][y],mapp[nx][ny]);
			}
		}
	}
	cout<<M[123804765];
	return 0;
}

1 月 28 日

P2534 [AHOI2012] 铁盘整理\color{#3498db}\textbf{P2534 [AHOI2012] 铁盘整理}

C++
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

const int MAXN=50;
int a[MAXN],n;
int f[MAXN];
bool flag;
int depth;

int estimate(){
	int ret=0;
	for(int i = 1; i<=n;i++){
		if(abs(a[i]-a[i+1])!=1) ++ret;
	}
	return ret;
}

void dfs(int step,int pre){
	int ccf=estimate();
	if(step+ccf>depth) return;
	if(step>depth) return;
	if(flag) return;
	if(!ccf){
		flag=true;
		printf("%d\n",step);
		return;
	}
	for(int i=2;i<=n;i++)
		if(i^pre){
			reverse(a+1,a+i+1);
			dfs(step+1,i);
			reverse(a+1,a+i+1);
		}
}

int main(){
	cin>>n;
	for(int32_t i = 1; i<=n; i++){
		cin>>a[i];
		f[i]=a[i];
	}
	stable_sort(f+1,f+n+1);
	for(int i = 1; i<=n; i++){
		a[i]=lower_bound(f+1,f+n+1,a[i])-f;
	}
	a[n+1]=n+1;
	for(depth=0;depth<=(n<<1)-2;depth++){
		flag=false;
		dfs(0,0);
		if(flag) break;
	}
	return 0;
}

P2357 守墓人\color{#ffc116}\textbf{P2357 守墓人}

小黑子露出鸡脚了把。

C++
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

#define int long long

const int N = 500050;

int n, m;
int a[N], L[N], tag[N], R[N];
int sum[N], lz[N];
int len;

class CHICKEN
{
private:
	static void CXK_1(int l, int r, int k)
	{
		if (tag[l] == tag[r])
		{
			sum[tag[l]] += ((r - l + 1) * k);
			for (int i = l; i <= r; i++)
			{
				a[i] += k;
			}
			return;
		}
		sum[tag[l]] += ((R[tag[l]] - l + 1) * k);
		for (int i = l; i <= R[tag[l]]; i++)
		{
			a[i] += k;
		}
		for (int i = tag[l] + 1; i < tag[r]; i++)
		{
			lz[i] += k, sum[i] += (R[i] - L[i] + 1) * k;
		}
		sum[tag[r]] += ((r - L[tag[r]] + 1) * k);
		for (int i = L[tag[r]]; i <= r; i++)
		{
			a[i] += k;
		}
		return;
	}

	static int_least64_t CXK_2(int l, int r)
	{
		int ans = 0;
		if (tag[l] == tag[r])
		{
			for (int i = l; i <= r; i++)
				ans += a[i] + lz[tag[i]];
			return ans;
		}
		for (int i = l; i <= R[tag[l]]; i++)
			ans += (a[i] + lz[tag[i]]);
		for (int i = tag[l] + 1; i < tag[r]; i++)
		{
			ans += sum[i];
		}
		for (int i = L[tag[r]]; i <= r; i++)
			ans += (a[i] + lz[tag[i]]);
		return ans;
	}

public:
	static void change(int l, int r, int k)
	{
		CXK_1(l, r, k);
		return;
	}
	static int_fast64_t query(int l, int r)
	{
		return CXK_2(l, r);
	}
};

signed main()
{
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	len = ((int)(sqrt((double)(n))));
	for (int i = 1; i <= n; i++)
		tag[i] = /*(int)(ceil(i/(len*1.0)))*/ (i - 1) / len + 1;
	for (int i = 1; i <= tag[n]; i++)
	{
		L[i] = R[i - 1] + 1, R[i] = min(n, L[i] + len - 1);
	}
	for (int_least32_t i = 1; i <= n; i++)
		cin >> a[i];
	for (int_fast16_t i = 1; i <= tag[n]; i++)
	{
		for (int j = L[i]; j <= R[i]; j++)
		{
			sum[i]+=a[j];
		}
	}
	int_least16_t opt;
	int l,r,k;
	while (m--)
	{
		cin>>opt;
		switch (opt)
		{
		case 1:
			cin>>l>>r>>k;
			CHICKEN::change(l,r,k);
			break;
		case 2:
			cin>>k;
			CHICKEN::change(1,1,k);
			break;
		case 3:
			cin>>k;
			CHICKEN::change(1,1,-k);
			break;
		case 4:
			cin>>l>>r;
			cout<<CHICKEN::query(l,r)<<'\n';
			break;
		case 5:
			cout<<CHICKEN::query(1,1)<<'\n';
			break;
		default:
			cout<<"Chtholly\n";
			break;
		}
	}
	return 0;
}

1 月 29 日

P3370 【模板】字符串哈希\color{#f39c11}\textbf{P3370 【模板】字符串哈希}

C++
#include <bits/stdc++.h>
using namespace std;

map<string,bool> M;
string str;
int n;

int main(){
    int ans=0;
    cin>>n;
    while(n--){
        cin>>str;
        if(!M[str]){
            M[str]=1;
            ans++;
        }
    }
    cout<<ans;
    return 0;
}

P4391 [BOI2009] Radio Transmission 无线传输\color{#52c41a}\textbf{P4391 [BOI2009] Radio Transmission 无线传输}

C++
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int N = 1e6 + 5;
int n, ans, s1, s2, M;
char ch[N];
int main()
{
	scanf("%d%s", &n, ch + 1);
	ans = n;
	M = 1;
	for (int i = 1; i < n; ++i)
	{
		s1 = s1 * 13331 + ch[i];
		s2 = ch[n - i + 1] * M + s2;
		M *= 13331;
		if (s1 == s2)
			ans = n - i;
	}
	printf("%d\n", ans);
	return 0;
}

P3375 【模板】 KMP 字符串匹配\color{#ffc116}\textbf{P3375 【模板】 KMP 字符串匹配}

C++
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int UNF=1e6+3;
char S[UNF],T[UNF];
int n,m,N[UNF],M[UNF];

int main(){
    scanf("%s",S+1);
    scanf("%s",T+1);
    n=strlen(S+1);
    m=strlen(T+1);
    N[1]=0;
    for(int i = 2; i<=m; i++){
        int p=N[i-1];
        while(T[p+1]!=T[i]&&p) p=N[p];
        if(!p){
            if(T[p+1]==T[i])N[i]=p+1;
            else N[i]=0;
        }else N[i]=p+1;
    }
    for(int i = 1; i<=n; i++){
        int p = M[i-1];
        while(T[p+1]!=S[i]&&p!=0) p=N[p];
        if(T[p+1]==S[i]) M[i]=p+1;
        else M[i]=0;
        if(M[i]==m) printf("%d\n",i-m+1);
    }
    for(int i = 1; i<=m; i++){
        printf("%d%c",N[i]," \n"[i==m]);
    }
    return 0;
}
原文