Dales and Hills Gym - 101411D

Problem D. Dales and Hills
Input le: dales.in
Output le: dales.out
Time limit: 2 seconds
Memory limit: 256 megabytes
Let‘s consider a number sequence a1; · · · ; aN . We call the continuous subsequence ai
; · · · ; aj ; · · · ; ak
(1 ≤ i < j < k ≤ N) of the sequence a hill if at < at+1 for any i ≤ t < j and at > at+1 for any j ≤ t < k.
In this case we call min{j ? i; k ? j} the height of the hill. Similarly, we call the continuous subsequence
a dale if at > at+1 for any i ≤ t < j and at < at+1 for any j ≤ t < k. In this case we call min{j ?i; k ?j}
the depth of the dale.
Compute the height of the highest hill and the depth of the deepest dale in the given sequence.
Input
The rst line of the input le contains T (1 ≤ T ≤ 100 000), the number of test cases. The test cases
follow, occupying two lines each. The rst of the two lines contains N (1 ≤ N ≤ 1 000 000), the second
the members of the sequence, separated by spaces. The sum of values of N over all test cases in the le
does not exceed 1 000 000. The absolute values of the members of the sequences do not exceed 100 000.
Output
The output le should consist of T lines and each line should contain two integers, the height of the highest
hill and the depth of the deepest dale. If there are no hills or no dales, output 0 in the corresponding
position.
Example
dales.in dales.out
2
10
4 4 1 6 3 2 1 2 5 7
10
2 3 4 5 6 7 8 9 10 9
1 3
1 0

题目大意:给出一个序列,求最高的山和最深的山谷。如果中间没有平的(a[i]==a[j]),那么必然是连续的先减后增或者先增后减。如果中间有平的地方,就重新判断是先增后减还是先减后增。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 1000005
using namespace std;
int a[maxn];
int h1,h2,h,d,d1,d2,mh,md,n,i,j,flag;//h1,h2,是山左右两边的深度,d1,d2是山谷左右两边的深度
void dale()
{
    while(a[j-1]>a[j]&&j<=n) j++;
     d1=j-i-1;
     if(flag)
     {
         h2=j-i-1;
         h=min(h1,h2);
         mh=max(mh,h);
     }
     i=j-1;
     if(a[i]==a[j]) return ;
     while(a[j-1]<a[j]&&j<=n) j++;
         d2=h1=j-i-1;
         i=j-1;
         d=min(d1,d2);
         md=max(md,d);
         flag=1;

}
void hill()
{
    while(a[j-1]<a[j]&&j<=n) j++;
    h1=j-i-1;
    if(flag)
    {
        d2=j-i-1;
        d=min(d1,d2);
        md=max(md,d);
    }
    i=j-1;
    if(a[i]==a[j]) return ;
    while(a[j-1]>a[j]&&j<=n) j++;
    h2=d1=j-i-1;
    i=j-1;
    h=min(h1,h2);
    mh=max(mh,h);
    flag=1;
}
int main()
{
     //freopen("dales.in", "r", stdin);
   // freopen("dales.out", "w", stdout);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int k=1;k<=n;k++)
            scanf("%d",&a[k]);
        h1=h2=h=d1=d2=d=mh=md=0;
        i=1;j=2;
        flag=0;
        while(j<=n)
        {
            if(a[i]==a[j])
            {
                i++;
                j++;
                flag=0;
            }
            if(a[i]>a[j]) dale();
            if(a[i]<a[j]) hill();
        }
        printf("%d %d\n",mh,md);
    }
    return 0;
}
时间: 2024-07-30 16:27:09

Dales and Hills Gym - 101411D的相关文章

Gym 101064 D Black Hills golden jewels (二分)

题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然后二分). 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib&

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

B - Average Gym - 101161B 组合数学

http://codeforces.com/gym/101161/attachments 今天被卡常了,其实是自己对组合数技巧研究的不够. 如果是n, m <= 1e5的,然后取模是质数,那么可以用费马小定理. 如果n, m都比较小,那么其实是直接杨辉三角.不用逆元那些. 这题的思路是,枚举每一一个ave,然后总和就是n * ave 相当于方程  x1 + x2 + .... + xn = n * ave中,在0 <= x[i] <= full的情况下,不同解的个数中,使得x[i] ==

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

CodeForces Gym 101063C 二进制压缩

http://codeforces.com/gym/101063/problem/C 给n个人,m样物品,每个人可以从物品中选择几样.两人选择物品的交集元素个数比上并集元素个数如果大于某个比例即可将两人配对.求配对数. n的范围是1e5,直接比较所有人的选择会TLE,应该将所有选择物品的情况用二进制压缩,m最大是10,情况数目小于2048,可以接受.注意配对总数范围应为long long. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> i

Gym 101246H ``North-East&#39;&#39;(LIS)

http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的点.输出它可能经过的点和一定会经过的点. 思路: 分析一下第一个案例,在坐标图上画出来,可以发现,他最多可以经过4个点,有两种方法可以走. 观察一下,就可以发现这道题目就是要我们求一个LIS. 首先,对输入数据排一下顺序,x小的排前,相等时则将y大的优先排前面. 用二分法求LIS,这样在d数组中就可

Gym 100712I Bahosain and Digits(开关翻转问题)

http://codeforces.com/gym/100712/attachments 题意: 给出一串数字,每次选择连续的k个数字加上任意数(超过10就取余),最后要使得所有数字都相等,求最大的k. 思路: 开关翻转问题. 算法具体可以参考<挑战程序竞赛>常用技巧篇. 这道题目就是在枚举k的同时再枚举一下最后要转换成的数字即可. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring>

很好的脑洞题:dfs+暴力 Gym - 101128A Promotions

http://codeforces.com/gym/101128 题目大意:给你一个a,b,e,p.有e个点,p条有向边,每条边为(x,y),表示x->y,每次我们都取出一个入度为0的,并且一次性取出来的个数为a(或b).当然,取出来的种类可能有很多种(即一个集合),问,这个集合中有多少个数字是相同的. 第一个输出集合长度为a的,第二个输出集合长度为b的,第三个输出无论如何都无法被取出的个数. 思路:建立正向图和反向图. 定义pair<int, int> interval[i] 表示第i

【树状数组】Gym - 101147J - Whistle&#39;s New Car

题意就是对每个点i,统计在其子树内(不含自身),且depj-depi<=xj的点有多少个. 把点分别按照dep-x和dep进行排序,离线处理, 每次把dep-x小于等于当前dep值的点插入树状数组,就变成了询问dfs序在一个区间内的点有多少个,可以用树状数组轻松解决. #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int f,C; void R(int &am