题目链接:Funny Positive Sequence

题意:给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个数列的这n种变换里,

A(0): a1,a2,…,an-1,an

A(1): a2,a3,…,an,a1

A(n-2): an-1,an,…,an-3,an-2

A(n-1): an,a1,…,an-2,an-1

有多少是正的数列。

思路:比赛的时候想了很久很久,肯定是要求在一次线性扫描里solve这个两重循环才能解决的问题,最后想到应该是计算负数会影响的项,然后大腿就想出来了。

很巧妙地线性扫描,每次只要向前遍历,看这个负数会影响到哪一项,继续向前找就可以了,有些负数可以跳过的,于是就是一次循环解决了,模拟一下过程就知道了。注意最后,要扫描到temp>0或者直接扫描到i=0就可以了。【ACMer的脑洞可怕....】

#include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 500010
using namespace std; double a[maxn];
int cas = 0; int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
for (int i=0; i<n; ++i) {
scanf("%lf", &a[i]);
} double temp = 0;
bool vis[maxn];
memset(vis, 0, sizeof(vis));
bool flag = true;
int sum = n; for (int i=n-1; i>=0; --i) {
if (a[i]<=0 && flag) flag = false;
if (flag == false) {
temp += a[i];
if (temp <= 0) vis[i] = 1, sum--;
else flag = true, temp = 0;
}
} if (temp <= 0) {
for (int i=n-1; i>=0; --i) {
temp += a[i];
if (temp <= 0 && !vis[i]) {
vis[i] = 1;
sum--;
}
else if (temp > 0) break;
}
} printf("Case %d: %d\n", ++cas, sum);
}
return 0;
}

  

FZU 1914 Funny Positive Sequence的更多相关文章

  1. FZU 1914 Funny Positive Sequence(线性算法)

    这个当时我没有做出来,看了很多人包括学长的代码才懂,我感觉最好的方法还是下面那一种,标记以谁开头的是不行的,我感觉有点不好理解,如果不懂举组样例在纸上写一下就会比较清楚了 #include<io ...

  2. Funny Positive Sequence (思维+前缀)

    There are n integers a 1,a 2,…,a n-1,a n in the sequence A, the sum of these n integers is larger th ...

  3. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

  4. FZU - 1914

    题意略. 思路: 我们应该着重关注负数对当前数列的影响,由于前缀和的性质,我们都是从当前数字向前加,这其实也是在枚举以哪个下标作为开头. 详见代码: #include<stdio.h> # ...

  5. Program B 暴力求解

    Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...

  6. UVA 11059

    Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the ...

  7. 最大乘积(Maximum Product,UVA 11059)

    Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...

  8. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  9. uva11059(最大乘积)

    Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...

随机推荐

  1. AS3 求两条直线的交点

    //粘贴到帧上运行即可 var p1Start:Point = new Point(0,0); var p1End:Point = new Point(50,50); var p2Start:Poin ...

  2. css position 绝对定位和相对定位

    position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距离浏览器左20px,距离浏览 ...

  3. [转]-Gradle使用手册(一):为什么要用Gradle?

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...

  4. Scrum Meeting--Twelve(2015-11-3)

    今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 服务器修改与优化 5h 服务器修改与优化 4h 胡亚坤 客户端数据更新 2h 客户端意见反馈收集 2 ...

  5. 笔记本_thinkpad_e40_FN

    1. 开机时按F10进入bios 然后在 bios 中选择 system configuration,看一下其中的 action keys mode .如果此选项后面为 enable 的话,是不需要按 ...

  6. Python学习笔记13—错误和异常

    常见的异常:

  7. linux之flock函数锁文件

    头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指定 ...

  8. numpy库的常用知识

    为什么有numpy这个库呢?准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2, ...

  9. iOS开发之Xcode 6 国际化

    Xcode6 国际化 (1) 新建一个Single View app模版项目,命名为LocalizationTest 1.建立strings文件,命名为Localization.strings 2.点 ...

  10. golang chan 超时

    golang chan 超时 Posted on 2013-12-24 13:03 oathleo 阅读(4227) 评论(0)  编辑  收藏 package main import (    &q ...