Time Limit:1s Memory Limit:1024MByte
Submissions:621Solved:206
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,ai+1,ai+2)变成(ai+ai+1,-(ai+1),ai+2+ai+1)
问是否能将A数组变成B数组。
思路(官方题解写的蛮清晰的):
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100500];
int b[100500];
int suma[100500];
int sumb[100500];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
for(int i=0;i<n;i++)scanf("%d",&b[i]);
for(int i=0;i<n;i++)
{
if(i==0)suma[i]=a[i];
else suma[i]=suma[i-1]+a[i];
}
for(int i=0;i<n;i++)
{
if(i==0)sumb[i]=b[i];
else sumb[i]=sumb[i-1]+b[i];
}
int flag=0;
sort(suma,suma+n);
sort(sumb,sumb+n);
for(int i=0;i<n;i++)
{
if(suma[i]==sumb[i])continue;
else flag=1;
}
if(flag==1)printf("No\n");
else printf("Yes\n");
}
}