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