CF - 580A. Kefa and First Steps - dp思维
2017-05-06 14:19
387 查看
1.题目描述:
A. Kefa and First Steps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th
day (1 ≤ i ≤ n) he makes ai money.
Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai.
Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!
Input
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
Examples
input
output
input
output
Note
In the first test the maximum non-decreasing subsegment is the numbers from the third to the
4000
fifth one.
In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
2.题意概述:
给你一组数组,要你最长的连续非递减子串的长度。
3.解题思路:
既然是连续,考虑dp思想,以i结尾最大的连续子串长度就和当前a[i]的值相关。因此记录一下当前递增的值即可。
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
int ans = 0;
int cur = 0, now = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i] >= now)
{
cur++;
ans = max(ans, cur);
}
else
cur = 1;
now = a[i];
}
printf("%d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
A. Kefa and First Steps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th
day (1 ≤ i ≤ n) he makes ai money.
Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence ai.
Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order.
Help Kefa cope with this task!
Input
The first line contains integer n (1 ≤ n ≤ 105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the length of the maximum non-decreasing subsegment of sequence a.
Examples
input
6 2 2 1 3 4 1
output
3
input
3 2 2 9
output
3
Note
In the first test the maximum non-decreasing subsegment is the numbers from the third to the
4000
fifth one.
In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
2.题意概述:
给你一组数组,要你最长的连续非递减子串的长度。
3.解题思路:
既然是连续,考虑dp思想,以i结尾最大的连续子串长度就和当前a[i]的值相关。因此记录一下当前递增的值即可。
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n;
while (~scanf("%d", &n))
{
int ans = 0;
int cur = 0, now = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
if (a[i] >= now)
{
cur++;
ans = max(ans, cur);
}
else
cur = 1;
now = a[i];
}
printf("%d\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
相关文章推荐
- Codeforces Round #321 (Div. 2) 580A. Kefa and First Steps
- Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟
- Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~
- Kefa and First Steps
- Codeforces Kefa and First Steps
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps
- Codeforces-580A-Kefa and First Steps
- A. Kefa and First Steps
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps 水题
- Codeforces Round #321 A Kefa and First Steps
- codeforces 580A(Kefa and First Steps) Java
- Code Forces 580 A. Kefa and First Steps(水~)
- Codeforces 580A - Kefa and First Steps
- codeforces水题100道 第十四题 Codeforces Round #321 (Div. 2) A. Kefa and First Steps (brute force)
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps
- 606第三周周六赛B - Kefa and First Steps
- Python database objects-- dbhash.first() and etc.
- Top 10 steps to optimize data access in SQL Server: Part III (Apply advanced indexing and denormalization)
- 【CF】 467C George and Job DP
- Being a Web App: attributes and listeners(Head First Servlets and JSP)