题解:P14966 Staring at Stars

· · 题解

题目

验题人题解。

思路:

题目可以转化为:给定 n 个点,每个点有一个权值 d_i,选择一个高度为 t+1,宽度为 1 的矩形,答案为矩形内点的权值和乘 \lfloor \frac{k}{t} \rfloor。

先说结论:选择 t=1 是不劣的。

考虑证明,设选择 t=i 时所有选择方案中点的权值和的最大值为 s_i,那么答案 ans=s_1\times k,若选择 t>1,则此时的 ans'=s_t\times \lfloor \frac{k}{t} \rfloor 。

由于 \frac{s_t}{t}\le s_1(由于 d_i\geq 0 ,可以用反证法证明),所以 s_1\times k\le s_t\times \frac{k}{t},即选择 t=1 时答案最大。

考虑证明 \frac{s_t}{t}\le s_1:

假设存在 x>1 满足 s_x> s_1\times x,那 s_x 包含的 x+1 个点中一定存在一个点 p 满足 d_p+d_{p+1}>s_1,与定义不符,假设不成立。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int N=1e5+10;
const int P=1e9+7,Inf=1e9;

int n,k,ma;
pii p[N];
map<pii,int> mp;//键值为点的下标,值为权值

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        mp.clear();
        ma=0;
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            int x,y,s;
            cin>>x>>y>>s;
            p[i]={x,y};
            mp[p[i]]=s;
        }
        for(int i=1;i<=n;i++){
            int x=p[i].fi;
            int y=p[i].se;
            ma=max(ma,mp[{x,y}]+mp[{x,y+1}]);
        }
        cout<<ma*k<<'\n';
    }
    return 0;
}