标签:
Description
Input
Output
Sample Input
5 4 1 0 0 1 1 0 1 1 1 1 4 4 1 0 0 1 1 0 0 2 2 1 4 4 2 1 0 0 0 0 0 0 1 1 3 3 4 4 2 1 0 0 0 0 0 0 1 3 4 1 3 5 2 1 1 2 2 0 2 2 1 1 0 1 3 2 4
Sample Output
No No Yes No Yes
贪心的策略是让A数组中的数更接近正确位置,正确位置是A数组中的数在B数组中出现的第一个未被标记的位置,让数接近正确位置的贪心就是对于每个区间sort。
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int a[maxn], b[maxn]; int id[maxn]; int main() { int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); memset(id, 0, sizeof(id)); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for(int i = 1; i <= n; i++) { scanf("%d", &b[i]); for(int j = 1; j <= n; j++) { if(!id[j] && a[j] == b[i]) { id[j] = i; break; } } } for(int i = 1; i <= m; i++) { int l, r; scanf("%d %d", &l, &r); sort(id + l, id + r + 1); } int flag = 1; for(int i = 1; i <= n; i++) { if(id[i] != i) { flag = 0; break; } } if(flag) puts("Yes"); else puts("No"); } }
标签:
原文地址:http://www.cnblogs.com/lonewanderer/p/5767279.html