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. JavaScript 事件循环竟还能这样玩!

    JavaScript 是一种单线程的编程语言,这意味着它一次只能执行一个任务.为了能够处理异步操作,JavaScript 使用了一种称为事件循环(Event Loop)的机制. 本文将深入探讨事件循环 ...

  2. WinUI 3学习笔记(1)—— First Desktop App

    随着Visual Studio 2019 16.10版本的正式发布,创建WinUI 3的APP对我们来说,已不存在任何的难度.本篇我们就试着来一探究竟,看看WinUI 3 APP到底是个啥玩意,能不能 ...

  3. Linux——添加默认路由(能ping通本网段,但是ping不通其他网段)

    2024/07/15 1.问题描述 2.问题处理 3.其他问题 1.问题描述 昨天服务器突然断电,今天重启后,网络出了些问题,具体情况如下: 能ping通本机IP ping不通网关 ping不通本网段 ...

  4. wiz 为知笔记服务器 docker 迁移爬坑指北

    本文主要是介绍 wiz 为知笔记服务器 docker 从旧服务器迁移到新服务器的步骤以及问题排查. 旧服务器升级 wiz docker 目的:保持和新服务器拉取的镜像版本一致. 官方只留了 wiz d ...

  5. JSP中的JSTL 标签库

    目录 JSTL 标签库 JSTL 标签库的使用步骤 core 核心库使用 <c:set /> (使用很少) <c:if /> <c:choose><c:whe ...

  6. WebShell流量特征检测_中国菜刀篇

    80后用菜刀,90后用蚁剑,95后用冰蝎和哥斯拉,以phpshell连接为例,本文主要是对这四款经典的webshell管理工具进行流量分析和检测. 什么是一句话木马? 1.定义 顾名思义就是执行恶意指 ...

  7. 深度学习Python代码小知识点(备忘,因为没有脑子)

    现在是2024年4月24日16:58,今天摸鱼有点多,备忘一下,都写到一篇内容里面,免得分散. 1. np.concatenate()函数'np.concatenate'是NumPy库中用来合并两个或 ...

  8. LiveChat vs LiveAgent vs Front vs Email

    它们是什么? LiveChat, LiveAgent 算是同类产品. LiveChat 的核心(或者说起点)是 live chat 这个功能, 而 LiveAgent 的核心是 ticket. 如果拿 ...

  9. Docker修改IP地址方法

    一.查看Docker IP root@master:/# ifconfig docker0 docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu ...

  10. 课时09:Metasploit使用基础

    下载地址:https://docs.metasploit.com/docs/using-metasploit/getting-started/nightly-installers.html Explo ...