hdu 4928 Series 2

本文探讨了序列的单调性质,并介绍了如何通过序列的各阶子序列来判断序列是否为丑陋序列或美好序列,同时给出了计算最优k值的方法。详细解释了序列单调性的定义和k阶单调性的概念,提供了输入输出示例和算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
 

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)
 

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值