bzoj 3704 昊昊的机油之GRST - 贪心
首先求$b - a$,再差分,令这个数组为$c$。
那么操作的次数是这个数组$c$中正数的和。
现在可以做的是选择一个地方+4,它后面的某个地方-4。
考虑什么时候可以使得当前的答案减少。
- 在$-3$处$+4$,在$3$处$-4$,答案减少2
- 在$-3$处$+4$,在$2$处$-4$,答案减少1
- 在$-2$处$+4$,在$3$处$-4$,答案减少1
首先我们将尽量靠后的$-3$和$3$匹配,然后将对它操作。
这个用一个栈就能做。
剩下就贪心一下就好了。注意当$-3$和$2$操作时会多出$-2$。
Code
/**
* bzoj
* Problem#3704
* Accepted
* Time: 1400ms
* Memory: 128316k
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;
typedef bool boolean; typedef class Input {
protected:
const static int limit = ;
FILE* file; int ss, st;
char buf[limit];
public: Input():file(NULL) { };
Input(FILE* file):file(file) { } void open(FILE *file) {
this->file = file;
} void open(const char* filename) {
file = fopen(filename, "r");
} char pick() {
if (ss == st)
st = fread(buf, , limit, file), ss = ;//, cerr << "str: " << buf << "ed " << st << endl;
return buf[ss++];
}
}Input; #define digit(_x) ((_x) >= '0' && (_x) <= '9') Input& operator >> (Input& in, unsigned& u) {
char x;
while (~(x = in.pick()) && !digit(x));
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
return in;
} Input& operator >> (Input& in, unsigned long long& u) {
char x;
while (~(x = in.pick()) && !digit(x));
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
return in;
} Input& operator >> (Input& in, int& u) {
char x;
while (~(x = in.pick()) && !digit(x) && x != '-');
int aflag = ((x == '-') ? (x = in.pick(), -) : ());
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
u *= aflag;
return in;
} Input& operator >> (Input& in, long long& u) {
char x;
while (~(x = in.pick()) && !digit(x) && x != '-');
int aflag = ((x == '-') ? (x = in.pick(), -) : ());
for (u = x - ''; ~(x = in.pick()) && digit(x); u = u * + x - '');
u *= aflag;
return in;
} Input& operator >> (Input& in, char* str) {
for (char x; ~(x = in.pick()) && x != '\n' && x != ' '; *(str++) = x);
return in;
} Input in (stdin); template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} int n;
int *a, *b, *c;
boolean *used; inline void init() {
in >> n;
a = new int[(n + )];
b = new int[(n + )];
c = new int[(n + )];
for (int i = ; i <= n; i++) {
in >> a[i];
}
for (int i = ; i <= n; i++) {
in >> b[i];
c[i] = (b[i] - a[i] + ) % ;
}
} stack<int> sta;
inline void solve() {
int ans = ;
for (int i = n; i > ; i--)
c[i] = c[i] - c[i - ];
used = new boolean[(n + )];
pfill(used, used + n + , false);
for (int i = ; i <= n; i++)
if (c[i] > )
ans += c[i];
for (int i = , y; i <= n; i++)
if (c[i] == -)
sta.push(i);
else if (c[i] == && !sta.empty()) {
y = sta.top();
sta.pop();
used[y] = used[i] = true;
ans -= ;
} int have_2 = , have_3 = ;
for (int i = ; i <= n; i++) {
if (used[i])
continue;
switch (c[i]) {
case :
if (have_3)
have_3--, ans--;
else if (have_2)
have_2--, ans--;
break;
case :
if (have_3)
have_3--, have_2++, ans--;
break;
case -:
have_2++;
break;
case -:
have_3++;
break;
}
}
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}
bzoj 3704 昊昊的机油之GRST - 贪心的更多相关文章
- bzoj 3704 昊昊的机油之GRST 贪心dp,思维
昊昊的机油之GRST Time Limit: 10 Sec Memory Limit: 1024 MBSubmit: 80 Solved: 33[Submit][Status][Discuss] ...
- UESTC 1256 昊昊爱运动 Map
昊昊爱运动 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他N天 ...
- CDOJ 1259 昊昊爱运动 II 线段树+bitset
昊昊爱运动 II 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r ...
- cdoj 1256 昊昊爱运动 预处理/前缀和
昊昊爱运动 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1256 Descr ...
- CDOJ 1259 昊昊爱运动 II bitset+线段树
题目链接 昊昊喜欢运动 他N天内会参加M种运动(每种运动用一个[1,m]的整数表示) 现在有Q个操作,操作描述如下 昊昊把第l天到第r天的运动全部换成了x(x∈[1,m]) 问昊昊第l天到第r天参加了 ...
- cdojQ - 昊昊爱运动 II
地址:http://acm.uestc.edu.cn/#/contest/show/95 题目: Q - 昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) ...
- UESTC-1259 昊昊爱运动 II
昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 昊昊喜 ...
- cdoj 1256 昊昊爱运动 预处理
昊昊爱运动 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 昊昊喜欢运动 他NN ...
- bzoj 3704: 昊昊的机油之GRST【贪心+脑洞】
脑洞题大概 首先处理出每个位置需要操作的次数c,假设第一次达到目标就不能再走,这样的操作次数是c差分后值的正数和,就想成分治每一段然后同减最小值然后从0处断开 然后考虑能一圈一圈走的情况,连续一段多走 ...
随机推荐
- Spring IOC原理解读 面试必读
Spring源码解析:Bean实例的创建与初始化 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. IoC容器 ...
- Codeforces Round #542 题解
Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...
- debug_backtrace
<?php one(); function one() { two(); } function two() { three(); } function three() { print_r( de ...
- 1. Scala概述
1.1 概述 联邦理工学院洛桑(EPFL)的Martin Odersky于2001年开始设计Scala Scala是Scalable Language的简写,是一门多范式的编程语言 1.2 Scala ...
- 免费为网站加上HTTPS
前言 最近有好几位同学直接微信赞助说快点更新文章.这个要和大家说声抱歉,的确很久没有写文章了.我们也不找借口,我会尽力保证多写文章.今天我们的主题来讲解 如何给自己的网站 加上HTTPS HTTPS是 ...
- robotframework RF使用中需要安装的工具和库
确保 Python 3.6.2 安装成功 安装 如下 RF使用中需要的工具和库 1. RF 在两个Python中安装 robotframework执行命令 pip install robotframe ...
- flutter 学习零碎知识点01
1.Expanded组件 占满可用空间 -----可以到达类似flex布局中 第一列占用大量空间,所以它必须包装在Expanded widget中. 写死的高度改成Expanded自动撑满屏幕如果还 ...
- xcode 10 模拟器报错
xcode 10(也可能是任意版本)run 模拟器时,发现会报下面的错误. This app could not be installed at this time.Could not access ...
- vue中loding
<template> <div class="mf-loading-container" v-show="ifShow"> <im ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...