Codeforces Round #333 DIV2
D:
1 second
256 megabytes
standard input
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
.
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).
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
.
10 4
1 5 2 9 1 3 4 2 1 7
2 4
3 8
7 10
1 9
17
82
23
210
7 6
5 7 7 4 6 6 2
1 2
2 3
2 6
1 7
4 7
3 5
2
0
22
59
16
8
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:
2 seconds
256 megabytes
standard input
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.
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 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.
4 2
1 3
3 4
2
4 6
1 2
1 3
1 4
2 3
2 4
3 4
-1
5 5
4 2
3 5
4 5
5 1
1 2
3
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:
2 seconds
256 megabytes
standard input
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.
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).
Print a single number — the maximum length of an almost constant range of the given sequence.
5
1 2 3 3 2
4
11
5 4 5 5 6 7 8 8 8 7 6
5
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的更多相关文章
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
- CodeForces Round 192 Div2
This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...
- 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 ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
随机推荐
- CS异步下载
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- asp.net mvc @RenderBody()的问题
在使用.net mvc 母版页布局时如果是进行上中下三块布局的话,那么就会像下面的图那样: 在上面的div 和下面的div之间会出现4cm的间隔, 解决如下: 给包裹@RenderBody()的div ...
- OSPF系列
实验一.点对点链路上的OSPF 拓扑图 1. 首先配置好路由器R1接口地址和回环地址 2. 配置路由器R2的接口地址和回环地址 3. 配置路由器R3的接口地址和环回地址 4. 配置R1的OSPF协议 ...
- Today’s dictation
A united nations expert on human rights in north korea has warned that the country's dictator, kim j ...
- AMQ学习笔记 - 12. Spring-JmsTemplate特性设置
概述 这是关于JmsTemplate的最后一篇总结,且只会介绍几个比较重要的特性. 消息的递送模式 在发送消息给时,可以告知这是持久化的消息,还是非持久化的消息.如果是非持久化的消息,broker会将 ...
- linux 环境变量【转】
1.引言 在 linux系统 下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现" command not found "的提示内容.如果每次都到安装目标文件夹内,找到可 ...
- java中的生产者和消费者的问题
1----使用Java.util.concurrent.locks包中的lock接口取代synchronized,通过ReentrantLock这个已经实现Lock接口的类, 创建ReentrantL ...
- C++ 11 之初始化
1.4中不同初始化的形式 a.string s("zhl").int i(3); //括号初始化 b.string s="zhl".int ...
- 不用Unity库,利用.NET动态代理自己实现AOP
AOP意为面向切面的程序设计,主要表现为对不同的代码逻辑进行隔离,从而降低不同业务逻辑之间的耦合性,AOP又理解为“横切”,可以在不改变原有实现的情况下,对代码进行拦截和扩展,如果原有设计像一个瓶子, ...
- UIKit,Core Data , Core Graphics, Core Animation,和OpenGLES框架
iOS的主要框架介绍 框架是一个目录,这个目录包含了共享库,访问共享库里代码的头文件,和其它的图片和声音的资源文件.一个共享库定义的方法或函数可以被应用程序调用. IOS提供了很多你可以在应用程序 ...


