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. 【牛客刷题】HJ3 明明的随机数

    题目链接 这题有两个要编码解决的问题,首先是去重,其次是排序. 最开始想着就用Java的TreeSet解决了,简单好用,去重排序都一并解决了,编码只需要考虑input的逻辑就可以,代码如下: impo ...

  2. ptmalloc2涉及的基础知识与基本数据结构

    随笔来源:ctfwiki CSDN 本随笔只为记录分析总结的自己学习的结论,方便未来回顾,以及为他人提供一个理解的思路,不保证正确.如有谬误,请大家指出. 1.堆相关的操作 malloc:返回对应大小 ...

  3. 【YashanDB数据库】YAS-00413 wait for receive timeout

    [问题分类]错误码处理 [关键字]yasql,00413 [问题描述]使用工具设置不同并发迁移数据的过程中,导致yasql登录报错:YAS-00413 wait for receive timeout ...

  4. Redis过期策略以及Redis的内存淘汰机制

    此篇介绍了Redis过期策略以及Redis的内存淘汰机制,从内存淘汰的8种策略,如何开启内存淘汰策略到如何选择合适的淘汰策略,对Redis的内存淘汰机制做了全方位的阐述 如何高效的使用内存对于redi ...

  5. [python][selenium] Web UI自动化页面切换iframe框架

    关联文章:Web UI自动化8种页面元素定位方式 1.切换iframe的方法:switch_to.frame  入参有4种:  1.1.id  1.2.name  1.3.index索引  1.4.i ...

  6. JavaScript – 单线程 与 执行机制 (event loop)

    前言 因为在写 RxJS 系列,有一篇要介绍 Scheduler.它需要对 JS 执行机制有点了解,于是就有了这里篇. 参考 知乎 – 详解JavaScript中的Event Loop(事件循环)机制 ...

  7. 【linux】【docker】Docker默认网段配置导致无法访问

    背景 集团有N个基地,所有基地的网络使用的是172.x.x.x网段,这本身没有什么问题!但Docker默认的桥接网段也是172.17.x.x的,如果不修改docker的默认配置会导致个别基地无法访问! ...

  8. Material Design In XAML Toolkit 5.0.0 Migration Guide

    MaterialDesignInXamlToolkit 5.0有破坏性的更新,下面的连接可以用于4.x升级到5.0的一个手册.仅供参考,欢迎升级5.0时使用. https://github.com/M ...

  9. PS安装插件提示无法加载扩展未正确签署 的解决办法

    PS安装插件提示无法加载扩展未正确签署解决方式 win系统: 1.打开"运行"窗口(点击电脑左下角"开始"菜单,从打开的菜单中依次点击"所有程序&qu ...

  10. CTF中特别小的EXE是怎么生成的

    我们在打CTF时候,出题的爷爷们给出的exe都很小 就10k左右,有的甚至就5k,那时候我很郁闷啊.现在我也能了啊哈哈 不多bb按如下操作: 我们来看看正常的release生成的代码 #include ...