暑假集训笔记Day2

· · 算法·理论

scanf是个好东西。

题目请见https://cppoj.kids123code.com/contest/289。

A

普通的区间 DP,唯一要注意的是在两个区间都不能合并的时候,它们的值都是 0,这时候会被判定成一样而可以合并,但实际上不行。所以要特判 0 的情况或者提前赋一个极小值,这里选用的是前者。

(帖子最有用的一集)

#include<bits/stdc++.h>
#define For(i,a,b) for(int i = a;i<=b;i++)
using namespace std;
inline __int128 read();
int a[260];
int f[300][300];
int main(){
    int n;
    scanf("%d",&n);
    For(i,1,n){
        scanf("%d",&a[i]);
    }
    For(i,1,n){
        For(j,1,n){
            if(i==j) f[i][j] = a[i];
            //else 
        }
    }
    For(len,2,n){
        for(int l = 1;l+len-1<=n;l++){
            int r = l+len-1;
            For(k,l,r-1){
                if(f[l][k] == f[k+1][r]&&f[l][k]){
                    f[l][r] = max(f[l][r],f[l][k]+1);
                }
            }
        }
    }
    int mx = -1;
    For(i,1,n){
        For(j,1,n){
            mx = max(mx,f[i][j]);
        }
    }
    printf("%d",mx);
    return 0;
}

单调栈

单调栈即满足单调性的栈结构。这里实现借助 STL 中的栈。

模版:

For/Rof(i,1,n){//n为数组中元素个数
        if(st.empty()){//如果栈为空
            f[i] = 0;//无解
        }
        else{
            while(!st.empty()&&a[st.top()]<=a[i]) st.pop();//当不合法的时候弹出
            if(st.empty()) f[i] = 0;
            else f[i] = st.top();//记录答案

        }
        st.push(i);//放入新元素
    }

while 循环中的第二个条件可根据题目灵活改变,这里找的是第一个比该元素大的,所以小的不合法。

在第一行中,顺着找可以找到左边第一个比该元素大/小的值,而逆着找可以找到右边。

B

模版。

C

由于每一次添加的值都位于最后一个的位置上,所以这时它本身就是最大后缀值,那么每一次我们都将答案和下标进行异或,即 ans \gets ans \oplus i(i 为该元素下标)。然后我们再把这次不是最大后缀值的去除掉,方法就是找到前面所有比它小的,再将它们的下标与答案异或一遍。如果找完了栈空了,那么就再把这个下标放进去。

#include<bits/stdc++.h>
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
#define ull unsigned long long
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
int n;
ull a[1000005],ans;
stack<int> st;
int main(){
    n = read();
    For(i,1,n) a[i] = read();
    For(i,1,n){
        while(!st.empty()&&a[st.top()]<=a[i]){
            ans^=st.top();
            st.pop();
        }
        if(st.empty()) st.push(i);
        else if(a[st.top()]>a[i]){
            st.push(i);
        }
        ans^=i;
        write(ans);
        putchar('\n');
    }
    return 0;
}

D

模版写两遍,注意直接把自己发射的能量值赋值给接收到的发射站,注意清空。

#include<bits/stdc++.h>
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
//#define __int128 long long
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
const int N = 1000005;
int h[N],v[N],f[N],right_f[N];
stack<int> st;
int main(){
    int n = read();
    For(i,1,n){
        h[i] = read();
        v[i] = read();
    }
    For(i,1,n){
        while(!st.empty()&&h[st.top()]<=h[i]) st.pop();
        if(!st.empty()){
            f[st.top()]+=v[i];
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();
    Rof(i,n,1){
        while(!st.empty()&&h[st.top()]<=h[i]) st.pop();
        if(!st.empty()){
            f[st.top()]+=v[i];
        }
        st.push(i);
    }
    int mx = -1;
    For(i,1,n){
        mx = max(f[i],mx);
    }
    write(mx);
    return 0;
}

E

有点难/dk,不过还是难不倒我(嘻嘻)。

以空间换时间,我们首先定义一个数组 f,f_{i,j,k} 表示第 i 列,第 j 行到第 k 行的最小值。实现方法是将本列上一行的最小值和该元素值取最小值,即 f_{i,j,k} = \min(f_{i,j,k-1},a_{k,i})(a 数组即为输入)。对于上下界相等(即 i = j)的情况,易得最小值为该元素值。

再计算二位前缀和用于获取子矩阵的元素和。

枚举上下界(记为 x_1,x_2),枚举这个区间中的每一列,对于它,为了使它的最小值成为这个区间中的最小值,需要找到左边和右边第一个比它小的最小值所在的列(记为 y_1,y_2),具体实现可以使用单调栈。值得一提的是,如果找不到,为了方便后面的加一和减一,左界可以设为 0,右界可以设为 m+1。

好的,现在我们已经找到了这个区间,它就是从 (x_1,y_1+1) 到 (x_2,y_2-1) 的这个区间。现在计算它的元素和和最小值(即为之前枚举的列的最小值)的乘积,再在这个上下界的所有乘积中取最大值。

再在所有枚举的上下界中取最大值。

最后,记得开 long long。

#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
//#define __int128 long long
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
int f[305][305][305],a[305][305],s[305][305],n,m;
ll ans;
ll solve(int tp,int bot){
    int b[305] = {0},x[305] = {0},y[305] = {0};
    For(i,1,m){
        b[i] = f[i][tp][bot];
    }
    stack<int> st;
    For(i,1,m){
        if(st.empty()){
            x[i] = 0;
        }
        else{
            while(!st.empty()&&b[st.top()]>=b[i]) st.pop();
            if(st.empty()) x[i] = 0;
            else x[i] = st.top();
        }
        st.push(i);
    }
    while(!st.empty()) st.pop();
    Rof(i,m,1){
        if(st.empty()){
            y[i] = m+1;
        }
        else{
            while(!st.empty()&&b[st.top()]>=b[i]) st.pop();
            if(st.empty()) y[i] = m+1;
            else y[i] = st.top();
        }
        st.push(i);
    }
    ll res = 0;
    For(i,1,m){
        int zuo = x[i]+1;
        int you = y[i]-1;
        ll val = s[bot][you]-s[tp-1][you]-s[bot][zuo-1]+s[tp-1][zuo-1];
        res = max(res,val*b[i]);
    }
    return res;
}
int main(){
    n = read();
    m = read();
    For(i,1,n){
        For(j,1,m){
            a[i][j] = read();
            s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];//前缀和 
        }
    }
    For(i,1,m){
        For(j,1,n){
            f[i][j][j] = a[j][i];//放到一起好习惯 
            For(k,j+1,n){
                f[i][j][k] = min(f[i][j][k-1],a[k][i]);//推导得出第i列第j-k行的最小值 
            }
        }
    }
    For(i,1,n){
        For(j,i,n){
            ans = max(ans,solve(i,j));
        }
    }
    write(ans);
    return 0;
}

F

贪心。记下第一天吃的药的数量,一次一次减去,如果剩余药量 \le k,那么上一次减去的时间 +1 天也就可以满足条件。如果一开始的药量就 \le k,输出 1。

#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
#define __int128 long long
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
ll sum;
struct node{
    int a,b;
}a[300005];
bool cmp(node x,node y){
    return x.a<y.a;
}
int main(){
    int n = read(),k = read();
    For(i,1,n){
        a[i].a = read();
        a[i].b = read();
        sum+=a[i].b;
    }
    sort(a+1,a+n+1,cmp);
    if(sum<=k){
        write(1);
        return 0;
    }
    For(i,1,n){
        sum-=a[i].b;
        if(sum<=k){
            write(a[i].a+1);
            return 0;
        }
    }
    return 0;
}

G

枚举普通罐头的个数,接着便可得知开罐器的个数,剩下的就是拉环罐头的个数。

对于三者,都由大到小排序。再对开罐器做前缀和,用二分找到需要的开罐器的数量(注意可以等于 0)。

如果开罐器不够了或者是普通罐头/开罐器过多,都说明该解不合法,同时也说明后面的解更不合法,直接跳出循环。

如果拉环罐头的个数不够预期,那么都选上还是可以获取最大的欢乐值。

#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i = a;i<=b;i++)
#define Rof(i,a,b) for(int i = a;i>=b;i--)
//#define __int128 long long
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
const int N = 2e5+5; 
int a[N],b[N],c[N],len1,len2,len3;
ll s[N],s_lahuan[N],s_putong[N],ans = 0;
int main(){
    int n = read(),m = read();
    For(i,1,n){
        int t = read(),x = read();
        if(t==0) a[++len1] = x;
        else if(t==1) b[++len2] = x;
        else if(t==2) c[++len3] = x;
    }
    sort(a+1,a+len1+1,greater<int>());
    sort(b+1,b+len2+1,greater<int>());
    sort(c+1,c+len3+1,greater<int>());
    For(i,1,len3){
        s[i] = s[i-1] + c[i];
    }
    For(i,1,len1){
        s_lahuan[i] = s_lahuan[i-1] + a[i];
    }
    For(i,1,len2){
        s_putong[i] = s_putong[i-1] + b[i];
    }
    For(i,0,len2){
        int t = lower_bound(s,s+len3+1,i)-s;
        if(t>len3) break;//开罐器不够了 
        int la_huan = m-t-i;
        if(la_huan<0) break;
        ll res = s_lahuan[min(la_huan,len1)]+s_putong[i];
        ans = max(ans,res);
    }
    write(ans);
    return 0;
}

H

签到题(怎么被放在最后了)。代码不放了。

I

顺序搜索,类似八皇后问题。将每一个 3 \times 3 的矩阵里都标上序号。再记录一下再本行、本列、本矩阵中该数字有没有出现。随后爆搜即可,注意回溯。

#include<bits/stdc++.h>
#define __int128 int
using namespace std;
inline __int128 read(){
    __int128 f = 1,x = 0;
    char c = getchar();
    while(c<'0'||c>'9'){
        if(c=='-') f = -1;
        c = getchar();
    }
    while(c>='0'&&c<='9'){
        x = x*10+c-'0';
        c = getchar();
    }
    return f*x;
}
inline void write(__int128 x){
    if(x<0){
        x = -x;
        putchar('-');
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
}
int a[11][11];
bool tmp[10][10]; //第i个区间内j是否出现过 
bool tmp_2[10][10];
bool tmp_3[10][10];
int f[10][10] = {{},
                    {0,1,1,1,2,2,2,3,3,3},
                    {0,1,1,1,2,2,2,3,3,3},
                    {0,1,1,1,2,2,2,3,3,3},
                    {0,4,4,4,5,5,5,6,6,6},
                    {0,4,4,4,5,5,5,6,6,6},
                    {0,4,4,4,5,5,5,6,6,6},
                    {0,7,7,7,8,8,8,9,9,9},
                    {0,7,7,7,8,8,8,9,9,9},
                    {0,7,7,7,8,8,8,9,9,9}};
void dfs(int x,int y){
    if(y>9){//下一行
        y = 1;
        x++;
    }
    if(x>9){//到底了输出
        for(int i = 1;i<=9;i++){
            for(int j = 1;j<=9;j++){
                write(a[i][j]);
                putchar(' ');
            }
            putchar('\n');
        }
        exit(0);
    }
    if(!a[x][y]){
        for(int i = 1;i<=9;i++){
            if(!tmp[f[x][y]][i]&&!tmp_2[x][i]&&!tmp_3[y][i]){
                a[x][y] = i;
                tmp[f[x][y]][i] = 1;
                tmp_2[x][i] = 1;
                tmp_3[y][i] = 1;
                dfs(x,y+1);
                a[x][y] = 0;
                tmp[f[x][y]][i] = 0;
                tmp_2[x][i] = 0;
                tmp_3[y][i] = 0;
            }
        }
    }
    else{
        dfs(x,y+1);
    }
}
int main(){
    for(int i = 1;i<=9;i++){
        for(int j = 1;j<=9;j++){
            a[i][j] = read();
            if(a[i][j]!=0){
                tmp_2[i][a[i][j]] = 1;
                tmp_3[j][a[i][j]] = 1;
                tmp[f[i][j]][a[i][j]] = 1;
            }
        }
    }
    dfs(1,1);
    return 0;
}