01(5.10 00:00~5.17 23.59)

· · 休闲·娱乐

坚持的第二周。

NOIP 2013 模拟(补档 00)

这场是简单的,另外加上基本上都做过,没啥难点速通了。但是华容道如果跑不过 9e8 的数据需要套上 SPFA 啥的图论建模优化估计还是挺坐牢的。

转圈游戏

式子推出来就结束了:(10^km+x) \mod n。推错了一遍害得我神经地笑了下,简单题也得好好写。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ui long long
#define N 1000005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, m, k, x;
ui qp(ui a, ui b) {
    ui ans=1;
    while(b) {
        if(b&1) ans=ans*a%n;
        a=a*a%n, b>>=1;
    }
    return ans;
}
int main() {
    read(n, m, k, x);
    x=(m*qp(10, k)+x)%n;
    write(x);LF;
    return 0;
}
/*
3 6 9 2 5 8 1 4 7 0
*/

::::

积木大赛

先开的这个,所以先说这个。答案是 \sum_{i=1}^n\max(a_i-a_{i-1},~0),默认 a_0 为 0。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 1000005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, a[N], s, c;
int main() {
    read(n);
    for(ui i=1; i<=n; ++i) {
        read(a[i]);
        if(a[i]>=a[i-1]) c=a[i];
        else s+=c-a[i], c=a[i];
    }
    write(s+c);LF;
    return 0;
}

::::

花匠

这个第一眼感觉很简单,感觉咋写都能过,所以瞎写了会儿发现这个东西就是统了个类似于“山峰”“山谷”的玩意,去重后统计,因为连续相同的多余的无意义,式子是 \sum_{i=1}^n\left[a_i>a_{i-1}==a_i<a_{i+1}\right],n 和 a 是去重完后的,== 表示判断两端返回的布尔值是否相等。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 2000005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, a[N], cnt=2, c=1;
bool f1, f2;
void solve() {
    read(n);
    for(ui i=1; i<=n; ++i)
        read(a[i]);
    for(ui i=2; i<=n; ++i) if(a[i]!=a[i-1]) a[++c]=a[i];
    n=c;
    for(ui i=2; i<n; ++i) {
        if(a[i]>a[i-1]&&a[i]>a[i+1]) ++cnt;
        if(a[i]<a[i-1]&&a[i]<a[i+1]) ++cnt;
    }
    if(n==1) --cnt;
    write(cnt);
}
int main() {
    solve();
    return 0;
}

::::

货车运输

开始做之前做过的题,这个直接默写了,没啥技术含量,不讲了。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define ui unsigned int
#define N 200005
using namespace std;
template<typename T> void read(T& x) {
    x=0;
    char ch=getchar();
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
template<typename T, typename... Args> void read(T& x, Args&... args) {
    read(x);
    read(args...);
}
template<typename T> void write(T x) {
    if(x==0) return (void)putchar('0');
    ui a[25], t=0;
    while(x) a[++t]=x%10, x/=10;
    while(t) putchar(a[t--]|48);
}
struct Edge { ui to, nxt, w; } edge[N<<1];
struct EDGE {
    ui x, y, z;
    void Rd() {
        read(x, y, z);
    }
    bool operator < (const EDGE& a) const {
        return z>a.z;
    };
} e[N];
ui n, m, x, y, head[N], idx, f[N], fat[N][16], g[N][16], dep[N];
void add(const ui u, const ui v, const ui w) {
    edge[++idx].to=v, edge[idx].nxt=head[u], edge[idx].w=w, head[u]=idx;
}
ui Find(const ui x) {
    return x==f[x] ? x : f[x]=Find(f[x]);
}
void dfs(const ui u, const ui fa) {
    fat[u][0]=fa, dep[u]=dep[fa]+1;
    for(ui i=1, j=0; i<16; ++i, ++j)
        fat[u][i]=fat[fat[u][j]][j], g[u][i]=min(g[u][j], g[fat[u][j]][j]);
    for(ui i=head[u]; i; i=edge[i].nxt) {
        const ui v=edge[i].to;
        if(v==fa) continue;
        g[v][0]=edge[i].w;
        dfs(v, u);
    }
}
ui fun(ui x, ui y) {
    if(dep[x]<dep[y]) swap(x, y);
    ui r=dep[x]-dep[y], res=1e9, t=0;
    while(r) {
        if(r&1) res=min(res, g[x][t]), x=fat[x][t];
        ++t, r>>=1;
    }
    if(x==y) return res;
    for(ui i=15; ~i; --i)
        if(fat[x][i]!=fat[y][i])
            res=min(res, min(g[x][i], g[y][i])), x=fat[x][i], y=fat[y][i];
    return min(res, min(g[x][0], g[y][0]));
}
int main() {
    read(n, m);
    for(ui i=1; i<=n; ++i) f[i]=i;
    for(ui i=1; i<=m; ++i) e[i].Rd();
    sort(e+1, e+1+m);
    for(ui i=1; i<=m; ++i) {
        x=Find(e[i].x), y=Find(e[i].y);
        if(x==y) continue;
        add(e[i].x, e[i].y, e[i].z);
        add(e[i].y, e[i].x, e[i].z);
        f[y]=x;
    }
    for(ui i=1; i<=n; ++i) if(Find(i)==i) dfs(i, 0);
    read(m);
    while(m--) {
        read(x, y);
        if(Find(x)!=Find(y)) puts("-1");
        else write(fun(x, y)), LF;
    }
    return 0;
}

::::

火柴排队

之前做过,但是赛场上急急瞎写,但是过了。它本质上是个类似于逆序对的玩意,我没写这个,思路可能大致相同。我做了个类似于前缀基准的东西,这个基准是表示这个位置是否换到正确位置去了,由于我们从前往后固定,所以我们可以认为这里成空位了,并且让整体后移了一下,你用树状数组把基准动态统下来,你对着这个基准推一下式子就做好了。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 1000005
#define mod 99999997
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, a[N], b[N], c[N], g[N], f[N], ans, cnt, need[N], Cnt[N];
map<ui, ui> p;
bool cmp(ui x, ui y) {
    return a[x]<a[y];
}
void update(ui i) {
    for(; i<=n; i+=(i&-i)) ++f[i];
}
ui getsum(ui i) {
    ui res=0;
    while(i) res+=f[i], i-=(i&-i);
    return res;
}
ui ad(const ui x) {
    return x<mod ? x : x-mod;
}
vector<ui> G[N];
int main() {
    read(n);
    for(ui i=1; i<=n; ++i) {
        read(a[i]);
        g[i]=i;
    }
    sort(g+1, g+1+n, cmp); 
    for(ui i=1; i<=n; ++i) {
        read(b[i]);
        c[i]=b[i];
    }
    sort(c+1, c+1+n);
    p[c[1]]=cnt=1;
    for(ui i=2; i<=n; ++i) if(c[i]!=c[i-1]) p[c[i]]=++cnt;
    for(ui i=1; i<=n; ++i)
        need[g[i]]=p[c[i]];
    for(ui i=1; i<=n; ++i)
        G[b[i]=p[b[i]]].push_back(i);
    for(ui i=1; i<=n; ++i) {
        const ui j=G[need[i]][Cnt[need[i]]++];
        ans=ad(ans+getsum(n-j+1)+j-i);
        update(n-j+1);
    }
    write(ans);
    return 0;
}

::::

华容道

没做过,场上如果写正解我肯定死亡了,但是数据量太小,暴力 bfs 过去了,没啥技术含量,看看代码。 ::::success[代码]

#include <cstdio>
#include <cstring>
#define LF putchar('\n')
#define ui int
#define N 35
#define M 1000005
#define INF 0x3f3f3f3f
const ui dx[5]= { 0, 1, 0, -1, 0 }, dy[5]= { 1, 0, -1, 0, 0 };
template<typename T> void read(T& x) {
    x=0;
    char ch=getchar();
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
template<typename T, typename... Args> void read(T& x, Args&... args) {
    read(x);
    read(args...);
}
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; }
    if(x<0) { putchar('-'); x=-x; }
    ui a[9], t=0;
    while(x) a[++t]=x%10, x/=10;
    while(t) putchar(a[t--]|48);
}
ui n, m, q, a[N][N], x, y, sx, sy, ex, ey, f[N][N][N][N], h, t, qx[M], qy[M], qx2[M], qy2[M], v;
void add(const ui a, const ui b, const ui x, const ui y, const ui v) {
    qx[++t]=a, qy[t]=b, qx2[t]=x, qy2[t]=y, f[a][b][x][y]=v;
}
bool check(const ui x, const ui y) {
    return x>0&&y>0&&x<=n&&y<=m&&a[x][y];
}
ui bfs() {
    memset(f, INF, sizeof f);
    h=t=0, add(sx, sy, x, y, 0);
    if(sx==ex&&sy==ey) return 0;
    while(h<t) {
        sx=qx[++h], sy=qy[h], x=qx2[h], y=qy2[h], v=f[sx][sy][x][y]+1;
        for(ui i=0, xx=x+dx[i], yy=y+dy[i]; i<4; xx=x+dx[++i], yy=y+dy[i])
            if(xx==sx&&yy==sy) {
                if(f[x][y][sx][sy]<INF) continue;
                if(x==ex&&y==ey) return v;
                add(x, y, sx, sy, v);
            }
            else if(check(xx, yy)&&f[sx][sy][xx][yy]==INF) add(sx, sy, xx, yy, v);
    }
    return -1;
}
int main() {
    read(n, m, q);
    for(ui i=1; i<=n; ++i) for(ui j=1; j<=m; ++j) read(a[i][j]);
    while(q--) {
        read(x, y, sx, sy, ex, ey);
        if(a[x][y]&&a[sx][sy]&&a[ex][ey]) write(bfs()), LF;
        else puts("-1");
    }
    return 0;
}

::::

NOIP 2011 模拟

成乐子了,速通完简单题后看 C,明显深度很小还写了个广搜上去死亡了。

铺地毯

如题面,找最后一个能盖到这个点的地毯输出,不然无解。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 10005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, a[N], b[N], g[N], k[N], x, y;
void solve() {
    read(n);
    for(ui i=1; i<=n; ++i)
        read(a[i], b[i], g[i], k[i]);
    read(x, y);
    for(ui i=n; i; --i) {
        if(a[i]<=x&&b[i]<=y&&a[i]+g[i]>x&&b[i]+k[i]>y) {
            write(i);
            return ;
        }
    }
    puts("-1");
}
int main() {
    solve();
    return 0;
}

::::

选择客栈

我想+写比我写这个总结快我能不总结这道题吗。你发现有咖啡馆可以的话我们就可以把这个咖啡馆即以前的酒店全部扔到桶里,你发现我们可以保证每个点就被扔一遍,所以总体还是 \Theta(n) 的,只遍历有东西的桶,然后你直接加桶里的数字,但是如果当前点也进桶了,记得 -1。 ::::success[代码]

#include <bits/stdc++.h>
#define LF putchar('\n')
#define SP putchar(' ')
#define ll long long
#define ui unsigned int
#define N 2000005
#define M 10005
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
template<typename T> void read(T& x) {
    x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); }
    while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x;
}
template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); }
template<typename T> void write(T x) {
    if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; }
    ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48);
}
template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } }
ui n, k, p, a, b, c, pre, x[M], s[M], G[M];
ll ans;
void solve() {
    read(n, k, p);
    for(ui i=1; i<=n; ++i) {
        read(a, b);
        ++G[a];
        if(G[a]==1) x[++c]=a;
        if(b<=p) {
            for(ui j=1; j<=c; ++j)
                s[x[j]]+=G[x[j]], G[x[j]]=0;
            c=0;
            ans+=s[a]-1;
        }
        else ans+=s[a];
    }
    write(ans);
}
int main() {
    solve();
    return 0;
}

::::

计算系数

::::success[代码] ```cpp #include <bits/stdc++.h> #define LF putchar('\n') #define SP putchar(' ') #define ll long long #define ui unsigned int #define N 2000005 #define INF 0x3f3f3f3f #define mod 10007 using namespace std; template<typename T> void read(T& x) { x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); } while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x; } template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); } template<typename T> void write(T x) { if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; } ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48); } template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } } ll a, b, k, n, m, fac[N], df[N]; ll qp(ll a, ll b) { ll res=1; while(b) { if(b&1) res=res*a%mod; a=a*a%mod, b>>=1; } return res; } int main() { read(a, b, k, n, m); fac[0]=df[0]=1; for(ui i=1; i<=k; ++i) fac[i]=fac[i-1]*i%mod, df[i]=qp(fac[i], mod-2); write(qp(a, n)*qp(b, m)%mod*fac[k]%mod*df[n]%mod*df[m]%mod); return 0; } ``` :::: ## 聪明的质监员 脑梗了想了半天,你直接二分这个 $W$,少了增大,大了减小,里面写个有点脑子的 $\Theta(n+m)$ 暴力就完事了,具体的,你对着当前有用的矿石数量做个前缀和,然后遍历每个区间算就完事了。 ::::success[代码] ```cpp #include <bits/stdc++.h> #define LF putchar('\n') #define SP putchar(' ') #define ll long long #define ui unsigned int #define N 1000005 #define INF 0x3f3f3f3f #define mod 1000000007 using namespace std; template<typename T> void read(T& x) { x=0; char ch=getchar(); bool f=0; while(ch<'0' || ch>'9') { if(ch=='-') f=1; ch=getchar(); } while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); if(f) x=-x; } template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); } template<typename T> void write(T x) { if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; } ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48); } template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } } ui n, m, w[N], x[N], y[N], cnt[N], g[N]; ll s, v[N], sum[N], ans=1e12; bool cmp(ui a, ui b) { return w[a]>w[b]; } ll ABS(const ll a, const ll b) { return a>b ? a-b : b-a; } ll check(const ui W) { for(ui i=1; i<=n; ++i) { sum[i]=sum[i-1], cnt[i]=cnt[i-1]; if(w[i]>=W) sum[i]+=v[i], ++cnt[i]; } ll res=0; for(ui i=1; i<=m; ++i) res+=(sum[y[i]]-sum[x[i]])*(cnt[y[i]]-cnt[x[i]]); ans=min(ans, ABS(res, s)); return res; } void solve() { read(n, m, s); for(ui i=1; i<=n; ++i) read(w[i], v[i]); for(ui i=1; i<=m; --x[i++]) read(x[i], y[i]); ui l=1, r=1e6; while(l<=r) { const ui mid=l+r>>1; if(check(mid)<s) r=mid-1; else if(check(mid)>s) l=mid+1; else { putchar('0'); return ; } } write(ans); } int main() { solve(); return 0; } ``` :::: ## Mayan 游戏 这个不是困难题,可能那天早上犯困写得老慢了,而且还搁那写广搜,不知道咋想的,改深搜就过了。你想要尽量让左换右,那你直接相邻了就搜索左换右,右边空了就把左边抽出来,左边空了把右边抽出来就行了,消除的话暴力比较就行,这个题可以全开 `uint8_t` 卡常,总操作次数我算着最坏情况大概搜索上几百万次就死了,你发现他正好让你存不下这个大小,所以广搜咕咕了。 ::::success[代码] ```cpp #include <cstdio> #include <cstdint> #include <algorithm> #define LF putchar('\n') #define SP putchar(' ') #define ui uint8_t #define A z.a[x][y] #define A1 z.a[x+1][y] #define C z.c[x] #define C1 z.c[x+1] template<typename T> void read(T& x) { x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); } template<typename T> void write(T x) { if(x==0) { putchar('0'); return ; } ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48); } template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } } struct node { ui c[6], a[6][9]; void init() { for(ui i=1; i<6; ++i) { read(a[i][c[i]=1]); while(a[i][c[i]]) read(a[i][++c[i]]); --c[i]; } } bool empty() { for(ui i=1; i<6; ++i) if(c[i]) return false; return true; } } q[9]; ui n, qx[9], qy[9], type[9]; void dfs(const ui h) ; void add(node z, const ui t) { bool flag=true, b[6][8]; node a; while(flag) { flag=false; for(ui x=1; x<6; ++x) { for(ui y=1; y<=C; ++y) { if((x>2&&A==z.a[x-2][y]&&A==z.a[x-1][y])||(x>1&&x<5&&A==z.a[x+1][y]&&A==z.a[x-1][y])||(x<4&&A==z.a[x+2][y]&&A==z.a[x+1][y])||(y>2&&A==z.a[x][y-2]&&A==z.a[x][y-1])||(y>1&&y<C&&A==z.a[x][y+1]&&A==z.a[x][y-1])||(y<C-1&&A==z.a[x][y+2]&&A==z.a[x][y+1])) b[x][y]=false, flag=true; else b[x][y]=true; } } if(flag) { for(ui x=1; x<6; ++x) { a.c[x]=0; for(ui y=1; y<=C; ++y) if(b[x][y]) a.a[x][++a.c[x]]=A; } for(ui x=1; x<6; ++x) { C=a.c[x]; for(ui y=1; y<=C; ++y) A=a.a[x][y]; for(ui y=C+1; y<8; ++y) A=0; } } } for(ui x=1; x<6; q[t].c[x]=C, ++x) for(ui y=1; y<8; ++y) q[t].a[x][y]=A; } void work(const ui t, node z, ui x, ui y) { A^=A1, A1^=A, A^=A1, qx[t]=x, qy[t]=y, type[t]=1; add(z, t); dfs(t); } void work2(const ui t, node z, ui x, ui y) { z.a[x][++C]=A1; for(ui i=y; i<C1; ++i) z.a[x+1][i]=z.a[x+1][i+1]; z.a[qx[t]=x+1][C1--]=0, qy[t]=y, type[t]=0; add(z, t); dfs(t); } void work3(const ui t, node z, ui x, ui y) { z.a[x+1][++C1]=A; for(ui i=y; i<C; ++i) z.a[x][i]=z.a[x][i+1]; z.a[qx[t]=x][C--]=0, qy[t]=y, type[t]=2; add(z, t); dfs(t); } void dfs(ui h) { node z; for(ui x=1; x<6; ++x) { C=q[h].c[x]; for(ui y=1; y<=C; ++y) A=q[h].a[x][y]; for(ui y=C+1; y<8; ++y) A=0; } if(++h>n&&z.empty()) { for(ui i=1; i<=n; ++i) { write(qx[i]-1, qy[i]-1); if(type[i]) puts(" 1"); else puts(" -1"); } exit(0); } if(h>n||z.empty()) return ; for(ui x=1; x<5; ++x) { for(ui y=1; y<=C; ++y) { if(y<=C1) work(h, z, x, y); else work3(h, z, x, y); if(x>1&&y>z.c[x-1]) work2(h, z, x-1, y); } } for(ui y=z.c[4]+1; y<=z.c[5]; ++y) work2(h, z, 4, y); } int main() { read(n); q[0].init(); dfs(0); puts("-1"); return 0; } ``` :::: ## 观光公交 场上就没读这个,似乎不算很难。 场上放的是 loj 加强过的数据,考虑大致 $\Theta(n\log_2n)$ 的时间复杂度。 你考虑分段处理,分段规则如下: 设 $S_i$ 为车能够到达第 $i$ 个位置的最早时间,$maxn_i$ 为车能够从第 $i$ 个位置出发的最早时间,也就是第 $i$ 个位置上的那个来的最晚的乘客的时间。 $$S_i=a_i+\max\left(S_{i-1},~maxn_{i-1}\right)$$ 当 $S_i\le maxn_i$ 时,以 $i$ 开头另起一段。 你发现这一段有个最大性价比 $v$,即所有在这一段中下车的人个个数,相当于在这段中加一次速减少 $v$ 的代价,这一段最多可以加速 $\min\left(S_i-maxn_i\right)$ 次。你把这个搞成结构体,按性价比排序扔到大根堆里进行一个优化,每次拿最大的计算答案,就做完了。 ::::success[代码] ```cpp #include <cstdio> #include <queue> #define LF putchar('\n') #define SP putchar(' ') #define ll unsigned long long #define ui unsigned int #define N 1000005 #define M 1000005 #define K 20 #define INF 0x3f3f3f3f using namespace std; namespace FastIO { template<typename T> void read(T& x) { x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar(); } template<typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); } template<typename T> void write(T x) { if(x==0) { putchar('0'); return ; } if(x<0) { putchar('-'); x=-x; } ui a[25], t=0; while(x) a[++t]=x%10, x/=10; while(t) putchar(a[t--]|48); } template<typename T, typename... Ts> void write(T x, Ts... args) { write(x); if(sizeof...(args)!=0) { SP; write(args...); } } } using namespace FastIO; namespace M_K_W_ { ui n, m, k, a[N], x, y, z, maxn[N], s[N], en[N]; ll sum[N], sum2[N], ans, d; struct node { ui l, r; ll t, v; bool operator < (const node& z) const { return v<z.v; } }; priority_queue<node> q; void fun(const ui l, const ui r) { if(l>=r) return ; if(!a[l]) { fun(l+1, r); return ; } for(ui i=l+1; i<r; ++i) if(maxn[i]>=sum2[i]) { fun(l, i); fun(i, r); return ; } ll minn=a[l], v=en[r]; for(ui i=l+1; i<r; ++i) minn=min(minn, sum2[i]-maxn[i]), v+=en[i]; a[l]-=minn; for(ui i=l+1; i<r; ++i) sum2[i]-=minn; q.push(node { l, r, minn, v } ); } void solve() { read(n, m, k); for(ui i=1; i<n; ++i) { read(a[i]); sum[i+1]=sum[i]+a[i]; } for(ui i=1; i<=m; ++i) { read(z, x, y); maxn[x]=max(maxn[x], z), ++s[x], --s[y], d+=z, ++en[y]; } for(ui i=1; i<n; ++i) ans+=en[i+1]*(sum2[i+1]=max(sum2[i], (ll)maxn[i])+a[i]); fun(1, n); while(q.size()) { ui l=q.top().l, r=q.top().r, t=q.top().t, v=q.top().v; q.pop(); if(t>k) { ans-=v*k, k=0; break; } ans-=v*t, k-=t; fun(l, r); } write(ans-d); } } int main() { M_K_W_ :: solve(); return 0; } ``` :::: # [Codeforces Round 1098 (Div. 2)](https://codeforces.com/contest/2228) A $0$ 的个数加上 $1$ 的个数和 $2$ 的个数的最小值加上 $1$ 的个数和 $2$ 的个数的差的一半(下取整)。 [B](https://www.luogu.com.cn/article/ghj7mmhg)。 [C](https://www.luogu.com.cn/article/8uqxp0h9)。 [D](https://www.luogu.com.cn/article/7ayub5hx)。