codeforces1073d Berland Fair 思维(暴力删除)
题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品。
思路:此题的关键是不能买则走到下一家,一旦走到下一家,我们会发现之前的这家以后无论转几圈我们都买不起,所以直接把这个店删掉就可以了。
于是先将n当成周期,算出此时的sum,和原来的money比较,能买几个周期则买几个周期,然后遍历双向链表,不能买则删去,更新周期和sum,继续判断能不能买得起此时的周期,然后走到下一家店,直到剩下一家店。
由于每家店最多被删去一次,所以时间复杂度是O(n)的,只要注意链表的一些细节处理就可以了。
//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read(){
ll x=,f=;
char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;}
const int maxn=2e5+;
struct node{
ll val;
int pre,Next;
}a[maxn];
ll sum,money,ans;
int main(){
int n;
cin>>n>>money;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i].val);
sum+=a[i].val;
a[i].pre=i-,a[i].Next=i+;
}
a[].pre=n,a[n].Next=;
if(money>=sum)
{
ans+=money/sum*n;
money%=sum;
}
int i=;
while(a[i].Next!=i)
{
if(money>=a[i].val)
{
ans++; money-=a[i].val;
if(money<=)break;
i=a[i].Next;
}else{
a[a[i].pre].Next=a[i].Next;
a[a[i].Next].pre=a[i].pre;
sum-=a[i].val;
n--;
if(money>=sum&&a[i].Next!=a[i].pre)
{
ans+=money/sum*n;
money%=sum;
}
if(money<=)break;
i=a[i].Next;
}
}
if(money>=sum)
{
ans+=money/sum*n;
money%=sum;
}
printf("%lld\n",ans);
}
2 seconds
256 megabytes
standard input
standard output
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nn booths, arranged in a circle. The booths are numbered 11through nn clockwise with nn being adjacent to 11. The ii-th booths sells some candies for the price of aiai burles per item. Each booth has an unlimited supply of candies.
Polycarp has decided to spend at most TT burles at the fair. However, he has some plan in mind for his path across the booths:
- at first, he visits booth number 11;
- if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
- then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
Calculate the number of candies Polycarp will buy.
The first line contains two integers nn and TT (1≤n≤2⋅1051≤n≤2⋅105, 1≤T≤10181≤T≤1018) — the number of booths at the fair and the initial amount of burles Polycarp has.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the price of the single candy at booth number ii.
Print a single integer — the total number of candies Polycarp will buy.
3 38
5 2 5
10
5 21
2 4 100 2 6
6
Let's consider the first example. Here are Polycarp's moves until he runs out of money:
- Booth 11, buys candy for 55, T=33T=33;
- Booth 22, buys candy for 22, T=31T=31;
- Booth 33, buys candy for 55, T=26T=26;
- Booth 11, buys candy for 55, T=21T=21;
- Booth 22, buys candy for 22, T=19T=19;
- Booth 33, buys candy for 55, T=14T=14;
- Booth 11, buys candy for 55, T=9T=9;
- Booth 22, buys candy for 22, T=7T=7;
- Booth 33, buys candy for 55, T=2T=2;
- Booth 11, buys no candy, not enough money;
- Booth 22, buys candy for 22, T=0T=0.
No candy can be bought later. The total number of candies bought is 1010.
In the second example he has 11 burle left at the end of his path, no candy can be bought with this amount.
codeforces1073d Berland Fair 思维(暴力删除)的更多相关文章
- 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 ...
- ZOJ - 3983 - Crusaders Quest(思维 + 暴力)
题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...
- CodeForces - 1073D Berland Fair
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...
- 【数据结构】【CF1073D】 Berland Fair
Description 给定 \(n\) 个商店,他们围成一个圆圈,按照顺时针从 \(1\) 到 \(n\) 编号.你有 \(T\) 元钱,从 \(1\) 号点开始按照顺时针方向走,每到一个商店,只要 ...
- Codeforces 1073D:Berland Fair(模拟)
time limit per test: 2 secondsmemory limit per test: 256 megabytesinput: standard inputoutput: stand ...
- Nikita and string [思维-暴力] ACM
codeforces Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes O ...
- [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]
http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- codeforces 768 C. Jon Snow and his Favourite Number(思维+暴力)
题目链接:http://codeforces.com/contest/768/problem/C 题意:给出n个数,k个操作,和一个x,每次操作先排序然后对奇数位数进行xor x操作,最后问k次操作后 ...
随机推荐
- 基于jquery 的插件,让IE支持placeholder属性
开发一个项目的时候为了美观和用户体验用到了input标签的placeholder属性,但是这个属性是html5中的,所以低版本的IE浏览器不支持.于是在百度找了一些解决方法,找了好几个都不是那么完美, ...
- docker问题:docker端口映射错误
1 docker端口映射错误 1.1 问题描述 利用docker启动nginx容器的时候报错: 1.2 解决办法 一次执行下面的命令就可以解决 pkill docker iptables -t nat ...
- VBox 安装 Ubuntu Server 的那些坑,键盘乱码、网卡互连、共享目录等
1.更新,相信大家都是有强迫症的 sudo apt-get update sudo apt-get upgrade 出现错误:Could not open lock file /var/lib/dpk ...
- Docker保存修改后的镜像
1.启动镜像并做出修改 docker run -it centos /bin/bash [root@afcaf46e8305 /]# 注意afcaf46e8305是产生的容器ID,前面运行的时候不要- ...
- c++虚析构函数的使用及其注意点
// ConsoleApplication33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- 无法链接到windows服务
1.先将鼠标移动到桌面右下角的显示桌面按钮处,选择右侧按钮列表中的搜索列表,输入cmd.exe,右击搜索结果,选择以管理员身份运行. 2.然后输入netsh winsock reset catalog ...
- App常用性能测试工具清单
APP的CPU,内存,耗电,流量测试工具 APP的CPU,内存,耗电,流量测试工具下载地址,后续文章会介绍如何使用Emmagee.itest.gt APP应用的CPU,内存,耗电,流量调查(可和同类产 ...
- oracle数据库之数据插入、修改和删除
作为一合格的测试人员对数据库的单表查询.多表查询.分组查询.子查询等等这些基本查询方法还是要会的.不然到企业中,容易被一些人鄙视,或者说如果数据库学不好,表查不明白,那么对自己能力来说也是一种侮辱,因 ...
- Oracle ERP系统借贷关系表
Oracle ERP系统借贷关系表 成本核算会计信息归纳 按照事务处理的来源类型归纳. 一. 采购接收入库和退货: 1.接收: 借:材料采购 (订单价格) 贷:应计负债 (订单价格) 2.入库: ...
- 简单接触oracle数据库nvl函数decode函数
SQL语句的DECODE()和NVL()函数用法 SELECT DECODE(choose_tool,0,'宝马',1,'电动车',2,'自行车','步行') AS my_tool FROM dat ...