A.Marathon (800)
题 意 : 题意: 题意:
比Timur跑得远的有多少人
思 路 : 思路: 思路:
枚举
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int a, b, c, d;
cin >> a >> b >> c >> d;
int num = 0;
if (b > a)
num ++ ;
if (c > a)
num ++ ;
if (d > a)
num ++ ;
cout << num << "\n";
}
return 0;
}
B.All Distinct (800)
题 意 : 题意: 题意:
每次移除两个元素,最终所含不重复元素的数组最长值
思 路 : 思路: 思路:
将出现的元素计数,根据元素的数量分为奇偶,若出现次数偶数的元素有偶数个,那么答案为奇数数量 + 偶数数量,否则为奇数数量 + 偶数数量 - 1
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int n;
cin >> n;
vector<int> a(N, 0);
for (int i = 0; i < n; i ++ ) {
int x;
cin >> x;
a[x] ++ ;
}
int odd = 0, even = 0;
for (int i = 1; i < N; i ++ )
if (a[i] && a[i] & 1)
odd ++ ;
else if (a[i])
even ++ ;
cout << odd + (even & 1 ? even - 1 : even) << "\n";
}
return 0;
}
C.Where’s the Bishop? (800)
题 意 : 题意: 题意:
找到对角线的中心点位置
思 路 : 思路: 思路:
暴力枚举,找到左上、右上、左下、右下都有#的元素位置
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
char g[10][10];
int dx[4] = {-1, -1, 1, 1}, dy[4] = {-1, 1, 1, -1};
bool check(int x, int y) {
for (int i = 0; i < 4; i ++ ) {
if (g[x + dx[i]][y + dy[i]] != '#')
return false;
}
return true;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
for (int i = 0; i < 8; i ++ )
cin >> g[i];
for (int i = 1; i <= 6; i ++ )
for (int j = 1; j <= 6; j ++ )
if (check(i, j)) {
cout << i + 1 << " " << j + 1 << "\n";
break;
}
}
return 0;
}
D.The Clock (1100)
题 意 : 题意: 题意:
在一个循环内,是回文子串形式的时间点出现次数
思 路 : 思路: 思路:
先找到x与1440的最大公因数t,然后将初始时间%t,在1440内每次时间+t,并计数
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
string s;
cin >> s;
int x;
cin >> x;
x = __gcd(x, 1440);
int t = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3));
t %= x;
int ans = 0;
for (int i = t; i < 1440; i += x ) {
int a = i / 600;
int b = i / 60 % 10;
int c = i / 10 % 6;
int d = i % 10;
if (a == d && b == c)
ans ++ ;
}
cout << ans << "\n";
}
return 0;
}
E.Binary Deque (1200)
题 意 : 题意: 题意:
每次可以移除左端点或右端点,花费1,求使得数组内元素的和为s的最小花费
思 路 : 思路: 思路:
从头遍历数组,直到累积和为s,然后更新花费,并更新满足累计和为s的左右端点,更新花费
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int n, s, sum = 0;
cin >> n >> s;
vector<int> a(n);
for (int &ai : a)
cin >> ai;
int ans = n + 1, cur = 0;
for (int i = 0, j = 0; i < n; i ++ ) {
while (j < n && cur + a[j] <= s)
cur += a[j], j ++ ;
if (cur == s)
ans = min(ans, n - (j - i));
cur -= a[i];
}
if (ans > n)
ans = -1;
cout << ans << "\n";
}
return 0;
}
F.3SUM (1300)
题 意 : 题意: 题意:
从数组选三个数,累计和最后一位为3
思 路 : 思路: 思路:
计算0~9每个数出现的次数,暴力枚举累计和出现的可能
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int n;
cin >> n;
vector<int> a(10, 0);
for (int i = 0; i < n; i ++ ) {
int x;
cin >> x;
a[x % 10] ++ ;
}
bool f = false;
for (int i = 0; i < 10; i ++ )
if (a[i])
for (int j = 0; j < 10; j ++ )
if (a[j])
for (int k = 0; k < 10; k ++ )
if (a[k])
if ((i + j + k) % 10 == 3) {
a[i] -- , a[j] -- , a[k] -- ;
if (a[i] >= 0 && a[j] >= 0 && a[k] >= 0)
f = true;
a[i] ++ , a[j] ++, a[k] ++ ;
}
cout << (f ? "YES" : "NO") << "\n";
}
return 0;
}
G.2^Sort (1400)
题 意 : 题意: 题意:
从下标为 i i i开始,满足 2 0 2^0 20 a i a_{i} ai < 2 1 2^1 21 a i + 1 a_{i+1} ai+1<…< 2 k 2^k 2k* a i + k a_{i+k} ai+k (i = 1, 2, …)格式的数量
思 路 : 思路: 思路:
经过观察,每次只需要维护相邻两个元素的大小关系,双指针,当长度满足的情况下就一直计数,否则从头开始计算长度
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &ai : a)
cin >> ai;
int ans = 0;
for (int i = 0; i < n - k; i ++) {
int t = a[i], tem = k, j = i + 1;
while (t < 2 * a[j] && j < n) {
t = a[j], j ++ ;
tem -- ;
ans += (tem <= 0);
}
i = j - 1;
}
cout << ans << "\n";
}
return 0;
}
H.Gambling (1700)
题 意 : 题意: 题意:
找出一个区间内的众数
思 路 : 思路: 思路:
使用map记录每个出现元素的次数和位置,然后假设区间左端点下标为i,右端点下标为j,那么 n u m = j − i + 1 + ( a j − a i + 1 − ( j − i − 1 ) ) num = j - i + 1 + (a_{j} - a_{i} + 1 - (j - i - 1)) num=j−i+1+(aj−ai+1−(j−i−1)),归纳后为 n u m = 2 ∗ j + a j − 2 ∗ i − a i + 1 num = 2 * j + a_{j} - 2 * i - a_{i} + 1 num=2∗j+aj−2∗i−ai+1,由于左端点固定,所以只考虑j,即只考虑 2 ∗ j + a j 2*j+a_{j} 2∗j+aj,使它变大
C o d e Code Code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, M = N << 1, INF = 0x3f3f3f3f;
typedef long long LL;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int _;
for (cin >> _; _ ; _ -- ) {
int n;
cin >> n;
map<int, vector<int>> p;
vector<int> x(n);
for (int i = 0; i < n; i ++ ) {
cin >> x[i];
p[x[i]].push_back(i);
}
int a = x[0], l = 0, r = 1, ans = 1;
for (auto pi : p) {
int b = pi.first;
vector<int> q = pi.second;
int min = 0, k = q[0];
for (int j = 0; j < int(q.size()); j ++ ) {
int i = q[j];
int cur = j - (i - j);
if (cur < min) { // num变负数,不符最优,更换左端点
min = cur;
k = i;
}
int res = cur + 1 - min;
if (res > ans) {
ans = res;
a = b;
l = k;
r = i + 1;
}
}
}
cout << a << " " << l + 1 << " " << r << "\n";
}
return 0;
}