题解:CF1372E Omkar and Last Floor

· · 题解

CF1372E Omkar and Last Floor

好题,太妙了。

题意:有一个 n 行 m 列的初始全 0 矩阵,每一行被划分为若干连续区间,现在你要在每个区间内选且仅选一个位置填上 1,定义 a_i 为第 i 列 1 的个数,最大化 \sum_{i=1}^{m}a_i^2 的值。

首先由初中知识可得 (a+b)^2 \ge a^2+b^2。

这意味着如果有一列上的 1 可以随意放置的话,最优的情况是尽可能把它们放在同一列。

考虑区间 dp。

下设 l 为区间左端点且 r 为区间右端点。

设 dp_{i,j} 表示所有满足 i \le l \le r \le j 的最大贡献和,v_{i,j,k} 表示满足 i \le l \le k \le r \le j 的区间数量。

每次转移时,我们钦定一列是闪耀的,当且仅当这一列存在某一行上该列所填的数为 1。

因此,由于上文提到的性质,左右列满足条件的区间应当尽可能向闪耀的列合并,这样才能使平方之和更大。

所以我们可以列出转移式:

dp_{i,j}=\max(dp_{i,j},dp_{i,k-1}+dp_{k+1,j}+v_{i,j,k}^2)

答案就是 dp_{1,m}。

预处理复杂度 O(n^4),转移复杂度 O(n^3)。

#include<bits/stdc++.h>
#define int long long
#define ll long long
#define ull unsigned long long
#define i128 __int128
#ifdef __linux__
//#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
#define CK_TM cerr<<'\n'<<1.0*fabs(&ENDM-&STARTM)/(1024*1024)<<"MB"<<" "<<1.0*clock()/CLOCKS_PER_SEC*1000<<"ms"<<'\n';
char buf[1<<25],*p1,*p2;
#define getchar() ((p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<25,stdin))?EOF:*p1++)
#define vector basic_string
#define pq priority_queue
#define mkpr make_pair
#define pii pair<int,int>
#define pb push_back
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define sz(h) (int)h.size()
#define popc(x) __builtin_popcount(x)
bool STARTM;
constexpr double eps=1e-10;
constexpr int i_inf=1e9;
constexpr ll ll_inf=1e18;
namespace AyanamiRei{
    inline int read(){
        int x=0,f=1;char c=getchar();
        while(c<'0'||c>'9') {if(c=='-') f^=1;c=getchar();}
        while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
        return f?x:~x+1;
    }
}namespace SoryuAsukaRangure{
    inline void write(int x){
        if(x<0) x=~x+1,putchar('-');
        if(x>9) write(x/10);
        return putchar(x%10^48),void();
    }
}namespace AyanamiYue{
    inline void spaceput(int x){
        SoryuAsukaRangure::write(x);putchar(' ');
    }
}namespace IkariYue{
    inline void output(int x){
        SoryuAsukaRangure::write(x);putchar('\n');
    }
}namespace IkariShinji{
    inline int max(int x,int y){
        return x>y?x:y;
    }
}namespace NagisaKaworu{
    inline int min(int x,int y){
        return x<y?x:y;
    }
}namespace MariMakinamiIllustious{
    inline double MAX(double x,double y){
        return x-y>eps?x:y;
    }
}namespace ShikinamiAsukaRangure{
    inline double MIN(double x,double y){
        return y-x>eps?x:y;
    }
}
using namespace std;
using namespace AyanamiRei;
using namespace SoryuAsukaRangure;
using namespace AyanamiYue;
using namespace IkariYue;
using namespace IkariShinji;
using namespace NagisaKaworu;
using namespace MariMakinamiIllustious;
using namespace ShikinamiAsukaRangure;
constexpr int N=1e2+5; 
constexpr int the_birthday_of_Asuka=20011204;
constexpr int the_birthday_of_Ayanami=20010330;
mt19937 Asuka(the_birthday_of_Asuka+time(0));
mt19937 Ayanami(the_birthday_of_Ayanami+time(0));
int n,m;
int dp[N][N],v[N][N][N];
#define S(x) (x*x)
bool ENDM;
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        int k;k=read();
        while(k--){
            int L,R;L=read(),R=read();
            for(int l=1;l<=L;l++){
                for(int r=R;r<=m;r++){
                    for(int p=L;p<=R;p++) v[l][r][p]++;
                }
            }
        }
    }
    for(int i=1;i<=m;i++) dp[i][i]=S(v[i][i][i]);
    for(int len=2;len<=m;len++){
        for(int i=1;i<=m;i++){
            int j=i+len-1;
            if(j>m) break;
            for(int k=i;k<=j;k++) dp[i][j]=max(dp[i][j],dp[i][k-1]+dp[k+1][j]+S(v[i][j][k]));
        }
    }
    write(dp[1][m]);
    CK_TM;
    return 0;
}