[AtCoder2558]Many Moves
Problem
共有n个格子,有两个硬币在a,b格子上,还有q个操作。
每个操作给你一个编号,要求将一个硬币移到这个编号上。
问你硬币移动的总距离最小值。
Solution
O(n^3):DP[i][a][b]表示i个操作后,1个硬币在a,一个硬币在b的总距离最小值。
O(n^2):DP[i][b]表示i个操作后,一个硬币在x[i],一个硬币在b的总距离最小值。
那么,DP[i][b] = DP[i - 1][b] + abs(a[i] - a[i - 1])
特别地,DP[i][x[i - 1]] = DP[i - 1][j] + abs(a[i] - j)
O(nlogn):用两棵线段树来维护RMQ,一棵维护其DP值减去x[i - 1],一颗维护其DP值加上x[i - 1]。
对于第一个方程,相当于整棵线段树加上abs(a[i] - a[i - 1])
对于第二个方程,j<a[i]时,即为DP[i - 1][j] - j + a[i], j > a[i]时,即为DP[i - 1][j] + j - a[i];
Notice
别忘了要开longlong
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const ll INF = 1e15;
const int N = 800000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int x[N / 4 + 5];
struct node
{
ll val[2][N + 5];
int left[N + 5], right[N + 5];
ll tag[N + 5];
inline void down(int u)
{
val[0][u + u] += tag[u], val[0][u + u + 1] += tag[u];
val[1][u + u] += tag[u], val[1][u + u + 1] += tag[u];
tag[u + u] += tag[u], tag[u + u + 1] += tag[u];
tag[u] = 0;
}
inline void up(int u)
{
val[0][u] = min(val[0][u + u] , val[0][u + u + 1]);
val[1][u] = min(val[1][u + u] , val[1][u + u + 1]);
}
void build(int u, int l, int r)
{
left[u] = l, right[u] = r, val[0][u] = val[1][u] = INF, tag[u] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(u + u, l, mid);
build(u + u + 1, mid + 1, r);
}
void modify(int u, int pos, ll v)
{
int l = left[u], r = right[u], mid = (l + r) >> 1;
if (l == r)
{
val[0][u] = min(val[0][u], v - pos), val[1][u] = min(val[1][u], v + pos);
return;
}
down(u);
if (pos <= mid) modify(u + u, pos, v);
else modify(u + u + 1, pos, v);
up(u);
}
ll query(int t, int u, int x, int y)
{
int l = left[u], r = right[u], mid = (l + r) >> 1; ll T = INF;
if (x <= l && y >= r) return val[t][u];
down(u);
if (x <= mid) T = min(T, query(t, u + u, x, y));
if (y > mid) T = min(T, query(t, u + u + 1, x, y));
return T;
}
ll find(int u)
{
int l = left[u], r = right[u], mid = (l + r) >> 1;
if (l == r) return min(val[0][u] + l, val[1][u] - l);
down(u);
return min(find(u + u), find(u + u + 1));
}
}Segment_tree;
int sqz()
{
int n = read(), q = read(), pos = read(); x[0] = read();
Segment_tree.build(1, 1, n);
Segment_tree.modify(1, pos, 0);
rep(i, 1, q)
{
x[i] = read();
ll now = min(Segment_tree.query(0, 1, 1, x[i]) + x[i], Segment_tree.query(1, 1, x[i], n) - x[i]);
Segment_tree.tag[1] += abs(x[i] - x[i - 1]), Segment_tree.val[0][1] += abs(x[i] - x[i - 1]), Segment_tree.val[1][1] += abs(x[i] - x[i - 1]);
Segment_tree.modify(1, x[i - 1], now);
}
printf("%lld\n", Segment_tree.find(1));
}
[AtCoder2558]Many Moves的更多相关文章
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- LeetCode Minimum Moves to Equal Array Elements II
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ 题目: Given a non-empt ...
- LeetCode Minimum Moves to Equal Array Elements
原题链接在这里:https://leetcode.com/problems/minimum-moves-to-equal-array-elements/ 题目: Given a non-empty i ...
- LeetCode 453 Minimum Moves to Equal Array Elements
Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- Vim 8.0
安装Vim 8.0yum install ncurses-devel wget https://github.com/vim/vim/archive/master.zip unzip master.z ...
- Codeforces 1006 F - Xor-Paths
F - Xor-Path 思路: 双向搜索dfs 如果普通的搜索复杂度是n 那么双向搜索复杂度是√n 代码: #include<bits/stdc++.h> using namespace ...
- English Voice of <<See You Again >>
<See You Again >(<当我们再相见>) 演唱:Wiz Khalifa/Charlie Puth 维兹·卡利法/查理·普斯 It's been a long da ...
- Linq to XML 增删改查
Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作.C#访问XML文件的常用类:XmlDocument,XmlEle ...
- stark 组件 url 二级分发的实现
模拟 admin 组件url设计思路 项目urls 文件中: from django.contrib import admin from django.urls import path from st ...
- Weighted Channel Dropout for Regularization of Deep Convolutional Neural Network
这是AAAI2019的一篇论文,主要是为了解决小数据集的过拟合问题,使用了针对于卷积层的Dropout的方法. 论文的要点记录于下: 1.在训练过程中对于卷积层的channels进行droipout, ...
- Linux中安装Mysql授权远程访问
一.直接授权 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OP ...
- 『计算机视觉』Mask-RCNN
一.Mask-RCNN流程 Mask R-CNN是一个实例分割(Instance segmentation)算法,通过增加不同的分支,可以完成目标分类.目标检测.语义分割.实例分割.人体姿势识别等多种 ...
- React文档(五)组件和props
组件可以让你将UI分割成独立的,可复用的模块,然后考虑将每个模块彼此隔离.从概念上理解,组件就像js中的函数.他们接受随意的输入(被称为props)然后返回React元素来描述屏幕上应该出现什么. 函 ...
- 数组的typedef 和函数的typedef
#include<stdio.h> #include<string.h> #include<stdlib.h> // 数组指针 语法 梳理 // //int a[1 ...