U4858 sum

题目提供者666sb666

最新讨论

题目背景

定义一个序列的价值为序列中相邻元素差的绝对值之和。

如序列{2,1,3}的价值为|2-1|+|1-3|=3,而序列{4}的价值为0。

题目描述

现对于一给定序列,求价值最大的子序列的数量。

保证原序列中相邻的两个数不同。

注意:子序列不用连续

输入输出格式

输入格式:

第一行一个正整数n,表示序列中元素的个数。

接下来n行,每行一个数表示序列中的一个元素。

输出格式:

一个数表示数量。答案对1000000007取模。

输入输出样例

输入样例#1:

3

1

2

3

输出样例#1:

2

说明

样例解释:

40%:n<=1000

100%:n<=100000

数组中的元素的范围在int内

/*
恩想到正解了.
恩想的太多+码力太差.
W到挺.
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
#define mod 1000000007
#define LL long long
using namespace std;
LL s[MAXN],n,tot,a[MAXN],ans=1,t;
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
LL ab(int x,int y)
{
if(x-y<=0) return y-x;
return x-y;
}
LL mi(LL a,LL b)
{
LL tot=1;
while(b)
{
if(b&1) tot=(tot*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return tot;
}
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=2;i<=n;i++)
{
if(a[i]<a[i-1]) t++;
else if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
}
if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
for(int i=2;i<=n;i++)
{
if(a[i]>a[i-1]) t++;
else if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
}
if(t>1) ans=(ans*mi(2,t-1))%mod;
cout<<ans;
return 0;
}
/*
正解还是比较好想的.
观察一下有的数是没有贡献的.
比如
1 2 3 4 5 4 3 2 1.
ans就是|5-1|+|1-5|.
那么中间的数可选可不选.
用乘法原理就可以了.
*/
#include<iostream>
#include<cstdio>
#define MAXN 100001
#define mod 1000000007
#define LL long long
using namespace std;
LL s[MAXN],n,tot,a[MAXN],ans=1,t;
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
LL ab(int x,int y)
{
if(x-y<=0) return y-x;
return x-y;
}
LL mi(LL a,LL b)
{
LL tot=1;
while(b)
{
if(b&1) tot=(tot*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return tot;
}
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read();
for(int i=2;i<=n;i++)
{
if(a[i]<a[i-1]) t++;
else if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
}
if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
for(int i=2;i<=n;i++)
{
if(a[i]>a[i-1]) t++;
else if(t>1) ans=(ans*mi(2,t-1))%mod,t=0;
}
if(t>1) ans=(ans*mi(2,t-1))%mod;
cout<<ans;
return 0;
}

最新文章

  1. 谢欣伦 - OpenDev原创例程 - 网络摄像机WebCamera
  2. jQuery实现无限加载瀑布流特效
  3. oracle给字段添加描述
  4. super.onCreate(SavedInstanceState);
  5. c# 扩展方法奇思妙用
  6. github入门
  7. memcached搭建缓存系统
  8. sun.misc.BASE64Encoder和sun.misc.BASE64Encoder 找不到解决的方法
  9. js构造函数式编程
  10. 修改首页的main里面的内容
  11. web前端的学习误区
  12. 在美国,一名 Uber 司机能赚多少?
  13. Surround the Trees(凸包求周长)
  14. 3.linux常用软件的安装方法
  15. 任务调度--spring下的任务调度quartz
  16. 如何调用别人提供的webservice接口
  17. 27.用webpack自搭react和vue框架
  18. int bool 字符串 列表 字典 集合
  19. 《剑指offer》第五十题(字符流中第一个只出现一次的字符)
  20. WPF系列学习

热门文章

  1. paramiko-ssh-sftp实例
  2. ueditor 编译出错
  3. 大数据学习(2)- export、source(附带多个服务器一起启动服务器)
  4. 题解 P2879 【[USACO07JAN]区间统计Tallest Cow】
  5. Boost Graph Library materials
  6. JS中的SRC
  7. vue 编译警告 Compiled with 4 warnings
  8. AL11新建目录、删除目录
  9. TCP/IP网络知识
  10. 第三篇.python编辑器和集成环境01