CF2063D Game With Triangles 题解
小清新三分,有几个细节赛时没有调完,遗憾离场,破防了。
注意到如果我们可以求 \(f(k)\),那么 \(k_{\text{max}}\) 只需要一直枚举 \(k\) 直到 \(f(k)\) 求不出来为止,因此我们直接考虑求 \(f(k)\)。
我们考虑枚举在 \(y=0\) 选择三角形的底边数量 \(x\),注意到答案关于 \(x\) 是单峰的。因为如果 \(x\) 不够多,可能 \(y=0\) 上一些距离较远的点无法被利用,如果 \(x\) 太多了,可能 \(y=2\) 上一些距离较远的点无法被利用。如果赛时不敢确定可以打个表看看。
考虑如何快速计算。我们可以预处理出来 \(y=0\) 和 \(y=2\) 上距离前 \(i\) 大的点两两匹配后得到的贡献,因为我们可以贪心地取这些点对使答案最大。然后取三角形需要另一条线段也贡献一个点,我们判断一下 \(y=0\) 和 \(y=2\) 两条线段上用的总点数,如果有一个超了,直接返回负无穷表示不成立。
现在唯一的问题是答案左右两边有一些不成立的位置,如果全部返回负无穷,我们无法确定该舍去哪一段。也就是说,这个函数并不是严格单峰。因此,我们在负无穷上面加上一些东西,使得答案变成严格单峰。具体的,我们可以通过使其不成立的条件判断出来是左边的不成立还是右边的不成立,如果是左边的不成立,那么返回负无穷加上 \(x\),否则返回负无穷加上 \(k-x\)。
还有一些坑点,重复地元素需要特判,以及三分遇到相等的点应该舍左边而不是舍右边。
#include <bits/stdc++.h>
using namespace std;
long long t,n,m,a[300000],b[300000],pa[300000],pb[300000],ou[300000];
long long check(long long x,long long k)
{
if((x*2+k-x)>n||pa[x]==-1e18)return -(long long)1e18+k-x;
if((x+(k-x)*2)>m||pb[k-x]==-1e18)return -(long long)1e18+x;
return pa[x]+pb[k-x];
}
int main()
{
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
for(int i=1;i<=m;i++)scanf("%lld",&b[i]);
sort(a+1,a+n+1),sort(b+1,b+m+1);
for(int i=1;i<=max(m,n);i++)pa[i]=pb[i]=-1e18;
for(int i=1;i<n-i+1;i++)
if(a[n-i+1]!=a[i])pa[i]=pa[i-1]+a[n-i+1]-a[i];
else break;
for(int i=1;i<m-i+1;i++)
if(b[m-i+1]!=b[i])pb[i]=pb[i-1]+b[m-i+1]-b[i];
else break;
long long flag=0;
for(int i=1;i<=max(m,n);i++)
{
long long l=0,r=i,ans=-1e18;
while(l<=r)
{
long long lmid=(l*2+r)/3,rmid=(l+r*2)/3;
if(check(lmid,i)>check(rmid,i))ans=max(ans,check(lmid,i)),r=rmid-1;
else ans=max(ans,check(rmid,i)),l=lmid+1;
}
ou[i]=ans;
if(ou[i]<0)
{
printf("%d\n",i-1),flag=i-1;
for(int j=1;j<=i-1;j++)printf("%lld ",ou[j]);
break;
}
}
if(flag)printf("\n");
}
return 0;
}
CF2063D Game With Triangles 题解的更多相关文章
- codechef Chef and The Right Triangles 题解
Chef and The Right Triangles The Chef is given a list of N triangles. Each triangle is identfied by ...
- [USACO20FEB]Equilateral Triangles P 题解
优雅的暴力. 设三个点为 \((i,j,k)\),则有 \(6\) 个未知数即 \(x_i,x_j,x_k,y_i,y_j,y_k\).又因为有 \(2\) 条关于这 \(6\) 个未知数的方程 \( ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces Round #309 (Div. 1) C. Love Triangles dfs
C. Love Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/553/pro ...
- Codeforces Round #308 (Div. 2) D. Vanya and Triangles 水题
D. Vanya and Triangles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- Project Euler 91:Right triangles with integer coordinates 格点直角三角形
Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...
- CodeForces 682E Alyona and Triangles (计算几何)
Alyona and Triangles 题目连接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/J Description You ar ...
- Little Zu Chongzhi's Triangles
Little Zu Chongzhi's Triangles Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 ...
- POJ1569 Myacm Triangles
Description There has been considerable archeological work on the ancient Myacm culture. Many artifa ...
- hdu 5784 How Many Triangles 计算几何,平面有多少个锐角三角形
How Many Triangles 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5784 Description Alice has n poin ...
随机推荐
- Nginx+Windows搭建域名访问环境, 由nginx --> 网关 ---> 服务
1).修改windows hosts文件改变本地域名映射,将gulimall.com映射到虚拟机ip 2).修改nginx的根配置文件nginx.conf,将upstream映射到我们的网关服务 up ...
- Linux Nginx tomcat集群--打程序补丁步骤
Linux Nginx Tomcat集群--打程序补丁步骤 一.若不知道服务器中nginx所在目录(知道目录位置,从第3条开始看) 1.查找nginx进程(默认80端口) netstat -lntup ...
- SpringBoot内容协商(Content Negotiation)
内容协商 在 HTTP 协议中,内容协商是一种机制,用于为同一 URI 提供资源不同的表示形式,以帮助用户代理指定最适合用户的表示形式(例如,哪种文档语言.哪种图片格式或者哪种内容编码).[^1] S ...
- JS函数调用的5种模式
函数是经常会用的工具, 这里总结一下关于在 js 中的 5种函数调用的模式如下: <!DOCTYPE html> <html lang="en"> < ...
- JavaScript最佳实践:从基础到高级
@charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...
- TVM:PACKFUNC机制
转载:https://www.cnblogs.com/wanger-sjtu/p/15063948.html 为实现多种语言支持,需要满足以下几点: 部署:编译结果可以从python/javascri ...
- String Manipulation related with pandas
String Manipulation related with pandas String object Methods import pandas as pd import numpy as np ...
- K-th Symbol in Grammar——LeetCode进阶路
原题链接https://leetcode.com/problems/k-th-symbol-in-grammar/ 题目描述 On the first row, we write a 0. Now i ...
- 解决Git异常 Access denied your account has 2FA enabled
摘要:解决Git双因子身份验证问题. 问题背景 在使用账号和密码的方式拉取公司GitLab代码时,遇到了以下错误提示问题: remote: HTTP Basic: Access denied. T ...
- DevOps 需要处理的工作
本文纯属个人工作记录: 1.部署Linux服务器 2.安装Docker 3.在Docker中安装Gitlab和runner 4.设置Gitlab pipline,即CI/CD 5.可能需要Jenkin ...