当前位置:网站首页>P6117-[JOI 2019 Final]コイン集め【贪心】
P6117-[JOI 2019 Final]コイン集め【贪心】
2022-06-24 07:49:00 【QuantAsk】
正题
题目链接:https://www.luogu.com.cn/problem/P6117
题目大意
平面上有 2 n 2n 2n的硬币,要给每个硬币匹配一个 x ∈ [ 1 , n ] , y ∈ [ 1 , 2 ] x\in[1,n],y\in[1,2] x∈[1,n],y∈[1,2]的位置(不能重复)。
使得所有硬币和它们匹配位置的曼哈顿距离之和最小。
1 ≤ n ≤ 1 0 5 , − 1 0 9 ≤ X i , Y i ≤ 1 0 9 1\leq n\leq 10^5,-10^9\leq X_i,Y_i\leq 10^9 1≤n≤105,−109≤Xi,Yi≤109
解题思路
先把每个硬币先移进 x ∈ [ 1 , n ] , y ∈ [ 1 , 2 ] x\in[1,n],y\in[1,2] x∈[1,n],y∈[1,2]这个范围内,然后考虑贪心去把每个硬币匹配。
我们在同一个 x x x的硬币如果上下直接能够补充缺口那么肯定优先上下补充。
不然就从左到右考虑,那么最左边的肯定往右移动多余/请求空缺,记 f i , j f_{i,j} fi,j表示位置 ( i , j ) (i,j) (i,j)现在的需求情况即可。
时间复杂度: O ( n ) O(n) O(n)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=1e5+10;
ll n,g[N][2],ans;
signed main()
{
scanf("%lld",&n);
for(ll i=1,x,y;i<=2*n;i++){
scanf("%lld%lld",&x,&y);
if(y>=2)ans+=y-2,y=2;
else ans+=1-y,y=1;
if(x>n)ans+=x-n,x=n;
else if(x<1)ans+=1-x,x=1;
g[x][y-1]++;
}
for(ll i=1;i<=n;i++){
g[i][0]--;g[i][1]--;
if(g[i][0]*g[i][1]<0){
if(g[i][0]<0){
ll p=min(-g[i][0],g[i][1]);
g[i][0]+=p;g[i][1]-=p;
ans+=p;
}
else{
ll p=min(g[i][0],-g[i][1]);
g[i][0]-=p;g[i][1]+=p;
ans+=p;
}
}
ans+=abs(g[i][0])+abs(g[i][1]);
g[i+1][0]+=g[i][0];
g[i+1][1]+=g[i][1];
}
printf("%lld\n",ans);
return 0;
}
边栏推荐
- 【ES6闯关】Promise堪比原生的自定义封装(万字)
- threejs辉光通道01(UnrealBloomPass && layers)
- 4274. suffix expression
- Groovy通过withCredentials获取Jenkins凭据
- Essay - Reflection
- 216. combined summation III enumeration method
- RISC-V架构下 FPU Context 的动态保存和恢复
- KaFormer个人笔记整理
- 学习太极创客 — ESP8226 (十二)ESP8266 多任务处理
- MySQL data (Linux Environment) scheduled backup
猜你喜欢

【牛客】把字符串转换成整数

Data middle office: detailed explanation of technical architecture of data middle office

Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing

数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题

JS to find and update the specified value in the object through the key

12、 Demonstration of all function realization effects

十二、所有功能实现效果演示

Opencv maximum filtering (not limited to images)
![[redis implements seckill business ①] seckill process overview | basic business implementation](/img/a3/9a50e83ece43904a3a622dcdb05b3c.png)
[redis implements seckill business ①] seckill process overview | basic business implementation

Spark - LeftOuterJoin 结果条数与左表条数不一致
随机推荐
Determination of monocular and binocular 3D coordinates
tcpdump抓包实现过程
I heard that you are still spending money to buy ppt templates from the Internet?
Data middle office: a collection of middle office construction architectures of large domestic factories
Mba-day25 best value problem - application problem
MySQL | store notes of Master Kong MySQL from introduction to advanced
[pytorch basic tutorial 30] code analysis of DSSM twin tower model
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
1704. judge whether the two halves of a string are similar
KaFormer个人笔记整理
用VNC Viewer的方式远程连接无需显示屏的树莓派
Pytorch读入据集(典型数据集及自定义数据集两种模式)
Data middle office: overview of data governance
MySQL | view notes on Master Kong MySQL from introduction to advanced
【LeetCode】387. 字符串中的第一个唯一字符
cookie加密 4 rpc方法确定cookie加密
[Niuke] length of the last word of HJ1 string
Squid proxy application
Applet cloud data, data request a method to collect data
【gdb调试工具】| 如何在多线程、多进程以及正在运行的程序下调试