【数据结构】【CF1073D】 Berland Fair
Description
给定 \(n\) 个商店,他们围成一个圆圈,按照顺时针从 \(1\) 到 \(n\) 编号。你有 \(T\) 元钱,从 \(1\) 号点开始按照顺时针方向走,每到一个商店,只要钱够就必须买这个商店的物品。商店中物品是无限的,即多次到达可能多次购买。求会买多少件物品
Input
第一行是一个整数 \(n\)
下面一行 \(n\) 个整数 \(a_i\),代表每个商店物品的价格
Output
一行一个整数代表答案
Hint
\(1~\leq~n~\leq~2~\times~10^5~,~1~\leq~T~\leq~10^{18}~,~1~\leq~a_i~\leq~10^9\)
Solution
解法一:
显然在钱数充足是我们可以直接除一下得到我们可以转多少圈,\(O(1)\) 统计这部分答案,然后把钱数取模即为这些圈转完后我们剩下的钱,这时的钱数是不足以转完一圈的。
下面我们考虑用这些钱可以连续走多远,即会到哪一个商店停下来。注意到这个值是可以二分的,具体的,我们维护一个前缀和,二分哪个位置的前缀和是最大的小于钱数的即可。然后剩下那个位置的商店显然再也不会被购买到了,于是可以将它直接删去,然后统计这一段连续走的位置的答案。删除这个位置后的前缀和可以用树状数组或线段树维护,一段区间中还剩多少个没有被删去的位置也可以树状数组维护。对于这个位置后面能连续走到哪里,我们依然可以二分这个值,以此类推直到一圈走完,然后从头开始重复流程即可。
注意到每次二分我们一定会删除一个位置,所以我们会二分 \(O(n)\) 次,直接二分+树状数组/线段树的话,单次二分复杂度 \(O(\log^2 n)\),总复杂度 \(O(n \log^2 n)\)。如果在线段树上二分,单次复杂度可以做到 \(O(\log n)\),总复杂度 \(O(n \log n)\)。
但是天知道为什么我的两个log算法跑的比一个log的还快
解法二:
算出当前的钱数可以走多少圈的方法同上,然后考虑我们暴力跑一圈,统计有哪些位置的是不能被购买到的,直接删掉。然后用剩下的钱再取模。
注意到 \(T\) 每次取模时的模数一定小于 \(T\),而一个数被比自己小的数取模至少减少二分之一,证明上可以分模数大于或小于等于 \(\frac{m}{2}\) 进行讨论。于是 \(T\) 被取模 \(O(\log T)\) 次,每次对应一次 \(O(n)\) 的统计答案,于是总复杂度 \(O(n \log T)\)。
Code
依据解法一写成。
\(O(n \log^2n)\)
#include <cstdio>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 200010;
int n;
ll t, ans;
int MU[maxn];
ll tree[maxn], val[maxn];
inline int lowbit(ci x) {return x & (-x);}
ll ask(ll*, int);
int check(int, ll);
void update(ll*, int, ci);
signed main() {
freopen("1.in", "r", stdin);
qr(n); qr(t);
for (rg int i = 1; i <= n; ++i) {qr(MU[i]); update(tree, i, MU[i]); update(val, i, 1);}
ll s = ask(tree, n); int cnt = n;
while (cnt) {
if (!t) break;
ans += t / s * cnt; t %= s;
int k = 0;
do {
int pre = k;
k = check(k, t);
if (k > n) {
t -= ask(tree, n) - ask(tree, pre);
ans += ask(val, n) - ask(val, pre);
break;
};
update(tree, k, -MU[k]); update(val, k, -1); --cnt;
t -= ask(tree, k - 1) - ask(tree, pre);
s -= MU[k];
ans += ask(val, k) - ask(val, pre);
} while (cnt && t);
}
qw(ans, '\n', true);
}
void update(ll *ar, int x, ci v) {
while (x <= n) {
ar[x] += v;
x += lowbit(x);
}
}
ll ask(ll* ar, int x) {
ll _ret = 0;
while (x) {
_ret += ar[x];
x -= lowbit(x);
}
return _ret;
}
int check(int pre, ll x) {
int l = pre, r = n + 1, mid = l, _ret = 0;
while (l <= r) {
mid = (l + r) >> 1;
if ((ask(tree, mid) - ask(tree, pre)) <= x) _ret = mid, l = mid + 1;
else r = mid - 1;
}
return _ret + 1;
}
\(O(n \log n)\)
#include <ctime>
#include <cstdio>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
int buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = int(x % 10 + '0');} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 200010;
const int maxt = 400010;
int n;
ll t, ans;
int tree[maxn], MU[maxn];
struct Tree {
Tree *ls, *rs;
int l, r;
ll v;
inline void pushup() {
this->v = this->ls ? (this->rs ? this->ls->v + this->rs->v : this->ls->v) : this->rs->v;
}
};
Tree *pool[maxt], qwq[maxt], *rot;
int top;
inline int lowbit(ci x) {return x & -x;}
int check(int, ll);
void buildpool();
void build(Tree*, ci, ci);
void update(int, ci);
void update(Tree*, ci);
ll ask(Tree*, ci, ci);
int ask(int);
int ask(Tree*, cl);
signed main() {
freopen("1.in", "r", stdin);
qr(n); qr(t); ll s = 0;
for (rg int i = 1; i <= n; ++i) {qr(MU[i]); s+= MU[i]; update(i, 1);}
int cnt = n;
buildpool();
build(rot, 1, n);
while (cnt) {
if (!t) break;
ans += t / s * cnt; t %= s;
int k = 0;
do {
int pre = k;
k = check(k, t);
if (k > n) {
t -= ask(rot, pre + 1, n);
ans += ask(n) - ask(pre);
break;
};
update(rot, k); update(k, -1); --cnt;
t -= ask(rot, pre, k - 1);
s -= MU[k];
ans += ask(k) - ask(pre);
} while (cnt && t);
}
qw(ans, '\n', true);
}
int check(int pre, ll x) {
x += ask(rot, 1, pre);
ll s = ask(rot, 1, n);
if (s <= x) return n + 1;
return ask(rot, x);
}
void buildpool() {
for (rg int i = 0; i < maxt; ++i) pool[i] = qwq + i;
rot = pool[maxt - 1]; top = maxt - 2;
}
void build(Tree *u, ci l, ci r) {
u->l = l; u->r = r;
if (l == r) {u->v = MU[l]; return;}
int mid = (l + r) >> 1;
if (l <= mid) build(u->ls = pool[top--], l, mid);
if (mid < r) build(u->rs = pool[top--], mid + 1, r);
u->pushup();
}
void update(int x,ci v) {
while (x <= n) {
tree[x] += v;
x += lowbit(x);
}
}
void update(Tree* u, ci x) {
if ((u->l > x) || (u->r < x)) return;
if (u->l == u->r) {u->v = 0; return;}
if (u->ls) update(u->ls, x);
if (u->rs) update(u->rs, x);
u->pushup();
}
int ask(int x) {
int _ret = 0;
while (x) {
_ret += tree[x];
x -= lowbit(x);
}
return _ret;
}
ll ask(Tree *u, ci l, ci r) {
if ((u->l > r) || (u->r < l)) return 0;
if ((u->l >= l) && (u->r <= r)) return u->v;
return u->ls ? (u->rs ? ask(u->ls, l, r) + ask(u->rs, l, r) : ask(u->ls, l, r)) : ask(u->rs, l, r);
}
int ask(Tree *u, cl v) {
if (u->l == u->r) return u->l;
if (u->ls->v <= v) return ask(u->rs, v - u->ls->v);
else return ask(u->ls, v);
}
Summary
该define int ll就要define啊= =要不然可能会fst的很惨= =
一个数对比自己小的数取模一次至少减少一半。
【数据结构】【CF1073D】 Berland Fair的更多相关文章
- cf1073D Berland Fair (二分答案+树状数组)
用一个树状数组维护前缀和,每次我二分地找一个位置,使得我能一路买过去 但这个买不了 那以后肯定也都买不了了,就把它改成0,再从头二分地找下一个位置,直到这一圈我可以跑下来 然后就看跑这一圈要花多少钱. ...
- cf1073d Berland Fair
~~~题面~~~ 题解: 可以发现,每走完一圈付的钱和买的数量是有周期性的,即如果没有因为缺钱而买不起某家店的东西,那么这一圈的所以决策将会和上一圈相同,这个应该是很好理解的,想想就好了. 因为钱数固 ...
- CF1073D Berland Fair 二分+线段树
考场上切的,挺简单的~ Code: #include <cstdio> #include <algorithm> #define N 200005 #define inf 10 ...
- CodeForce edu round 53 Div 2. D:Berland Fair
D. Berland Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces1073d Berland Fair 思维(暴力删除)
题目传送门 题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品. 思路:此题的关键是不能买则走到下一家 ...
- 【CF1073D】Berland Fair(模拟)
题意:初始有t元,每次从1开始买,从1到n依次有n个人,每个人的东西价格为a[i],该人依次能买就买,到n之后再回到1从头开始,问最后能买到的东西数量 n<=2e5,t<=1e18,a[i ...
- CodeForces - 1073D Berland Fair
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...
- Codeforces 1073D:Berland Fair(模拟)
time limit per test: 2 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: stand ...
- Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair
题意:一个人 有T块钱 有一圈商店 分别出售 不同价格的东西 每次经过商店只能买一个 并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...
随机推荐
- openstack系列文章(二)
学习openstack的系列文章-keystone openstack 架构 Keystone 基本概念 Keystone 工作流程 Keystone Troubleshooting 1. open ...
- Codeforces Round #515 (Div. 3) 解题报告(A~E)
题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...
- XGB算法梳理
学习内容: 1.CART树 2.算法原理 3.损失函数 4.分裂结点算法 5.正则化 6.对缺失值处理 7.优缺点 8.应用场景 9.sklearn参数 1.CART树 CART算法是一种二分递归分割 ...
- PHP 伪协议
1.file:// file://用于访问本地文件系统,不受allow_url_fopen影响 <?php include($_GET['file']); ?> 2.http:// GET ...
- unzip/tar命令详解
博客目录总纲首页 原文链接:https://www.cnblogs.com/zdz8207/p/3765604.html Linux下的压缩解压缩命令详解及实例 实例:压缩服务器上当前目录的内容为xx ...
- iframe子页面position的fixed
前言: 首先说一说我昨天天的苦逼经历.中午吃饭时一同事跟我说,他做的项目嵌套iframe后,子页面的position设置fixed失效了. 经过反复询问,得知他用了两层iframe,再加上最外的父页面 ...
- System 类的使用
/*System 系统类 主要用于获取系统的属性数据.System类常用的方法: arraycopy(Object src, int srcPos, Object dest, int destPos, ...
- 深入理解Java类加载器(1)
类加载器概述: java类的加载是由虚拟机来完成的,虚拟机把描述类的Class文件加载到内存,并对数据进行校验,解析和初始化,最终形成能被java虚拟机直接使用的java类型,这就是虚拟机的类加载机制 ...
- 简单Profibus/DP实验系统的组建
转自:http://www.dndev.com/Profibus/profibustr/system_4.html 引言: 为了让更多刚接触到Profibus系统的朋友能对Profibus的网络架构及 ...
- redis简介及增删改查
redis 是一个文档(nosql)数据库,工作与内存,主要用做高速缓存 缓存经常会查到的数据 存入的值默认是字符串 使用步骤: 1 从redis.io下载 2 点击redis-server.exe启 ...