UVALive 7501 Business Cycle
细心题
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define ms(arr,a) memset(arr,a,sizeof arr)
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
typedef long long ll;
const int maxn=1e5+5;
int n;
ll g,p;
int a[maxn];
ll dp[maxn],sum[maxn];
bool ok1(ll m)
{
if(m>=g)return true;
int tmp=min(ll(n),p);
for(int i=0;i<tmp;++i)
{
m=m+a[i]<=0?0:m+a[i];
if(m>=g)return true;
}
return false;
}
bool ok2(ll m)
{
for(int i=0;i<n;++i)
{
m=m+a[i]<=0?0:m+a[i];
if(m>=g)return true;
}
ll gain,gg=0;
if(sum[n-1]<=0)
{
int tmp=p<2*n?p-n:n;
gain=dp[tmp-1];
}
else
{
if(p<2*n)gain=dp[p-n-1];
else
{
int tmp=p%n?p%n+n:n;
if(((g-m)/sum[n-1])<=((p-n-tmp)/n))return true;
gain=(p-n-tmp)/n*sum[n-1];
gg=max(dp[tmp%n-1]+sum[n-1],dp[n-1]);
}
}
if(m+gain>=g-gg)return true;
return false;
}
int main()
{
//freopen("Input.txt","r",stdin);
//freopen("1.txt","w",stdout);
int T;scanf("%d",&T);
for(int Case=1;Case<=T;++Case)
{
scanf("%d%lld%lld",&n,&g,&p);
for(int i=0;i<n;++i)scanf("%d",a+i);
sum[0]=a[0];dp[0]=a[0]>0?a[0]:0;
for(int i=1;i<n;++i)
{
sum[i]=sum[i-1]+a[i];
dp[i]=sum[i]>dp[i-1]?sum[i]:dp[i-1];
}
if(p<=n)
{
ll l=0,m,r=g;
while(l<r)
{
m=(l+r)/2;
if(ok1(m))r=m;
else l=m+1;
}
printf("Case #%d: %lld\n",Case,l);
}
else
{
ll l=0,m,r=g;
while(l<r)
{
m=(l+r)/2;
if(ok2(m))r=m;
else l=m+1;
}
printf("Case #%d: %lld\n",Case,l);
}
}
//freopen("CON","w",stdout);
//system("start 1.txt");
}
UVALive 7501 Business Cycle的更多相关文章
- UVALive 7501 Business Cycle(二分)题解
题意:n个数,有一个起始值,按顺序从第一个开始不断循环取数,如果取完后相加小于0就变为0,最多取p个数,问你得到大于等于值g所需要的最小起始值为多少 思路:这题目爆long long爆的毫无准备,到处 ...
- 2015 ACM/ICPC EC-Final
A. Boxes and Balls 二分找到最大的不超过$n$的$\frac{x(x+1)}{2}$形式的数即可. #include <bits/stdc++.h> using name ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- ORGANISING THE TEST CASES
ORGANISING THE TEST CASES -Test note of “Essential Software Test Design” 2015-09-24 目录 22.1 Test Cas ...
- AIMR 固定收益推荐读物
目录 AIMR Suggested Fixed-Income Readings I. Perspectives on Interest Rates and Pricing of Traditional ...
- UVALive - 5135 - Mining Your Own Business(双连通分量+思维)
Problem UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...
- UVALive - 5135 Mining Your Own Business
刘汝佳白书上面的一道题目:题意是给定一个联通分量,求出割顶以及双连通分量的个数,并且要求出安放安全井的种类数,也就是每个双连通分量中结点数(除开 割顶)个数相乘,对于有2个及以上割顶的双连通分量可以不 ...
- UVALive 5135 Mining Your Own Business 双连通分量 2011final
题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太平井逃脱, ...
- Business Cards UVALive - 4384(画图看图。。)
只能由三种情况 都横着放 都竖着放 横和竖交错放 那就去判断好了... 具体看代码 #include <iostream> #include <cstdio> #inclu ...
随机推荐
- F - F HDU - 1173(二维化一维-思维)
F - F HDU - 1173 一个邮递员每次只能从邮局拿走一封信送信.在一个二维的直角坐标系中,邮递员只能朝四个方向移动,正北.正东.正南.正西. 有n个需要收信的地址,现在需要你帮助找到一个地方 ...
- H、Magic necklace
链接:https://ac.nowcoder.com/acm/contest/3570/H 来源:牛客网 题目描述 There was a magic necklace. The necklace i ...
- Python多线程同步互斥锁
接着上篇多线程继续讲,上篇最后的多线程共享全局变量对变量的处理值出错在本文中给出解决方案. 出现这个情况的原因是在python解释器中GIL全局解释器锁. GIL:全局解释器锁,每个线程在执行的过程都 ...
- 1067 Sort with Swap(0, i) (25分)
Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...
- gdb调试工具常用命令 && kdb
编译程序时需要加上-g,之后才能用gdb进行调试:gcc -g main.c -o main gdb中命令: 回车键:重复上一命令 (gdb)help:查看命令帮助,具体命令查询在gdb中输入help ...
- Linux下删除大量文件效率对比
来自公众号:马哥Linux运维 今天我们来测试一下Linux下面删除大量文件的效率. 首先建立50万个文件 $ test for i in $(seq 1 500000);do echo text ...
- Pormetheus(一)
(1)Prometheus由来普罗米修斯的灵感来自于谷歌的Borgmon.它最初是由马特·t·普劳德(Matt T. Proud)作为一个研究项目开发的,普劳德曾是谷歌(google)的一名雇员.在普 ...
- ******Prometheus(二)***********
Prometheus的数据与安全模型: (1)Metric (度量)名字就是描述采集数据的名称——例如,website_visits_total 作为网站访问总数.名称可以包含ASCII字母.数字.下 ...
- 模仿NetFlix首页效果
之前写过UWP 带左右滚动按钮的横向ListView———仿NetFlix首页河的设计,讲述了如何设计一个带有左右滚动按钮横向的ListView. 不过我在半年之前挖了一个坑,由于工作关系,没时间来填 ...
- HashMap之KeySet分析
本篇涵盖 1.HashMap并不是用keySet来存储key的原因及证明 2.keySet方法返回后的remove.add操作原理 一.方法作用 概括一下 1.keySet方法返回map中包含的键的集 ...