跳到正文

题解

题解:P14966 Staring at Stars

Trivial_std约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月17日
收录
2026年11月5日
更新
2026年11月5日
篇幅
约 3 分钟
获赞
9
题目
P14966

版本

  1. v1当前

题目

验题人题解。

思路:

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

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

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

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

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

假设存在 x>1x>1 满足 sx>s1×xs_x> s_1\times x,那 sxs_x 包含的 x+1x+1 个点中一定存在一个点 pp 满足 dp+dp+1>s1d_p+d_{p+1}>s_1,与定义不符,假设不成立。

代码:

C++
#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;
}
原文