HDU5146Sequence

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 863    Accepted Submission(s): 456


Problem Description
Today we have a number sequence A includes n elements.
Nero thinks a number sequence A is good only if the sum of its elements with odd index equals to the sum of its elements with even index and this sequence is not a palindrome.
Palindrome means no matter we read the sequence from head to tail or from tail to head,we get the same sequence.
Two sequence A and B are consider different if the length of A is different from the length of B or there exists an index i that  AiBi .
Now,give you the sequence A,check out it’s good or not.
 

Input
The first line contains a single integer T,indicating the number of test cases.
Each test case begins with a line contains an integer n,the length of sequence A.
The next line follows n integers  A1,A2,,An .

[Technical Specification]
1 <= T <= 100
1 <= n <= 1000
0 <=  Ai  <= 1000000
 

Output
For each case output one line,if the sequence is good ,output "Yes",otherwise output "No".
 

Sample Input
  
  
3 7 1 2 3 4 5 6 7 7 1 2 3 5 4 7 6 6 1 2 3 3 2 1
 

Sample Output
  
  
No Yes No
 
题目比较简单,Bestcode 里面的题目,看不懂题目意思的可以去看中文版的:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=557&pid=1001

题目意思:看给出的序列是好的还是坏的;
   如何判断一个数列是好的还是坏的,题目中给的定义是:
该数列的奇数项和偶数项的和不相等,并且该数列不为回文数列(回文数列就是该数列正着看和倒着看是一样的);并且两个序列A和B被认为不同,当且仅当两个序列的长度或者存在某个位置a[i]!=b[i];

给出AC代码:
#include<iostream>
using namespace std;
int a[1000005], b[1000005], c[1000005];
int main()
{
	int t, n;
	int p, q;
	cin >> t;
	while (t--)
	{
		p = 0;
		q = 0;
		cin >> n;
		int sum1, sum2;
		sum1 = 0;
		sum2 = 0;
		for (int i = 1; i <= n; i++)
		{
			if (i % 2 == 1)
			{
				cin >> a[p];
				c[i] = a[p];
				sum1 += a[p++];//将奇数项的数列累加起来
			}
			else if (i % 2 == 0)
			{
				cin >> b[q];
				c[i] = b[q];
				sum2 += b[q++];//将偶数项的数列累加起来;
			}
		}
		int i, j;
		int flag1=1, flag2=0;
		if (p == q)  
		{
			for ( i = p - 1, j = q - 1; i >= 0 && j >= 0; i--, j--) //判断奇数数列和偶数数列存在 a[i]!=b[i];
			{
				if (a[i] != b[j])flag1 = 1;
			}
			if (flag1 != 1)flag1 = 0;
		}
		else flag1 = 1;

		for (i = 1, j = n; i < n && j >= 1; i++, j--)//判断数列是否为回文串;
		{
			//cout << a[i] << "  " << b[j] << endl;
			if (c[i] != c[j])flag2 = 1;
		}
		//cout << "sum1==" << sum1 << "   " << "sum2==" << sum2 << endl;
		if (sum1 == sum2&&flag1&&flag2)cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值