D:

B. Lipshitz Sequence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

A function is called Lipschitz continuous if there is a real constant K such that the inequality |f(x) - f(y)| ≤ K·|x - y| holds for all . We'll deal with a more... discrete version of this term.

For an array , we define it's Lipschitz constant as follows:

  • if n < 2,
  • if n ≥ 2, over all 1 ≤ i < j ≤ n

In other words, is the smallest non-negative integer such that |h[i] - h[j]| ≤ L·|i - j| holds for all 1 ≤ i, j ≤ n.

You are given an array of size n and q queries of the form [l, r]. For each query, consider the subarray ; determine the sum of Lipschitz constants of all subarrays of .

Input

The first line of the input contains two space-separated integers n and q (2 ≤ n ≤ 100 000 and 1 ≤ q ≤ 100) — the number of elements in array and the number of queries respectively.

The second line contains n space-separated integers ().

The following q lines describe queries. The i-th of those lines contains two space-separated integers li and ri (1 ≤ li < ri ≤ n).

Output

Print the answers to all queries in the order in which they are given in the input. For the i-th query, print one line containing a single integer — the sum of Lipschitz constants of all subarrays of .

Sample test(s)
Input
10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9
Output
17
82
23
210
Input
7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5
Output
2
0
22
59
16
8
Note

In the first query of the first sample, the Lipschitz constants of subarrays of with length at least 2 are:

The answer to the query is their sum.

思路是;虽然题目说了很多定义,但是基本都是把人弄晕的。

一定是相邻的两个点的最值 ,可以对应在二维坐标上YY 一下。

然后由数组a[I]->得到B[I];

之后就变成统计问题了;

给一个数组B;

比如:3,2,5,7,8,4,9

求 所有子数组最大值得和;

你可以对一个I 求出I 左边范围 比如L,A[L].,A[L+1]...A[I]<A[I];(注意是小于)

右边 I R,A[I+1],A[I+2]..A[R]<=A[I](注意大于等于)

符号要注意 如果都是大于等于就会重复算(我这里错了几百遍

写得很丑

 #include<bits/stdc++.h>

 using namespace std;
typedef long long ll; #define N 123456 int a[N],L[N],R[N];
int b[N],n; int dp[N][]; void init()
{
int k=log(n)/log(2.0);
for (int i=;i<=n;i++)
dp[i][]=b[i]; for (int j=;j<=k;j++)
for (int i=;i+(<<(j-))-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
} int rmq(int l,int r)
{
int k=log(r-l+)/log(2.0);
return max(dp[l][k],dp[r-(<<k)+][k]);
} int main()
{
int q;
scanf("%d%d",&n,&q);
for (int i=;i<=n;i++)
scanf("%d",&a[i]); n--;
for (int i=;i<=n;i++)
b[i]=abs(a[i+]-a[i]);
init(); for (int i=;i<=n;i++)
{
int l=,r=i;
L[i]=i;
for (int _=;_<;_++)
{
int mid=(l+r)/;
if (rmq(mid,i-)<b[i])
L[i]=mid,r=mid;
else l=mid+;
} l=i,r=n;
for (int _=;_<;_++)
{
int mid=(l+r)/;
if (rmq(i,mid)<=b[i])
R[i]=mid,l=mid+;
else r=mid;
}
} // for (int i=1;i<=n;i++)
// cout<<L[i]<<" "<<R[i]<<endl; while (q--)
{
int l,r;
scanf("%d%d",&l,&r); ll ans=;
r--;
for (int i=l;i<=r;i++)
// ll tmp=(ll) (i-max(l,L[i])+1)*(min(r,R[i])-i+1);
// cout<<tmp<<endl;
ans+=(ll)(i-max(l,L[i])+)*(min(r,R[i])-i+)*b[i]; cout<<ans<<endl;
// printf("%I64d\n",ans);
} return ;
}

C:

C. The Two Routes
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network — for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.

A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.

You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety — in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.

Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 400, 0 ≤ m ≤ n(n - 1) / 2) — the number of towns and the number of railways respectively.

Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 ≤ u, v ≤ n, u ≠ v).

You may assume that there is at most one railway connecting any two towns.

Output

Output one integer — the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output  - 1.

Sample test(s)
Input
4 2
1 3
3 4
Output
2
Input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
-1
Input
5 5
4 2
3 5
4 5
5 1
1 2
Output
3
Note

In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time.

In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.

太无语了。

没想到,写了一个暴力的BFS 结果TLE了

真心不会读题。。

因为BUS或者 train总有一个 直接连接1-N,所以只要算另一个的最短路就好了O(n^2);

 #include<bits/stdc++.h>
using namespace std; const int inf=<<; bool sb;
queue<int >q;
int dis[];
int mp[][];
int n,m;
int dfs()
{
for(int i=; i<n+; i++)
dis[i]=inf;
q.push();
dis[]=;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=; v<=n; v++)
{
if(sb&&!mp[u][v])
{
if(dis[v]==inf)
{
dis[v]=dis[u]+;
q.push(v);
}
}
else if(!sb&&mp[u][v])
{
if(dis[v]==inf)
{
dis[v]=dis[u]+;
q.push(v);
}
}
}
}
return dis[n];
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int x,y;
for(int i=; i<m; i++)
{
scanf("%d%d",&x,&y);
mp[x][y]=;
mp[y][x]=;
if((x==n&&y==)||(x==&&y==n))
sb=true; }
int t=dfs();
t=max(t,);
if(t==inf)
printf("-1\n");
else printf("%d\n",t); }
}

B:

B. Approximating a Constant Range
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging — but why not make a similar programming contest problem while we're at it?

You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.

A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l ≤ i ≤ r; the range [l, r] is almost constant if M - m ≤ 1.

Find the length of the longest almost constant range.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of data points.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000).

Output

Print a single number — the maximum length of an almost constant range of the given sequence.

Sample test(s)
Input
5
1 2 3 3 2
Output
4
Input
11
5 4 5 5 6 7 8 8 8 7 6
Output
5
Note

In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.

In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].

好像想复杂了

我是对于:for (int i=1;i<==n;i++)

{

二分Mid 值求出(i,mid)最大值-最小值的大小

}

好吧的确想多了、、、

或者随便set搞搞应该可以

 #include<bits/stdc++.h>

 using namespace std;
#define N 500100 int maxl[N][], minl[N][];
int n, m, a[N]; int min(int a, int b)
{
if (a>b) return b; return a;
} int max(int a, int b)
{
if (a>b) return a; return b;
} void S_table()
{
int l = int(log((double)n)/log(2.0));
for (int j=;j<=l;j++)
{
for (int i=; i + ( << (j-) ) - <=n;++i)
{
maxl[i][j] = max(maxl[i][j-], maxl[i + ( << (j-) )][j-]);
minl[i][j] = min(minl[i][j-], minl[i + ( << (j-) )][j-]);
}
}
} int rmq(int l, int r)
{
int k = int(log((double)(r-l+))/log(2.0));
int a1 = max(maxl[l][k], maxl[r - (<<k) + ][k]);
int a2 = min(minl[l][k], minl[r - (<<k) + ][k]);
return a1-a2;
//printf("Max: %d Min: %d\n", a1, a2);
} int main()
{ cin>>n;
for (int i=;i<=n;++i)
{
scanf("%d", &a[i]);
maxl[i][] = minl[i][] = a[i];
}
S_table(); int ans=;
for (int i=;i<=n;i++)
{
int l=i,r=n;
for (int _=;_<;_++)
{
int mid=(l+r)/;
if (rmq(i,mid)<=) l=mid+,ans=max(ans,mid-i+);
else r=mid;
}
//cout<<ans<<endl;
} cout<<ans<<endl;
return ;
}

A:题水

Codeforces Round #333 DIV2的更多相关文章

  1. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  2. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  3. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  4. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  5. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

  6. Codeforces Round #626 Div2 D,E

    比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...

  7. CodeForces Round 192 Div2

    This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...

  8. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  9. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

随机推荐

  1. 数理方程:Laplace变换 & 留数(更新中)

    更新:25 APR 2016 Laplace变换 设函数\(f(t)\)在\(t>0\)时有定义,积分 \(F(s)=\int_0^{+\infty}f(t)e^{-st}dt \qquad ( ...

  2. CSS盒模型简单用法

    1.盒模型 margin:外边距: margin-top /margin-right/margin-bottom/margin-left; 或者 margin:top right bottomleft ...

  3. android sdk manager 无法更新

    1.在C:\Windows\System32\drivers\etc找到Hosts文件用记事本打开,在最末尾添加如下代码,保存关闭: #Google主页203.208.46.146 www.googl ...

  4. Markdown 学习资源

    语法 Mastering Markdown Markdown Cheatsheet pandoc 在线预览工具

  5. 第一个Cocos2d-JS游戏

    我们的编写的第一个Cocos2d-JS程序,命名为HelloJS,从该工程开始学习其它的内容.创建工程我们创建Cocos2d-JS工程可以通过Cocos2d-x提供的命令工具cocos实现,但这种方式 ...

  6. 【学习笔记】【C语言】标识符

    1. 什么是标识符 标识符就是在程序中自定义的一些符号和名称.要跟关键字区分开来:关键字是C语言默认提供的符号,标识符是程序员自定义的 2. 标识符的作用 1) 标识符,从字面上理解就是用来标识某些东 ...

  7. Chrome系列 Failed to load resource: net::ERR_CACHE_MISS

    在IE/FF下没有该错误提示,但在Chrome下命令行出现如下错误信息: Failed to load resource: net::ERR_CACHE_MISS 该问题是Chrome浏览器开发工具的 ...

  8. php面向对象的特性:OOP的封装

    字段的作用域: 1.public 公共的(类外可以访问) 2.private 私有的(只能类内访问) 3.protected 受保护的(类内和子类可以访问,类外无法访问) /*通过公共的方法来访问私有 ...

  9. 10款免费CSS编辑器应对于Linux和Ubuntu

    您是否在使用Linux和Ubuntu的,不知道在哪里可以找到一些优秀且免费的CSS编辑器用于Linux和Ubuntu的?如果你的答案是肯定的,然后停止幻想,开始浏览这个帖子里,我们展示了前10名,并免 ...

  10. 杭电ACM2097--Sky数

    这题思路很简单,把10,12,16进制数都按位相加,然后进行比较即可. http://acm.hdu.edu.cn/showproblem.php?pid=2097 <span style=&q ...