ABC 084 - D:https://abc084.contest.atcoder.jp/tasks/abc084_d
题意:
在1~1e5以内,有一种判定合法的方式:就是如果当前数 x 是素数,且 (x + 1) / 2也是素数,那么这个数就是合法的。给你 q 个查询区间,问你每段区间内存在多少个合法的数。
思路:
这题考点就是预处理配合前缀的使用,先枚举1e5以内的素数,然后继续枚举合法的素数并且打上标记。记一个前缀数组cnt[maxx],作用是统计当前数之前有多少个合法的数(包括当前数在内),初始化第一项为0,这是因为第一项是1,1之前加上1本身没有素数,然后cnt[i] += cnt[i - 1],如果当前数 i 被标记过,那就证明当前数本身合法,所以当前 cnt[i] 执行 ++,然后在每段查询区间内,用 R 之前出现的合法数减去 L 之前出现的合法数,如果 L 本身合法,那结果再加1即可。
我的AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx = 1e5;
const int Inf = 1 << 30;
int n;
int l, r;
int p[maxx + 7];
int dd[maxx + 7];
bool vis[maxx + 7];
int cnt[maxx +7];
int ans;
void solve() {
int t = 0;
for(int i = 2; i <= maxx; i++) {
bool flg = 1;
for(int j = 2; j <= sqrt(i); j++) {
if(i % j == 0) {
flg = 0; break;
}
}
if(flg) {
p[++t] = i;
vis[p[t]] = 1;
}
}
int f = 0;
for(int i = 3; i <= maxx; i++) {
if(vis[i]) {
int tmp = (i + 1) / 2;
if(vis[tmp]) dd[++f] = i;
}
}
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= f; i++) vis[dd[i]] = 1;
cnt[1] = 0;
for(int i = 2; i <= maxx; i++) {
cnt[i] += cnt[i - 1];
if(vis[i]) cnt[i]++;
}
}
int main() {
solve();
cin >> n;
while(n--) {
cin >> l >> r;
if(vis[l]) ans = cnt[r] - cnt[l] + 1;
else ans = cnt[r] - cnt[l];
cout << ans << endl;
}
}
ABC 085 - D:https://abc085.contest.atcoder.jp/tasks/abc085_d
题意:
你有 n 把武器,怪兽有 h 滴血,第 i 把武器有两个属性,挥动伤害值 a[i],以及投掷伤害值 b[i]( a[i] <= b[i] )。每把武器的挥动伤害都能任意进行若干次,但是每把武器的投掷伤害,只能进行一次,问,最少几步打死怪兽。
思路:
这题考点就是结构体排序,由于 b[i] 的值较大,且只能进行换一次,那么我就从大往小消耗 b[i],而 a[i] 可以进行无限多次,那么若想步数最少,那就每次挥动伤害都进行最大的a[i]即可。换句话说,提供伤害的是某些 b[i] 和最大的 a[i],再仔细想下,这某些b[i],就是比最大的 a[i] 还要大的b[i],那么我先消耗他们,到了最大的a[i]的这个值的时候,我就不断挥动伤害,直到怪兽死亡,比它小的 b[i] 就不用进行多余的消耗了。那我采取的处理方式是,将 b[n + 1] 这项赋值成 a[i] 的最大值 max0,然后对这 n + 1 项,以 b[i] 降序进行结构体排序,然后,按照上述红色加粗的操作模拟一下就ok啦~
我的AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx = 1e5 + 7;
const int Inf = 1 << 30;
int n, h;
int ans;
struct PP {
int a;
int b;
} p[maxx];
bool cmp(PP x, PP y) { return x.b > y.b; }
int main() {
cin >> n >> h;
int max0 = 0;
for(int i = 1; i <= n; i++) {
cin >> p[i].a >> p[i].b;
max0 = max(max0, p[i].a);
}
p[n + 1].a = 0, p[n + 1].b = max0;
sort(p + 1, p + n + 2, cmp);
for(int i = 1; i <= n + 1; i++) {
if(p[i].b > max0) {
ans++;
h -= p[i].b;
}
else if(p[i].b == max0) {
ans += h / max0;
if(h % max0) ans++;
h -= ans * max0;
}
if(h <= 0) break;
}
cout << ans << endl;
}