A组
外卖店的优先级:模拟
#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 100010;
int n, m, t;
PII menu[N];
int last[N], score[N];
bool st[N];
int main()
{
cin >> n >> m >> t;
for (int i = 0; i < m; i ++ )
{
scanf("%d%d", &menu[i].x, &menu[i].y);
}
sort(menu, menu + m);
for (int i = 0; i < m; i ++ )
{
int x = menu[i].x, y = menu[i].y;
if (x - last[y] > 1)
{
score[y] -= x - last[y] - 1;
if (score[y] < 0) score[y] = 0;
if (score[y] <= 3) st[y] = false;
}
score[y] += 2;
if (score[y] > 5) st[y] = true;
last[y] = x;
}
int cnt = 0;
for (int i = 1; i <= n; i ++ )
{
if (last[i] != t)
{
score[i] -= t - last[i];
if (score[i] <= 3) st[i] = false;
}
if (st[i])
cnt ++ ;
}
cout << cnt << endl;
return 0;
}
修改数组:并查集
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1100010;
int n;
int p[N];
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n;
for (int i = 1; i < N; i ++ ) p[i] = i;
for (int i = 0; i < n; i ++ )
{
int x;
scanf("%d", &x);
x = find(x);
printf("%d ", x);
p[x] = x + 1;
}
return 0;
}
B组
回文日期:模拟
错误思路:
这道题当时做的时候被坑了好久,原因是当时从下一年开始枚举的,其实要从当年开始枚举,因为每一年回文日期是一个确定的数,所以我们只需要枚举年份就行,然后判断是否回文。还有一个坑的地方就是最后的A和B不能相同,因此需要特判
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
int res, res2;
int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool is_leap(int y, int m)
{
if (m != 2) return false;
return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}
bool check(int y, int m, int d)
{
if (m < 1 || m > 12) return false;
if (d < 1 || (d > months[m] + is_leap(y, m))) return false;
return true;
}
int flip(int x)
{
int res = 0;
while (x)
{
res = res * 10 + x % 10;
x /= 10;
}
return res;
}
int main()
{
int n;
cin >> n;
int y = n / 10000;
for (int i = y; i <= 9999; i ++ )
{
int yy = i, mm = 0, dd = 0;
int t = flip(yy);
dd = t % 100;
mm = t / 100;
t = yy * 10000 + mm * 100 + dd;
if (t <= n) continue;
if (!res && check(yy, mm, dd)) res = yy * 10000 + mm * 100 + dd;
if (mm == dd && dd % 10 != dd / 10 && check(yy, mm, dd))
{
res2 = yy * 10000 + mm * 100 + dd;
break;
}
}
printf("%d\n%d\n", res, res2);
return 0;
}
子串分值和
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
char str[N];
typedef long long LL;
int pos[26];//记录每个字符最后一次出现的位置
int f[N];
int main() {
scanf("%s", str + 1);
int n = strlen(str + 1);
LL ans = 0;
for(int i = 1; i <= n; ++i) {
int t = str[i] - 'a';
f[i] = f[i - 1] + i - pos[t];
pos[t] = i;
ans += f[i];
}
printf("%lld\n", ans);
return 0;
}
C组
最少砝码
// 从前i个砝码中选,重量为j的最少砝码的个数
// 数据范围较大,可能是个数学题
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int k;
cin >> k;
cout << ceil(log(2*k+1)/log(3)) << endl;
}
杨辉三角形:二分+找规律
- 每一次乘积的时候都要判断会不会越界
- 找规律的题多写几行
- 求组合数的代码
- 判边界
- 多测试几个数据
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
int n;
LL C(int a, int b)
{
LL res = 1;
for (int i = a, j = 1; j <= b; i --, j ++ )
{
res = res * i / j;
if (res > n) return res;
}
return res;
}
bool check(int k)
{
LL l = 2 * k, r = max(LL(n), l);
while (l < r)
{
LL mid = l + r >> 1;
if (C(mid, k) >= n) r = mid;
else l = mid + 1;
}
if (C(r, k) != n) return false;
cout << r * (r + 1) / 2 + k + 1 << endl;
return true;
}
int main()
{
cin >> n;
for (int k = 16; ; k -- )
if (check(k))
break;
return 0;
}