CF1187D Subarray Sorting

思路:

线段树好题。对a数组中的每个元素从左到右依次操作,判断最终是否能够转化成b数组。在此过程中使用线段树维护区间最小值判断是否能够进行合法操作。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 300005, INF = 0x3f3f3f3f;
 4 deque<int> d[N];
 5 int a[N], b[N], tree[N * 4];
 6
 7 void build(int num, int l, int r)
 8 {
 9     if (l == r) { tree[num] = a[l]; return; }
10     int m = l + r >> 1;
11     build(num << 1, l, m);
12     build(num << 1 | 1, m + 1, r);
13     tree[num] = min(tree[num << 1], tree[num << 1 | 1]);
14 }
15
16 void update(int num, int l, int r, int x, int y)
17 {
18     if (l == r) { tree[num] = y; return; }
19     int m = l + r >> 1;
20     if (x <= m) update(num << 1, l, m, x, y);
21     else update(num << 1 | 1, m + 1, r, x, y);
22     tree[num] = min(tree[num << 1], tree[num << 1 | 1]);
23 }
24
25 int query(int num, int l, int r, int x, int y)
26 {
27     if (x <= l && y >= r) return tree[num];
28     int m = l + r >> 1;
29     int ans = INF;
30     if (x <= m) ans = min(ans, query(num << 1, l, m, x, y));
31     if (y >= m + 1) ans = min(ans, query(num << 1 | 1, m + 1, r, x, y));
32     return ans;
33 }
34
35 int main()
36 {
37     int t, n; cin >> t;
38     while (t--)
39     {
40         cin >> n;
41         for (int i = 1; i <= n; i++) d[i].clear();
42         for (int i = 1; i <= n; i++) { cin >> a[i]; d[a[i]].push_back(i); }
43         build(1, 1, n);
44         for (int i = 1; i <= n; i++) cin >> b[i];
45         bool flg = true;
46         int i = 1, j = 1;
47         while (i <= n && j <= n)
48         {
49             while (i <= n && a[i] == INF) i++;
50             if (a[i] == b[j]) { d[a[i]].pop_front(); i++; j++; }
51             else if (a[i] < b[j]) { flg = false; break; }
52             else
53             {
54                 if (d[b[j]].empty()) { flg = false; break; }
55                 else
56                 {
57                     int t = d[b[j]].front();
58                     int minn = query(1, 1, n, i, t);
59                     if (minn < a[t]) { flg = false; break; }
60                     else
61                     {
62                         update(1, 1, n, t, INF); a[t] = INF;
63                         d[b[j]].pop_front();
64                         j++;
65                     }
66                 }
67             }
68         }
69         cout << (flg ? "YES" : "NO") << endl;
70     }
71     return 0;
72 }

原文地址:https://www.cnblogs.com/wangyiming/p/11125790.html

时间: 2024-10-10 04:29:01

CF1187D Subarray Sorting的相关文章

D. Subarray Sorting

D. Subarray Sorting 题意:给两个长度为n的数字串,a,b每次可以给a串任意长度区间按非递减排序,问a串是否能变为b串. 思路:一开始写的时候,贪心思路错=.=  看了看别人贪心的思路然后过了,做法是依次枚举b串上的每个数字,然后在a串上依次寻找里b串最近的位置k,然后判断在a串中[1,k]这个区间中,这个数字是不是最小值,如果是ok,否则no,在a中判断成立的数字要去掉.(这个贪心emm,总感觉哪里怪怪的,没法证明) 搞定贪心的思路就好做了,给a串建个线段树,用来查找[1,k

Educational Codeforces Round 67 D. Subarray Sorting

Educational Codeforces Round 67 D. Subarray Sorting 传送门 题意: 给出两个数组\(a,b\),现在可以对\(a\)数组进行任意次排序,问最后能否得到\(b\)数组. \(n\leq 3*10^5,a\leq n.\) 思路: 首先注意到任意次排序可以等价于任意次交换两个相邻的数,当且仅当前一个数不小于后面一个数. 我一开始想的是按权值从小到大来构造,但最终发现这条路走不通. 正解就是比较直接的思路,按位置一个一个来匹配. 对于一个\(b_i\

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted

Given an unsorted array arr[0..n-1] of size n, find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted.Examples:1) If the input array is [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60], your program should

Codeforces Educational Codeforces Round 67

目录 Contest Info Solutions A. Stickers and Toys B. Letters Shop C. Vasya And Array D. Subarray Sorting E. Tree Painting Contest Info Data:2019.6.30 Solved:4/7 Solutions A. Stickers and Toys 题意: 有\(A\)物品\(s\)个,\(B\)物品\(t\)个,现在将这些物品装到\(n\)个箱子里,每个箱子只有一下三

LeetCode-152 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 思路:使用动态规划. 设max[i]是以num[i]为结尾元素的子数组的最大乘积:

[LeetCode]Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的. 如

【Divide and Conquer】53.Maximum Subarray(easy)

#week2# #from leetcode# Description Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil