Codeforces Round #632 (Div. 2) B. Kind Anton

B. Kind Anton

题目链接-B. Kind Anton
在这里插入图片描述
在这里插入图片描述
题目大意
数组 a n an 由 { 1 , 0 , 1 −1,0,1 } 中的元素构成,并且你可以将数组中的任意元素 a j aj 替换为 a j + a i ( j > i ) aj+ai(j>i) ,问数组 a n an 能否通过变换变成数组 b n bn

解题思路

  • 因为只能用前面的数字来改变后面的,所以从后向前模拟即可
  • 如果 a j < b j aj<bj ,那么必须存在 a i = 1 ( i < j ) ai=1(i<j) ;如果 a j > b j aj>bj ,那么必须存在 a i = 1 ( i < j ) ai=−1(i<j) ,如果不满足条件就输出NO
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
//#pragma GCC diagnostic error "-std=c++11"
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
int a[N],b[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
        map<int,int> mp;
        bool ass=1;
        for(int i=0;i<n;i++){
        	cin>>a[i];
        	mp[a[i]]++;
		}
        for(int i=0;i<n;i++)
        	cin>>b[i];
        for(int i=n-1;i>=0;i--){
            mp[a[i]]--;
            if(b[i]>a[i]&&!mp[1]){
                ass=0;
                break;
            }
            if(b[i]<a[i]&&!mp[-1]){
                ass=0;
                break;
            }
        }
        ass?cout<<"YES"<<endl:cout<<"NO"<<endl;
    }
	return 0;
}
发布了175 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/105418517