# 官方题解|欢乐赛#44 T5
2025-04-09 22:10:10
发布于:浙江
16阅读
0回复
0点赞
T5
思路分析
本题很显然当区间长度是奇数的时候,无法满足条件。当区间长度为偶数的时候,只有整个数组中 和 的出现次数都至少要是 才能满足条件,其中 是区间长度。
代码
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt1, cnt0;//cnt1代表原数组中1的个数,cnt0代表原数组中-1的个数
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++ ){
int x;
cin >> x;
if(x == -1) cnt0 ++;
else cnt1 ++;
}
while(m -- ){//处理m个询问
int l, r;
cin >> l >> r;
int len = r - l + 1;//求原数组长度
if((r - l + 1) & 1) cout << "sad\n";
else{
if(cnt0 >= len / 2 && cnt1 >= len / 2) cout << "happy\n";
else cout << "sad\n";
}
}
return 0;
}
这里空空如也
有帮助,赞一个