Boris and His Amazing Haircut(数据结构 + 贪心)
题目描述:
Boris thinks that chess is a tedious game. So he left his tournament early and went to a barber shop as his hair was a bit messy.
His current hair can be described by an array
The barber has
- Choose any razor which hasn't been used before, let its size be
; - Choose a segment
; - Set
for each ;
Notice that some razors might have equal sizes — the barber can choose some size
He may perform as many operations as he wants, as long as any razor is used at most once and
Can you determine whether the barber can give Boris his desired haircut?
输入描述:
Each test contains multiple test cases. The first line contains the number of test cases
The first line of each test case contains a positive integer
The second line of each test case contains
The third line of each test case contains
The fourth line of each test case contains a positive integer
The fifth line of each test case contains
It is guaranteed that the sum of
输出描述:
For each test case, print "YES" if the barber can cut Boris's hair as desired. Otherwise, print "NO".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
样例:
input:
7
3
3 3 3
2 1 2
2
1 2
6
3 4 4 6 3 4
3 1 2 3 2 3
3
3 2 3
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
3
1 1 1
1 1 2
12
4 2 4 3 1 5 6 3 5 6 2 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
8
1 5 3 5 4 2 3 1
13
7 9 4 5 3 3 3 6 8 10 3 2 5
5 3 1 5 3 2 2 5 8 5 1 1 5
7
1 5 3 4 2 3 1
3
19747843 2736467 938578397
2039844 2039844 2039844
1
2039844
output:
YES
NO
YES
NO
YES
NO
YES
Note:
In the first test case, Boris's hair is initially
- The barber uses the razor with size
on the segment ; hence Boris's hair becomes . - The barber uses the razor with size
on the segment ; hence Boris's hair becomes which is the desired haircut.
In the third test case, no operation has to be done since Boris's hair is already as desired.
In the fourth test case, no cuts will be able to increase the third element in
AC代码:
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int T = 1; int n, m; int a[N]; int b[N]; // 要使a数组全部变成b数组,贪心的想,要用尽可能少的剃刀剃尽可能多的头发 // 这就需要将b数组看成多个区间,区间中相同的数只需要用一次剃刀就可以 // 区间中的数可以都一样,也可以有大有小,但是大的数必须在区间两边 // 否则区间中大的数会被小的数代替掉,就永远也达不到目标 void solve() { scanf("%d", &n); for(int i = 1; i <= n; i ++) scanf("%d", &a[i]); for(int i = 1; i <= n; i ++) scanf("%d", &b[i]); scanf("%d", &m); map<int, int> x; // 用来存剃刀的数量,大小相同的放在一起 for(int i = 1; i <= m; i ++) { int s; scanf("%d", &s); x[s] ++; } stack<int> q; // 单调栈,非严格单调递减地存储区间 bool f = 1; for(int i = 1; i <= n; i ++) { if(a[i] < b[i]) // 剃过之后的头发比不可能比原来大,这种情况直接舍弃 { printf("NO\n"); return; } // 只要新的数小于等于栈顶的数,则可以存入这个区间 // 如:2 1 1 2 // 如:5 3 5 while(q.size() && q.top() < b[i]) q.pop(); // a[i] == b[i]这种情况是不需要用任何剃刀的 // 在a[i] != b[i]时,若栈顶的数等于新的数,就说明栈顶和新的数是同一区间的,就不需要用任何剃刀 // 如上面两例中2 1 1 2 变成2 2,5 3 5 变成 5 5 if((!q.size() || (q.size() && q.top() != b[i])) && a[i] != b[i]) { q.push(b[i]); // 如果需要用剃刀但是剃刀已经被全使用过,则无法达到目的 if(x[b[i]] <= 0) { printf("NO\n"); return; } // 使用过一次后剃刀数量减一 x[b[i]] --; } } printf("YES\n"); } int main() { scanf("%d", &T); while(T --) solve(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话