博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces1073d Berland Fair 思维(暴力删除)
阅读量:5145 次
发布时间:2019-06-13

本文共 3750 字,大约阅读时间需要 12 分钟。

  题目大意:一圈人围起来卖糖果,标号从1-n,每个位置的糖果都有自己的价格,一个人拿着钱从q开始走,能买则买,不能买则走到下一家,问最多能买多少件物品。

  思路:此题的关键是不能买则走到下一家,一旦走到下一家,我们会发现之前的这家以后无论转几圈我们都买不起,所以直接把这个店删掉就可以了。

  于是先将n当成周期,算出此时的sum,和原来的money比较,能买几个周期则买几个周期,然后遍历双向链表,不能买则删去,更新周期和sum,继续判断能不能买得起此时的周期,然后走到下一家店,直到剩下一家店。

  由于每家店最多被删去一次,所以时间复杂度是O(n)的,只要注意链表的一些细节处理就可以了。

//#pragma comment(linker,"/STACK:102400000,102400000")#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#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=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=2e5+10;struct node{ ll val; int pre,Next;}a[maxn];ll sum,money,ans;int main(){ int n; cin>>n>>money; for(int i=1;i<=n;i++) { scanf("%lld",&a[i].val); sum+=a[i].val; a[i].pre=i-1,a[i].Next=i+1; } a[1].pre=n,a[n].Next=1; if(money>=sum) { ans+=money/sum*n; money%=sum; } int i=1; while(a[i].Next!=i) { if(money>=a[i].val) { ans++; money-=a[i].val; if(money<=0)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<=0)break; i=a[i].Next; } } if(money>=sum) { ans+=money/sum*n; money%=sum; } printf("%lld\n",ans);}
View Code

 

D. Berland Fair
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

The first line contains two integers nn and TT (1n21051≤n≤2⋅105, 1T10181≤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 (1ai1091≤ai≤109) — the price of the single candy at booth number ii.

Output

Print a single integer — the total number of candies Polycarp will buy.

Examples
input
Copy
3 38 5 2 5
output
Copy
10
input
Copy
5 21 2 4 100 2 6
output
Copy
6
Note

Let's consider the first example. Here are Polycarp's moves until he runs out of money:

  1. Booth 11, buys candy for 55, T=33T=33;
  2. Booth 22, buys candy for 22, T=31T=31;
  3. Booth 33, buys candy for 55, T=26T=26;
  4. Booth 11, buys candy for 55, T=21T=21;
  5. Booth 22, buys candy for 22, T=19T=19;
  6. Booth 33, buys candy for 55, T=14T=14;
  7. Booth 11, buys candy for 55, T=9T=9;
  8. Booth 22, buys candy for 22, T=7T=7;
  9. Booth 33, buys candy for 55, T=2T=2;
  10. Booth 11, buys no candy, not enough money;
  11. 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.

 

 

转载于:https://www.cnblogs.com/mountaink/p/9941089.html

你可能感兴趣的文章
绝望的第四周作业
查看>>
一月流水账
查看>>
npm 常用指令
查看>>
判断字符串在字符串中
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>
HashPump用法
查看>>
cuda基础
查看>>
Vue安装准备工作
查看>>
oracle 创建暂时表
查看>>
201421410014蒋佳奇
查看>>
Xcode5和ObjC新特性
查看>>
LibSVM for Python 使用
查看>>
Centos 7.0 安装Mono 3.4 和 Jexus 5.6
查看>>
CSS属性值currentColor
查看>>
java可重入锁reentrantlock
查看>>
浅谈卷积神经网络及matlab实现
查看>>
解决ajax请求cors跨域问题
查看>>
《收获,不止Oracle》pdf
查看>>
LinkedList<E>源码分析
查看>>
Real-Time Rendering 笔记
查看>>