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(二分)的更多相关文章

  1. 2017ccpc 杭州Master of Sequence

    Problem K. Master of SequenceTherearetwosequencesa1,a2,··· ,an, b1,b2,··· ,bn. LetS(t) =∑n i=1⌊t−bi ...

  2. 计蒜客 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 ...

  3. Master of Sequence

    Master of Sequence 时间限制: 10 Sec  内存限制: 128 MB 题目描述 There are two sequences a1,a2,...,an , b1,b2,..., ...

  4. poj 3111 K Best 最大化平均值 二分思想

    poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件 ...

  5. 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 ...

  6. 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 ...

  7. 机器学习理论与实战(十)K均值聚类和二分K均值聚类

    接下来就要说下无监督机器学习方法,所谓无监督机器学习前面也说过,就是没有标签的情况,对样本数据进行聚类分析.关联性分析等.主要包括K均值聚类(K-means clustering)和关联分析,这两大类 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Linux系统内核的作用

      Linux系统内核在操作系统中扮演着至关重要的角色,其作用主要体现在以下几个方面: 进程管理:内核负责创建和销毁进程,这是操作系统对计算机上正在运行的程序进行管理的核心部分.内核通过调度器对进程进 ...

  2. 一文搞懂Cortex-A9 ADC裸机和基于Linux驱动编写方法

    前言 在嵌入式开发中,ADC应用比较频繁,本文主要讲解ADC的基本原理以及如何编写基于ARM的裸机程序和基于Linux的驱动程序. ARM架构:Cortex-A9 Linux内核:3.14 在讲述AD ...

  3. Microsoft Build 2023 After Party (Placeholder)

    一年过去了,微软BUILD又回来了! https://build.microsoft.com/en-US/home This is a placeholder page for Microsoft B ...

  4. 基于Material Design风格开源的Avalonia UI控件库

    前言 今天大姚给大家分享一款基于Material Design风格开源.免费(MIT License)的Avalonia UI控件库:Material.Avalonia. 当前项目还处于alpha阶段 ...

  5. 【测试平台开发】——04Flask后端api开发实战(一)

    一.测试平台开发模式 要开发一套平台有两种开发模式,一个[大而全],一个[小而简]. 说道[大而全]想到目前大型项目都使用的语言[JAVA],[小而简]想到的是[Python]语言. 重武器(大而全) ...

  6. Kubernetes-10:Ingress-nginx介绍及演示

    Ingress介绍 Ingress是什么? ingress 是除了 hostport  nodeport  clusterIP以及云环境专有的负载均衡器外的访问方式,官方提供了Nginx ingres ...

  7. docker 修改容器内容后更新镜像的流程

    在 Docker 中,如果你修改了一个容器的内容并希望将这些更改保存为一个新的镜像,可以按照以下步骤进行: docker version: 26.1 1. 确保容器运行 首先,确保你正在修改的容器是运 ...

  8. OData – 权限管理

    前言 OData 其实没有权限的机制, Client 可以任意的 $select, $expand. 即便它可以做简单防御设置, 但是离平常的业务需求还是很远. 一般上 query entity 常见 ...

  9. [rCore学习笔记 027]地址空间

    写在前面 本随笔是非常菜的菜鸡写的.如有问题请及时提出. 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 引言 ...

  10. 暑假集训CSP提高模拟7

    这个 T1 的 \(n^{3}\) 的 SPJ 效率还是太慢了,膜拜 SPJ 大神学长,还会画画 A.Permutations & Primes 这题感觉挺水的但是感觉有不是那么水,主要还是因 ...