Skip to content

Commit d7e64a5

Browse files
committed
add niuke code tk2
1 parent 3fd5a7f commit d7e64a5

5 files changed

Lines changed: 251 additions & 0 deletions

File tree

  • .vs
  • leetcode/Study Plan/3347. 执行操作后元素的最高频率 II
  • niuke/contest/牛客2025秋季算法编程训练联赛2-基础组

.vs/slnx.sqlite

0 Bytes
Binary file not shown.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution:
2+
def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:
3+
# 排序数组,便于后续二分查找和统计连续相同元素
4+
nums.sort()
5+
6+
# 记录最终答案:某个值能达到的最大出现频率
7+
ans = 0
8+
9+
# 统计原数组中每个数字的出现次数
10+
num_count = defaultdict(int)
11+
12+
# 候选模式集合:存储所有可能成为高频目标值的数字
13+
modes = set()
14+
15+
def add_mode(value):
16+
"""
17+
将value及其相关候选值加入modes集合
18+
19+
思路:如果要让某个数字出现次数最多,有三种可能的目标值:
20+
1. value本身
21+
2. value - k:范围内的其他数可以通过 +k 操作变成 value
22+
3. value + k:范围内的其他数可以通过 -k 操作变成 value
23+
"""
24+
modes.add(value)
25+
# 只有当 value-k 在数组范围内时才加入
26+
if value - k >= nums[0]:
27+
modes.add(value - k)
28+
# 只有当 value+k 在数组范围内时才加入
29+
if value + k <= nums[-1]:
30+
modes.add(value + k)
31+
32+
# 记录当前处理的相同数字的起始位置
33+
last_num_index = 0
34+
35+
# 第一次遍历:统计每个不同数字的出现次数
36+
for i in range(len(nums)):
37+
# 当遇到不同的数字时
38+
if nums[i] != nums[last_num_index]:
39+
# 记录前一个数字的出现次数(从last_num_index到i-1)
40+
num_count[nums[last_num_index]] = i - last_num_index
41+
# 更新答案(不操作时的最大频率)
42+
ans = max(ans, i - last_num_index)
43+
# 将该数字及其相关候选值加入modes
44+
add_mode(nums[last_num_index])
45+
# 更新起始位置为当前位置
46+
last_num_index = i
47+
48+
# 处理最后一组相同的数字
49+
num_count[nums[last_num_index]] = len(nums) - last_num_index
50+
ans = max(ans, len(nums) - last_num_index)
51+
add_mode(nums[last_num_index])
52+
53+
# 第二次遍历:对每个候选模式计算能达到的最大频率
54+
for mode in sorted(modes):
55+
# 使用二分查找找到 [mode-k, mode+k] 范围内的所有元素
56+
# l: 第一个 >= mode-k 的元素位置
57+
l = bisect.bisect_left(nums, mode - k)
58+
# r: 最后一个 <= mode+k 的元素位置
59+
r = bisect.bisect_right(nums, mode + k) - 1
60+
61+
# 计算以mode为目标值时能达到的最大频率
62+
if mode in num_count:
63+
# 情况1:mode原本就存在于数组中
64+
# 最大频率 = min(范围内总元素数, 原有数量 + 允许操作次数)
65+
temp_ans = min(r - l + 1, num_count[mode] + numOperations)
66+
else:
67+
# 情况2:mode不在原数组中
68+
# 最大频率 = min(范围内总元素数, 允许操作次数)
69+
# 因为每个变成mode的元素都需要一次操作
70+
temp_ans = min(r - l + 1, numOperations)
71+
72+
# 更新全局最大值
73+
ans = max(ans, temp_ans)
74+
75+
return ans
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
int main()
7+
{
8+
ios::sync_with_stdio(false);
9+
cin.tie(0);
10+
11+
ll a, b, c, x, y, z;
12+
cin >> a >> b >> c >> x >> y >> z;
13+
14+
// 贪心策略:
15+
// 牛牛的石头(a) 对 牛可乐的剪刀(y) -> 赢 min(a, y) 局
16+
// 牛牛的剪刀(b) 对 牛可乐的布(z) -> 赢 min(b, z) 局
17+
// 牛牛的布(c) 对 牛可乐的石头(x) -> 赢 min(c, x) 局
18+
19+
ll win_cnt = min(a, y) + min(b, z) + min(c, x);
20+
21+
cout << win_cnt << endl;
22+
23+
return 0;
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
int main()
7+
{
8+
ios::sync_with_stdio(false);
9+
cin.tie(0);
10+
11+
int n;
12+
string s;
13+
cin >> n >> s;
14+
15+
// 统计字符 '6' 和 '1' 的个数
16+
int cnt6 = 0, cnt1 = 0;
17+
for (char c : s)
18+
{
19+
if (c == '6')
20+
cnt6++;
21+
else if (c == '1')
22+
cnt1++;
23+
}
24+
25+
// 要形成 k 个 "616" 子串,需要 (k+1) 个 '6' 和 k 个 '1'
26+
// 所以:k <= cnt6 - 1 且 k <= cnt1
27+
// 答案:min(cnt6 - 1, cnt1)
28+
29+
int ans = 0;
30+
if (cnt6 >= 2 && cnt1 >= 1)
31+
{
32+
ans = min(cnt6 - 1, cnt1);
33+
}
34+
35+
cout << ans << endl;
36+
37+
return 0;
38+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define ll long long
5+
6+
const ll INF = 2e18; // 溢出判断边界
7+
8+
// 快速幂,带溢出检测
9+
// 返回 a^b,如果溢出返回 INF 或 -INF
10+
ll quick_pow(ll a, ll b)
11+
{
12+
// 特殊情况
13+
if (b == 0)
14+
return 1;
15+
if (a == 0)
16+
return 0;
17+
if (a == 1)
18+
return 1;
19+
if (a == -1)
20+
return (b % 2 == 0) ? 1 : -1;
21+
22+
// 如果指数太大且底数绝对值 >= 2,肯定会溢出
23+
if (b >= 60 && abs(a) >= 2)
24+
return (a > 0) ? INF : ((b % 2 == 0) ? INF : -INF);
25+
26+
ll result = 1;
27+
ll base = a;
28+
29+
while (b > 0)
30+
{
31+
if (b % 2 == 1)
32+
{
33+
// 检测乘法溢出
34+
if (result > 0 && base > 0 && result > INF / base)
35+
return INF;
36+
if (result < 0 && base < 0 && result < INF / base)
37+
return INF;
38+
if (result > 0 && base < 0 && result > -INF / base)
39+
return -INF;
40+
if (result < 0 && base > 0 && result < -INF / base)
41+
return -INF;
42+
43+
result *= base;
44+
45+
// 检测结果是否超出范围
46+
if (abs(result) > INF)
47+
return result > 0 ? INF : -INF;
48+
}
49+
50+
// base = base * base
51+
if (abs(base) > 1e9) // 避免溢出
52+
{
53+
if (b > 1)
54+
return (base > 0) ? INF : ((b % 2 == 0) ? INF : -INF);
55+
}
56+
else
57+
{
58+
base = base * base;
59+
}
60+
61+
b /= 2;
62+
}
63+
64+
return result;
65+
}
66+
67+
int main()
68+
{
69+
ios::sync_with_stdio(false);
70+
cin.tie(0);
71+
72+
int t;
73+
cin >> t;
74+
75+
while (t--)
76+
{
77+
ll a, b, c, d, e, f, g;
78+
cin >> a >> b >> c >> d >> e >> f >> g;
79+
80+
ll ad = quick_pow(a, d);
81+
ll be = quick_pow(b, e);
82+
ll cf = quick_pow(c, f);
83+
84+
// 如果任何一项溢出,检查是否明显不等于 g
85+
if (abs(ad) > INF || abs(be) > INF || abs(cf) > INF)
86+
{
87+
cout << "No" << endl;
88+
continue;
89+
}
90+
91+
// 检测加法溢出
92+
ll sum = ad;
93+
if (sum > INF - be || sum < -INF - be)
94+
{
95+
cout << "No" << endl;
96+
continue;
97+
}
98+
sum += be;
99+
100+
if (sum > INF - cf || sum < -INF - cf)
101+
{
102+
cout << "No" << endl;
103+
continue;
104+
}
105+
sum += cf;
106+
107+
if (sum == g)
108+
cout << "Yes" << endl;
109+
else
110+
cout << "No" << endl;
111+
}
112+
113+
return 0;
114+
}

0 commit comments

Comments
 (0)