A. Dragons(简单贪心)
题目链接:codeforces 230A
题意:
一个人初始力量为 s ,有n条龙, 如果力量大于龙的力量x[i],那么可以打过这条龙并获得这条龙的额外给定的力量y[i],如果可以打过所有龙(可以按任意顺序),输出YES , 否则输出 NO
题解:
肯定是先打力量小的龙,如果力量相同,先打给额外力量多的龙
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1005;
struct node{
int x, y;
}a[maxn];
bool cmp(node k, node u){
if(k.x == u.x){
return k.y > u.y;
}
return k.x < u.x;
}
int main(){
int s, n;
cin >> s >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].x >> a[i].y;
}
sort(a+1, a+1+n, cmp);
bool flag = true;
for(int i = 1; i <= n; i++){
if(s > a[i].x){
s += a[i].y;
}
else{
flag = false;
break;
}
}
if(flag){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
return 0;
}
B.T-primes(质数扩展,思维)
题目链接:codeforces 230B
题意:
找10的12次方内的只有 3 个因子的数
题解:
因为质因子有两个因子,所以找质因子的平方就可以了 ,质因子范围缩小到10的6次方内
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
int e[maxn],a[maxn];
void solve(){
e[1] = 1;
for(int i = 2; i < maxn; i++){
e[i] = i;
}
for(int i = 2;i < maxn;i++){
if(e[i] == i){
for(int j = i; j < maxn; j= j + i){
e[j] = e[j] / i * (i-1);
if(j - e[j] == 1){
a[j] = 1;
}
}
}
}
}
int main(){
int n;
cin >> n;
solve();
for(int i = 1; i <= n; i++){
ll k;
cin >> k;
ll x = sqrt(k);
if(x * x == k && a[x] == 1){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}