codeforce -602B Approximating a Constant Range(暴力)

CF上说是数据结构类型。表示不会。

题意是,找连续的并且任意两个数相差不超过1的最长串。

思路:题中说相邻的两个数相差不超过1;

那么cnt最小为2,cnt赋初值2;由于要相差不超过一,所以每个串的最大值最小值相差不能超过一,

那么从第三个元素开始,如果abs(a[i]-max)<=1&&abs(a[i]-min)<=1,就cnt++,表示该元素能加入上一个串,因为有新数字加入,所以更新max,和minn.

如果不符合的话cnt置为2就以a[i],开始向前找串。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<string.h>
 6 using namespace std;
 7 int a[100005];
 8 int main(void)
 9 {
10     int n,i,k,p,q,j;
11     while(scanf("%d",&n)!=EOF)
12     {
13         for(i=0; i<n; i++)
14         {
15             scanf("%d",&a[i]);
16         }
17         int maxx,minn;
18         maxx=max(a[0],a[1]);
19         minn=min(a[0],a[1]);
20         int cnt=2;
21         int sum=2;
22         int x=maxx,y=minn;
23         for(i=2; i<n; i++)
24         {
25             if(abs(a[i]-maxx)<=1&&abs(a[i]-minn)<=1)
26             {
27                 cnt++;
28                 maxx=max(maxx,a[i]);
29                 minn=min(a[i],minn);
30             }
31             else
32             {
33                 cnt=2;
34                 maxx=max(a[i],a[i-1]);
35                 minn=min(a[i],a[i-1]);
36                 for(j=i-2; j>=0; j--)//从前往后找
37                 {
38                     if(abs(a[j]-maxx)<=1&&abs(a[j]-minn)<=1)
39                     {
40                         cnt++;
41                         maxx=max(maxx,a[j]);
42                         minn=min(minn,a[j]);
43                     }
44                     else break;
45                 }
46             }
47             if(cnt>sum)
48             {
49                 sum=cnt;
50             }
51         }
52         printf("%d\n",sum);
53     }
54     return 0;
55 }
时间: 2024-11-09 20:54:36

codeforce -602B Approximating a Constant Range(暴力)的相关文章

Codeforces 602B Approximating a Constant Range(想法题)

B. Approximating a Constant Range 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 suffi

codeforces 602B Approximating a Constant Range

B. Approximating a Constant Range 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 suffi

cf602B Approximating a Constant Range

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 t

Approximating a Constant Range

原题地址:http://codeforces.com/contest/602/problem/B 题意给定一个序列,满足abs(a[i+1]-a[i])<=1要求找到最长的一个子序列[l,r]满足序列中最大值max和最小值之差小于等于1 题解要求找到题意要求的最长序列,对应的序列有什么特征呢?假如序列起点终点是l,r,那么应该有abs(A[l-1]-A[r])==2所以我们枚举终点,对应的起点可以用一个p数组O(1)维护和查询同时,要求这个序列中A[r]-1和A[r]+1不能同时出现,也可以用p

【Virtual Judge】H - 大概做不来的题 Approximating a Constant Range

Description 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 yif and onl

codeforces#333 div2 B. Approximating a Constant Range

http://codeforces.com/contest/602/problem/B 这道题的两种做法, 滑窗+线段树查询区间最值: #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int

【Virtual Judge】G - 一般水的题2-Approximating a Constant Range

Description 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 o

CF-Approximating a Constant Range

Description 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 o

CodeForces - 602B

题目链接:https://vjudge.net/problem/284704/origin Approximating a Constant Range 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 equi