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 ...
随机推荐
- Java基础之时间类
- 2023 ECF 游记
录播 闭幕式 回顾 1.11 本来计划下午 vp 去年的 ecf,结果 zjj 忘记买票来投奔我了,所以鸽了.分块和莫队也没看 晚上收拾东西,完全装不下,应该早点寄走一些东西的 1.12 昨晚还想着别 ...
- Linux程序之可变参数&&选项那些事!
一.linux应用程序如何接收参数? 1. argc.argv Linux应用程序执行时,我们往往通过命令行带入参数给程序,比如 ls /dev/ -l 其中参数 /dev/ .-l都是作为参数传递给 ...
- XSS 基本概念和原理介绍
XSS 基本概念和原理介绍 基本概念 跨站脚本攻击 XSS(Cross Site Scripting),为了不和层叠样式表 ( Cascading Style Sheets,CSS ) 的缩写混淆,故 ...
- C#学习日记
2023年9月9日 工具visual stdio 2019 窗口名称修改 lable标签 button 点击事件 点击换颜色 formLearn.ActiveForm.BackColor = Colo ...
- Spring Boot 框架中配置文件 application.properties 当中的所有配置大全
Spring Boot 框架中配置文件 application.properties 当中的所有配置大全 #SPRING CONFIG(ConfigFileApplicationListener) s ...
- man 切换颜色配置
man 命令显示的命令手册默认是没有颜色的.为了使 man 命令的输出更为生动,可以使用如下两种方法修改 man 命令的颜色配置. 方法一:设置环境变量 在你的 .zshrc / .bashrc 中添 ...
- Android Studio 项目已经sync完成,但是在布局中显示:Design editor is unavaliable until after a sunncessful project sync
原因:在drawable文件夹中新增了一个png图标 解决:同步在drawable-v24文件中复制一份即可
- windows服务器使用 azure devops 批量自动发布网站到IIS
最近由于一个API项目,需要利用负载均衡来做支撑,因此需要将同一份代码分发到多个服务器,之前一直手工干这个活,感觉太累了,去开发交流群,还有搜索了一下资料,发现很多人推荐我用ftp.还有磁盘共享等这种 ...
- vue项目自动导入components
开发项目中一般组件都放在 components 目录下,对于一些高频使用的组件我们需要在入口文件中设置为全局组件, 一个一个搞,很繁琐,这里通过webpack自动挂载components为全局组件. ...