Series 2
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 499 Accepted Submission(s): 110
Problem Description
Let A be an integral series {A
1, A
2, . . . , A
n}.
The zero-order series of A is A itself.
The first-order series of A is {B 1, B 2, . . . , B n-1, where B i = A i+1 - A i.
The ith-order series of A is the first-order series of its (i - 1)th-order series (2<=i<=n - 1).
We say A is monotonic iff A 1<=A 2<=. . . <=A n or A 1>= A 2 >=. . . >= A n.
A is kth-order monotonic iff all ith-order series (0<=i<=k) are monotonic, and (k + 1)th-order are not.
Specially, if the zero-order series of A is not monotonic, then A is named ugly series. If all ith-order (0<=i<=n - 1) series of A are monotonic, then A is a nice series.
Given A, determine whether it’s a ugly series or nice series. If both are not, determine k.
The zero-order series of A is A itself.
The first-order series of A is {B 1, B 2, . . . , B n-1, where B i = A i+1 - A i.
The ith-order series of A is the first-order series of its (i - 1)th-order series (2<=i<=n - 1).
We say A is monotonic iff A 1<=A 2<=. . . <=A n or A 1>= A 2 >=. . . >= A n.
A is kth-order monotonic iff all ith-order series (0<=i<=k) are monotonic, and (k + 1)th-order are not.
Specially, if the zero-order series of A is not monotonic, then A is named ugly series. If all ith-order (0<=i<=n - 1) series of A are monotonic, then A is a nice series.
Given A, determine whether it’s a ugly series or nice series. If both are not, determine k.
Input
The input consists of several test cases. The first line of input gives the number of test cases T (T<=50).
For each test case:
The first line contains a single integer n(1<=n<=10 5), which denotes the length of series A.
The second line consists of n integers, describing A 1, A 2, . . . , A n. (0<=|A i|<=2 60)
For each test case:
The first line contains a single integer n(1<=n<=10 5), which denotes the length of series A.
The second line consists of n integers, describing A 1, A 2, . . . , A n. (0<=|A i|<=2 60)
Output
For each test case, output either ugly series, nice series or a single integer k.
Sample Input
4 3 1 3 2 4 1 4 6 7 4 1 3 4 7 5 -1 0 3 11 29
Sample Output
ugly series nice series 0 nice series
Author
BUPT
Source
题解看这: http://weibo.com/p/1005051809706204/weibo?from=page_100505_home&wvr=5.1&mod=weibomore#3740344145782887
看完题解后发现这道题只要缩点,就不会超时。复杂度为NlogV
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF 1e9
#define maxn 100005
#define maxm 100005+10
const ll mod = 1e10+7;
#define eps 1e-7
#define PI acos(-1.0)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define scan(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d%d",&n,&m)
#define scans(s) scanf("%s",s);
#define ini(a) memset(a,0,sizeof(a))
#define out(n) printf("%d\n",n)
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll a[2][maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
int T;
int n;
cin>>T;
while(T--)
{
scanf("%d",&n);
ini(a);
rep1(i,n) scanf("%I64d",&a[0][i]);
int s = 0,start = 1,end = n;
bool finish = 1;
rep(i,n)
{
while(start < end && a[s][start] == 0) start++; //缩点
start = max(1 ,start-1);
while(end > start && a[s][end] == 0) end--;
end = min(end + 1,n - i);
if(start >= end) break;
int inc = 1, dec = 1;
for(int j = start;j < end; j++)
{
if(a[s][j] > a[s][j+1]) inc = 0;
else if(a[s][j] < a[s][j+1]) dec = 0;
}
finish = inc | dec;
if(!finish)
{
if(i == 0) puts("ugly series");
else printf("%d\n",i - 1);
break;
}
for(int j = start;j < end; j++)
{
a[s^1][j] = a[s][j+1] - a[s][j];
}
s ^= 1;
}
if(finish)
puts("nice series");
}
return 0;
}