题目链接: http://codeforces.com/contest/893/problem/D

  题目描述: 每天晚上一个数值a,如果a>0表示今天收入a元,<0表示亏损a元,=0表示去检验自己的卡。每天早上(在前者之前)可以去充卡,可以随便充,但是充完之后不能大于上限d。每一次去查卡的时候,卡上的钱不能是个负数,任何时刻如果卡上的钱大于d了就视为不合法,直接输出-1。问最少充几次卡可以保证n天完全合法。

  解题思路: 一开始自己是知道只在查卡的时候充卡就可以了, 然后我是找到与(d相近的s)最小距离的那个下标, 然后0的时候加上去, 在第20组测试样例的时候T掉了, 然后这道题就弃了, 看了题解才知道只需要维护两个区间, 一个是剩余最大, 一个是最小

  我的超时的原因是我算清了每一步都需要加上多少为了取得这个值复杂度加上了O(n) , 所以其实维护的这个区间的作用就是我使得每一次查卡充卡的时候都冲到了d, 同时又记录了如果我不需要充卡的时候会不会超过d, 这样在每次充卡的时候在++ cnt就算出了答案

  代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
using namespace std;const int maxn = 1e5+10;
int a[maxn];
const int INF = 0x3fffffff;
int n, d;int find_index(int s, int id, int & min_value) {int min_d = INF;int ret = -1;for(int i = id+1; i <= n; i++) {s += a[i];if(d-s < min_d) {min_d = d-s;ret = i;}}min_value = min_d;return ret;
}int main() {//freopen("in.txt", "r", stdin);    scanf("%d%d", &n, &d);for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);}int s = 0;for(int i = 1; i <= n; i++) {s += a[i];if(s > d) {printf("-1\n");return 0;}}s = 0;int cnt = 0;int res = 0;while(cnt < n) {int pre_cnt = cnt+1;int min_value;cnt = find_index(s, cnt, min_value);int flag = 1;for(int i = pre_cnt; i <= cnt; i++) {s += a[i];if(a[i] == 0 && s < 0 && flag == 1) {s += min_value; flag = 0;res ++;}if(a[i] == 0 && s < 0 && flag == 0 ) {printf("-1\n");return 0;}}}printf("%d\n", res);            return 0;
}

My TLE code

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
using namespace std;const int maxn = 1e5+10;
int a[maxn];
const int INF = 0x3fffffff;
int n, d;int main() {scanf("%d%d", &n, &d);for(int i = 0; i < n; i++) {scanf("%d", &a[i]);}int flag = 1;int top, bottom;int res = 0;top = bottom = 0;for(int i = 0; i < n; i++) {if(a[i] == 0) {if(top < 0) {res++;top = d;            }bottom = max(0, bottom);}else {top += a[i];bottom += a[i];top = min(top, d);if(bottom > d) {flag = -1;break;}}}if(flag == 1) {printf("%d\n", res);}else {printf("-1\n");}return 0;
}

View Code

  思考: 自己考虑问题还是不够全面 啊, ?

转载于:https://www.cnblogs.com/FriskyPuppy/p/7911345.html

Codeforces 893 D Credit Card 贪心 思维相关推荐

  1. 【CodeForces】893 - D Credit Card (贪心)

    题目链接 题目读了好久.. 题意: 有一个人新办了一张信用卡,初始账户上没有钱.给出两个数字q,d. q是操作次数. 操作分为三种: 当aiaia_i为正值时表示信用卡增加了aiaia_i元. 当ai ...

  2. cf#401(Div. 2)B. Game of Credit Card(田忌赛马类贪心)

    题干: After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle b ...

  3. 1450F The Struggling Contestant(贪心+思维)

    1450F The Struggling Contestant(贪心+思维) Codeforces Global Round 12 F. The Struggling Contestant 题面:Th ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. TensorFlow for Hackers (Part VII) - Credit Card Fraud Detection using Autoencoders in Keras

    It's Sunday morning, it's quiet and you wake up with a big smile on your face. Today is going to be ...

  6. Educational Codeforces Round 24 E. Card Game Again(双指针)

    题目链接:Educational Codeforces Round 24 E. Card Game Again 题意: 给你n个数和一个数k. 现在每次可以拿掉前x个数,后y个数,剩下的数的乘积要能被 ...

  7. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 1 /* 2 题意:n个头,m个士兵,问能否砍掉n个头 3 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 4 */ 5 #include <cstdio> 6 #i ...

  8. Python:实现测试信用卡号码有效性credit card validator的算法(附完整源码)

    Python:实现测试信用卡号码有效性credit card validator的算法 def validate_initial_digits(credit_card_number: str) -&g ...

  9. 论文 | Credit Card Fraud Detection Using Convolutional Neural Networks

    本篇博客继续为大家介绍一篇论文,也是关于用卷积神经网络 CNN 来进行信用卡欺诈检测的. 论文信息 论文题目:Credit card fraud detection using convolution ...

最新文章

  1. Python--数据存储:pickle模块的使用讲解
  2. VC++中使用MFC通过ADO连接数据库
  3. 文本相似度算法(余弦定理)
  4. ux和ui_使用UX设计师为Amazon的Alexa学习会话式UI的基础
  5. 淘宝电子面单怎么用CAINIAO打印组件调打印偏移
  6. 很简单只需五步,实现图片换背景!
  7. 倒立摆的实现 1.前期准备
  8. H5游戏开发:游戏引擎入门推荐
  9. Python添加flac文件标签并实现wav转flac
  10. APP内跳转QQ和陌生人聊天实现客服功能
  11. 为fetch添加拦截器功能
  12. 软件测试从入门到入职,自学规划真的很重要~
  13. MQTT断线重连订阅无法接收
  14. 微信小程序——wxs脚本
  15. 近几年计算机系论文参考文献,近几年计算机学校参考文献 哪里有计算机学校参考文献...
  16. 个人笔记本 | 网络爬虫 | requests爬虫
  17. PDF 阅读与注释的实践——知之阅读、Markdown+Git、Kami、Edge、Mendeley、Zotero
  18. 软件AutoID Network Navigator设置基恩士扫码枪的使用教程
  19. Java程序员应该看的14本Java书籍!
  20. Automatically Labeled Data Generation for Large Scale Event Extraction

热门文章

  1. oracle ctl 递增,增加oracle的控制文件
  2. html5模板区别,网页设计和平面设计理念的六大区别
  3. 制定交叉编译工具_制作交叉编译工具链的方法总结(详细)
  4. java面向对象实现表达式计算,java面向对象课程设计-数学表达式计算器
  5. python文件同时读写_python 同时读取多个文件的例子
  6. mysql merge查询速度_MySQL 查询优化之 Index Merge
  7. README.md怎么写比较好
  8. 如何隐藏地址栏中的真实地址_代理IP如何隐藏真实IP
  9. springboot怎么返回404_自定义SpringBoot REST API 404返回信息
  10. 安卓上传文件时修改文件名称_使用mmv命令批量修改文件名称