题目链接:点击打开链接
Time Limit:1s Memory Limit:1024MByte
Submissions:597Solved:191
In a mysterious cave, PigVan ( Mr.Van's pet ) has lived for thousands years. PigVan absorbed the power of nature, and it may pretend a human in speaking, walking and so on.
One day, he bought some valuable stone, and divided them into n piles of stone where the ith pile (1≤i≤n) contains values ai .
After PigVan put them in a line, he wants to play a game.
In the boring game, he can do this operation:
Choose a stone pile ai (i>1)and its two adjacent piles ai-1, ai+1, turn (ai-1, ai, ai+1) to (ai-1 + ai, -ai, ai + ai+1).
PigVan wonders whether he can get (b1, b2, b3, …, bn) after several operations.
Note:
If you choose the last pile an, the operation will be ( an-1 + an, -an ) .
For each test case:
In the first line, there are only one integer nn(n≤10 5), indicating the number of food piles.
The second line is nn integers indicate sequence aa ( | a i | ≤ 10 6).
The third line is nn integers indicate sequence bb ( | b i | ≤ 10 6).
思路:
考虑一次操作
(ai-1, ai, ai+1)->(ai-1 + ai, -ai, ai+1 + ai)
如果考虑前缀和,那么一次操作等效为
(si-1, si, si+1)->(si, si-1, si+1)
即对于前缀和而言,他只是交换了位置。因此,我们只需求出前缀和,然后看元素是否对等就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int sum1[1000010],sum2[1000010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
sum1[i]=sum1[i-1]+x;
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
sum2[i]=sum2[i-1]+x;
}
sort(sum1+1,sum1+n+1);
sort(sum2+1,sum2+n+1);
bool flag=1;
for(int i=1;i<=n;i++)
{
if(sum1[i]!=sum2[i])
{
flag=0;
break;
}
}
if(flag)
puts("Yes");
else
puts("No");
}
return 0;
}