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次操作后 ...
随机推荐
- 8-python模拟登入(无验证码)
方式: 1.手动登入,获取cookie 2.使用cookielib库 和 HTTPCookieProcessor处理器 #_*_ coding: utf-8 _*_ ''' Created on 20 ...
- ROS naviagtion analysis: costmap_2d--ObstacleLayer
博客转载自:https://blog.csdn.net/u013158492/article/details/50493676 构造函数 ObstacleLayer() { costmap_ = NU ...
- matplotlib的颜色和控制条
为了方便记忆,收藏备用 一 linestyle '-' solid line style '--' dashed line style '-.' dash-dot line style ':' dot ...
- Luogu 4251 [SCOI2015]小凸玩矩阵
BZOJ 4443 二分答案 + 二分图匹配 外层二分一个最小值,然后检验是否能选出$n - k + 1$个不小于当前二分出的$mid$的数.对于每一个$a_{i, j} \geq mid$,从$i$ ...
- HTTP请求:POST和GET的差异
1,一般情况下应用目的不同:GET是从服务器上获取数据,POST是向服务器传送数据. 2,将数据提交到服务器的方式不同:GET是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各 ...
- Part9---代码搬移不可少
1.回顾ARM启动流程就可知道需要执行代码搬移 2.代码搬移 1)起点:NAND FLASH,今天的起点是SRAM垫脚石.为什么?因为我们要从nandflash取搬移数据需要先对其进行初始化,二而我们 ...
- (转)EASYUI+MVC4通用权限管理平台
原文地址:http://www.cnblogs.com/hn731/archive/2013/07/15/3190947.html 通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在 ...
- WPF DataGrid CheckBox 多选 反选 全选
效果图 实现此效果的必要关键是 Style+DataTemplate 关键代码: <Window.Resources> <DataTemplate x:Key="Check ...
- linux下文件权限的介绍
linux操作系统下,使用ll查看该目录下所有文件及其文件权限,以下是对文件权限的介绍 d代表的是目录(或称之为文件夹) 红框内的这3个是代表3个组的权限每组都是3个 第一组rwx代表是本用户的权 ...
- winform发布桌面程序后提示需开启“目录浏览”
把发布文件里的publish.htm名字改为index.htm就好了