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 Ai≠Bi .
Now,give you the sequence A,check out it’s good or not.
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 Ai≠Bi .
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
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;
}