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}, ...
随机推荐
- 图--DFS求连通块
The GeoSurvComp geologic survey company is responsible for detecting u ...
- Android ActionBar以及menu的代码设置样式
menu部分xml代码 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android= ...
- iOS - UIViewController
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder <NSCoding, UIAppearanceC ...
- linux虚拟机安装
1.真实机第一次安装必须先搞f2进入boot从光盘启动,虚拟机不用 进入的时候五个选项Install or upgrade an existing system:安装或升级现有系统Install sy ...
- bootstrap学习笔记<九>(菜单,按钮。导航基本元素)
有了bootstrap作导航不再麻烦,几个样式,几个标签就能轻松搞定. 下面就来分解学习导航条的制作. 一.首先是下拉菜单 <div class="dropdown"> ...
- django连接mysql自动同步生成数据表
python manage.py makemigrations python manage.py migrate 如果是 Django 不主动提示创建管理员(Django 1.9不提示)用下面的命令创 ...
- Oracle正则表达式函数:regexp_like、regexp_substr、regexp_instr、regexp_replace
Oracle正则表达式函数:regexp_like.regexp_substr.regexp_instr.regexp_replace --去掉所有特殊字符,只剩字母 SELECT REGEXP ...
- 怎么进入BAT的研发部门?
怎么进入BAT的研发部门? ======================================剑指offer+leetcode+geeksforgeeks+编程之美+算法导论====秒杀BA ...
- String.join()方法的使用
String.join()方法是JDK1.8之后新增的一个静态方法,使用方式如下所示: String result = String.join("-","java&qu ...
- Subversion中文手册(svnbook) TortoiseSVN中文帮助手册
文档地址 http://svndoc.iusesvn.com/