cf1073d Berland Fair
题解:
可以发现,每走完一圈付的钱和买的数量是有周期性的,即如果没有因为缺钱而买不起某家店的东西,那么这一圈的所以决策将会和上一圈相同,这个应该是很好理解的,想想就好了。
因为钱数固定时,决策固定,所以每次都O(n)扫一遍看当前情况下走一圈会花多少钱。
然后直接一直取这么多钱,直到钱不够为止,再进行下一次计算走一圈的钱数,显然用类似取模的东西可以解决。
为什么这样的复杂度是正确的呢?
一开始觉得是$n^2$的,还想了半天如何优化,但其实因为每次钱数都取了模,而一个数每次取模至少会减少一半,所以其实只会进行log次,所以复杂度是$nlogn$的
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 230000
#define LL long long int n, top;
LL ans, t, minn = 10000000000000000LL;
LL s[AC]; inline LL read()
{
LL x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void upmin(LL &a, LL b){
if(b < a) a = b;
} void pre()
{
n = read(), t = read();
for(R i = ; i <= n; i ++)
s[i] = read(), upmin(minn, s[i]);
} void work()
{
while(t >= minn)
{
LL tmp = t, rnt = ;
for(R i = ; i <= n; i ++)
if(tmp >= s[i]) tmp -= s[i], ++ rnt;
tmp = t - tmp;//获得实际消耗的钱
LL k = t / tmp;
ans += k * rnt;
t -= k * tmp;
}
cout << ans << endl;
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}
cf1073d Berland Fair的更多相关文章
- cf1073D Berland Fair (二分答案+树状数组)
用一个树状数组维护前缀和,每次我二分地找一个位置,使得我能一路买过去 但这个买不了 那以后肯定也都买不了了,就把它改成0,再从头二分地找下一个位置,直到这一圈我可以跑下来 然后就看跑这一圈要花多少钱. ...
- 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
Description 给定 \(n\) 个商店,他们围成一个圆圈,按照顺时针从 \(1\) 到 \(n\) 编号.你有 \(T\) 元钱,从 \(1\) 号点开始按照顺时针方向走,每到一个商店,只要 ...
- 【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号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...
随机推荐
- Git学习系列 (二)
#克隆目录并重命名为newname,默认目录名是使用.git前的grit git clone git://github.com/schacon/grit.git newname #将所有已经跟踪过的文 ...
- 一、Lambda表达式
一.Lambda是什么? Lambda是一个匿名函数,我们可以把Lambda理解为是一段可以传递的代码.可以写出简洁.灵活的代码.作为一种更紧凑的代码风格,使java的语言表达能力得到提升. 二.La ...
- Linux用户切换和密码修改
1.普通用户切换到root su - 再输入root密码,密码正确,成功切换,再输入exit则切换回普通用户 2.root切换到其他用户,例user su - user 再输入exit,则切换回roo ...
- ReadyAPI创建功能测试的方法
声明:如果你想转载,请标明本篇博客的链接,请多多尊重原创,谢谢! 本篇使用的 ReadyAPI版本是2.5.0 在ReadyAPI中有多种方法可以创建功能测试,本篇将分步操作创建功能测试. 1.从So ...
- Selenium(Python) ddt数据驱动
首先, 添加ddt模块: import unittestfrom time import sleep from ddt import ddt, data, unpack# 导入ddt模块from se ...
- 反片语 (Ananagrams,UVa 156)
题目描述: #include <iostream> #include <string> #include <cctype> #include <vector& ...
- 单机部署Fastfds+nginx
一.环境 centos6.8 x64 IP:192.168.134.128 所需软件包: libfastcommon-1.0.7.zip,FastDFS_v5.05.tar.gz,nginx-1.7 ...
- sqlserver 2008 merger语句
Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...
- C Program进阶-二维数组动态内存开辟
对于二维数组,我们知道可以用Type ArrayName[Row][Colume]的方式来定义,这是一种静态内存开辟的方式,程序在编译的时候就为该数组分配了空间,而且行和列大小也是指定的.这篇文章里我 ...
- HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)
Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...