FZU 1914 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的更多相关文章
- FZU 1914 Funny Positive Sequence(线性算法)
这个当时我没有做出来,看了很多人包括学长的代码才懂,我感觉最好的方法还是下面那一种,标记以谁开头的是不行的,我感觉有点不好理解,如果不懂举组样例在纸上写一下就会比较清楚了 #include<io ...
- 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 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
- FZU - 1914
题意略. 思路: 我们应该着重关注负数对当前数列的影响,由于前缀和的性质,我们都是从当前数字向前加,这其实也是在枚举以哪个下标作为开头. 详见代码: #include<stdio.h> # ...
- Program B 暴力求解
Given a sequence of integers S = {S1,S2,...,Sn}, you should determine what is the value of the maxim ...
- UVA 11059
Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the ...
- 最大乘积(Maximum Product,UVA 11059)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- uva11059(最大乘积)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
随机推荐
- apt系统中sources.list文件的解析
/etc/apt/sources.list 一般源信息都存在这个文件中.但众多软件源都放在一个文件中实在有点乱,于是新版ubuntu也有了分类的方法: 文件夹 /etc/apt/sources.li ...
- JavaScript屏蔽Backspace键
原文:http://www.cnblogs.com/xdp-gacl/p/3785806.html 今天在IE浏览器下发现,当把使用readonly="readonly"属性将文本 ...
- D3.js 插入元素,删除元素
插入元素涉及的函数有两个: 一.append():在选择集末尾插入元素 假设有三个段落元素 <p>Apple</p> <p>Pear</p> <p ...
- jquery中html(), text(),val()区别(zhuan)
https://zhidao.baidu.com/question/307317838.html http://www.cnblogs.com/aqbyygyyga/archive/2011/11/0 ...
- tinyXml直接解析XML字符串
一直都用tinyxml直接LoadFile来解析XML,发现原来也可以直接解析XML字符串. XML文件: <?xml version=\"1.0\" encoding=\& ...
- 转!数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)
数据库连接池概念.种类.配置(DBCP\C3P0\JndI与Tomact配置连接池) 一.DBCP 连接:DBCP 连接池是 Apache 软件基金组织下的一个开源连接池实现. 需要的 java 包c ...
- eclipse设置字体、背景(豆绿)色、自动提示
背景色:(护眼豆绿色) window-->preferences-->General-->Editors-->Text Editors-->(最下遍一栏中的)Backgr ...
- git 10.8
git clone xxxx.git生成一个本地的文件夹acd agit checkout -b abcgit checkout mastergit pull然后数据全部由更新 但是是远程的更新 不能 ...
- 关于ipxe启动的几个疑问
关于ipxe启动的几个疑问 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=373026&page=1&extra=#pid ...
- JSP Scripting Element
There are five different types of scripting elements Scripting Element Example Comment <%-- comme ...