codeforces 230 A (简单贪心) B(质数扩展)

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值