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}, ...
随机推荐
- 详解C#委托,事件与回调函数
.Net编程中最经常用的元素,事件必然是其中之一.无论在ASP.NET还是WINFrom开发中,窗体加载(Load),绘制(Paint),初始化(Init)等等.“protected void Pag ...
- 数据库mysql中having 和where的区别
having的用法 having字句可以让我们筛选成组后的各种数据,where字句在聚合前先筛选记录,也就是说作用在group by和having字句前.而 having子句在聚合后对组记录进行筛选. ...
- Nginx基础知识之——配置文件信息(检查配置文件是否正确)
一.检查配置文件是否正确: /usr/local/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 检查结果: nginx: [emerg] ngin ...
- linux gksu用法
apt-get install gksu gksu是linux下图形化的su/sudo工具 sudo 用来执行命令行(CLI)程序 gksu 用来执行图形的(GUI)程序 GUI = Graphica ...
- golang代码执行顺序
一:首先man.go,整个程序的入口 func main() { beego.Run() } 然后beego.run()代码 // Run beego application. // beego.Ru ...
- HBase的shell命令行界面按退格键(Backspace)无法删除问题
在HBase的shell命令行界面输入错误项按"退格键"删除,却怎么也删除不了: 解决办法: 第一步,修改SecureCRT的设置参数: 第二步,按"Ctrl+退格键(B ...
- 利用ITextSharp导出PDF文件
最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...
- python的模式匹配 - 正则表达式
之前学过perl,深知perl在模式匹配方面的强大,现在想完全转到python,虽说python的模式匹配没有perl那么强大,但还是可以用的,只是需要一个熟悉的过程. 参考资料: Python正则表 ...
- 【转】 SIFT算法详解
尺度不变特征变换匹配算法详解Scale Invariant Feature Transform(SIFT)Just For Fun zdd zddmail@gmail.com 对于初学者,从Davi ...
- 【poj2728】Desert King
[poj2728]Desert King 题意 最优比率生成树. http://blog.csdn.net/ophunter_lcm/article/details/10113817 分析 Dinke ...