Problem K. Master of Sequence(二分)
Problem K. Master of Sequence(二分)
补补题,人太菜了,一个题解看了两个小时才看明白(当然也可能是比赛的时候这个题完全不知道怎么下手qwq)
题目链接:http://acm.hdu.edu.cn/downloads/CCPC2018-Hangzhou-ProblemSet.pdf
分析思路
二分还是比较好想的,查询大于一个数的最小值呗就~~
题的主要难点在于这个求和,下取整(当时比赛连难点都没找到,做的明白才奇怪呢)
突破点在这个a数组,数据范围只有1000。那我们可以用二维数组存这个余数,用前缀和搞出来分母是a[i]时大于等于这个余数的个数,所以我们预处理这个:
for (i = 1; i <= n; i++)
cin >> a[i];
for (i = 1; i <= n; i++)
{
cin >> b[i];
yu[a[i]][b[i] % a[i]]++;//余数数组分母是a[i]时的余数个数
ans += b[i] / a[i];
}
然后前缀和
for (i = 1; i <= 1000; i++)
for (j = i - 1; j >= 0; j--)
yu[i][j] += yu[i][j + 1];
因为下取整,我们不能让这个答案少算或者漏算。
在后面我们二分答案的时候,把这个多余的余数产生的答案减去就可以了
check函数
bool check(int ans, int mid)
{
int res = 0;
for (int i = 1; i <= 1000; i++)
{
res += mid / i * yu[i][0];
res -= yu[i][mid % i + 1];
}
if (res >= ans)
return true;
return false;
}
完整代码
/*made in dirt & sand */
#include <bits/stdc++.h>
#include <deque>
#define bug(a) cout << a << endl;
#define mem(a, b) memset(a, b, sizeof a)
#define bug2(a, b) cout << a << ' ' << b << endl;
#define bug3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define pb push_back
#define int long long
#define x first
#define y second
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int inf = 1e-6;
int i, j, k, n, m, l, x, y, t, ans[N], a[N], b[N];
int yu[1010][1010];
bool check(int ans, int mid)
{
int res = 0;
for (int i = 1; i <= 1000; i++)
{
res += mid / i * yu[i][0];
res -= yu[i][mid % i + 1];
}
if (res >= ans)
return true;
return false;
}
signed main()
{
//freopen("black.in","r",stdin);
std::ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
memset(yu, 0, sizeof yu);
int ans = 0;
cin >> n >> m;
for (i = 1; i <= n; i++)
cin >> a[i];
for (i = 1; i <= n; i++)
{
cin >> b[i];
yu[a[i]][b[i] % a[i]]++;
ans += b[i] / a[i];
}
for (i = 1; i <= 1000; i++)
for (j = i - 1; j >= 0; j--)
yu[i][j] += yu[i][j + 1]; //余数
while (m--)
{
int op;
cin >> op;
if (op == 1)
{
cin >> x >> y;
for (i = b[x] % a[x]; i >= 0; i--)
yu[a[x]][i]--;
for (i = b[x] % y; i >= 0; i--)
yu[y][i]++;
ans -= b[x] / a[x];
ans += b[x] / y;
a[x] = y;
}
else if (op == 2)
{
cin >> x >> y;
for (i = b[x] % a[x]; i >= 0; i--)
yu[a[x]][i]--;
for (i = y % a[x]; i >= 0; i--)
yu[a[x]][i]++;
ans -= b[x] / a[x];
ans += y / a[x];
b[x] = y;
}
else
{
cin >> k;
int l = 0, r = 1e9;
while (l < r)
{
int mid = l + r >> 1;
if (check(ans + k, mid))
r = mid;
else
l = mid +1;
}
cout << l << endl;
}
}
}
}
/*
*
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ██████━━█████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
总结
没有总结233~
Problem K. Master of Sequence(二分)的更多相关文章
- 2017ccpc 杭州Master of Sequence
Problem K. Master of SequenceTherearetwosequencesa1,a2,··· ,an, b1,b2,··· ,bn. LetS(t) =∑n i=1⌊t−bi ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- Master of Sequence
Master of Sequence 时间限制: 10 Sec 内存限制: 128 MB 题目描述 There are two sequences a1,a2,...,an , b1,b2,..., ...
- poj 3111 K Best 最大化平均值 二分思想
poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- 机器学习理论与实战(十)K均值聚类和二分K均值聚类
接下来就要说下无监督机器学习方法,所谓无监督机器学习前面也说过,就是没有标签的情况,对样本数据进行聚类分析.关联性分析等.主要包括K均值聚类(K-means clustering)和关联分析,这两大类 ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]
题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力
Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...
随机推荐
- disconf分布式配置管理(二) 与spring集成
上一章介绍了disconf的安装预配置,这章主要介绍下disconf与spring集成 1.添加依赖 <dependency> <groupId>com.baidu.disco ...
- Blazor开发框架Known-V2.0.8
V2.0.8 Known是基于Blazor的企业级快速开发框架,低代码,跨平台,开箱即用,一处代码,多处运行.目前已有部分客户在使用,最近客户的项目和产品,有的在Docker中运行,有的在重新升级改造 ...
- 处理报错 ResizeObserver loop completed with undelivered notifications.
// 处理报错 ResizeObserver loop completed with undelivered notifications. export const handlerResizeObse ...
- Kubernetes-9:Service介绍及演示
Service Kubernetes 的Service定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问他们的策略 -- 微服务,这一组Pod能够被Service访问到,通常是通过tabel ...
- Kubernetes-6:Pod生命周期介绍(init Container)
Pod生命周期 生命周期 1.API server调用kubelet下达Pod创建指令 2.容器环境初始化 3.进入Pod生命周期内(Pod开始创建) 4.Pod只要创建,就会自动生成一个pause容 ...
- 设线性表中每个元素有两个数据项k1和k2,现对线性表按一下规则进行排序:先看数据项k1,k1值小的元素在前,大的在后;在k1值相同的情况下,再看k2,k2值小的在前,大的在后。满足这种要求的
题目: 设线性表中每个元素有两个数据项k1和k2,现对线性表按一下规则进行排序:先看数据项k1,k1值小的元素在前,大的在后:在k1值相同的情况下,再看k2,k2值小的在前,大的在后.满足这种要求的排 ...
- RxJS 系列 – Transformation Operators
前言 前几篇介绍过了 Creation Operators Filter Operators Join Creation Operators Error Handling Operators 这篇继续 ...
- SQL Server – Temporal Table 时态表
前言 之前写过一篇, 但那个时候还没有开始用, 现在是要用了, 所以翻新一下呗. SQL server temporal table 学习笔记 主要参考: 官网 Temporal tables [译] ...
- 在 ArkTS 中,如何有效地进行内存管理和避免内存泄漏?
ArkTS 是鸿蒙生态的应用开发语言,它在 TypeScript 的基础上进行了优化和定制,以适应鸿蒙系统的需求. 以下是在 ArkTS 中进行有效的内存管理和避免内存泄漏: 1. 使用 const ...
- Resource Acquisition Is Initialization
在 C++ 中,资源获取即初始化(RAII, Resource Acquisition Is Initialization)是一种管理资源的编程惯用法.其核心思想是将资源的获取和释放绑定到对象的生命周 ...