牛客练习赛34D(数字处理)

题目:

旅行到K国的小w发现K国有着很多物美价廉的商品,他想要买一些商品。

结果一掏钱包,包里只剩下n张K国的纸币了,说起来也奇怪,K国纸币并不像其他国家一样都是1元,5元,10元…而是各种奇怪的面值,所以找零就不是很方便。

已知商店里的商品价格都是小于等于m的正整数,如果有可能存在某个商品的价格为x<=m并且x无法在不找零的情况下支付,小w就不能任意购买一件商店中的商品,小w想知道自己在不找零的情况下能否任意购买一件商店中的商品,你能帮帮他么?

这道题又考验了自己对数字的认识,很惭愧,还是没有想到。。只是考虑到了前面的要加等于后面不存在的数,否则就要出错,但是没有想出要如何处理。很菜,遗憾啊;

后来知道了写的思想,但是还没有写对,自己的写法的问题,使得一部分没有考虑,就是如果给的m已经满足了。但是还可以加,但是这时候不符合你给的判断条件就出现了,错误,只过了%88。看了其他人的代码。才知道代码怎么写最好。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pi acos(-1)
#define e exp(1)
#define For(i, a, b) for(int (i) = (a); (i) <= (b); (i) ++)
#define Bor(i, a, b) for(int (i) = (b); (i) >= (a); (i) --)
#define max(a,b) (((a)>(b))?(a):(b))
#define min(a,b) (((a)<(b))?(a):(b))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define eps 1e-7
#define INF 0x3f3f3f3f
#define inf -2100000000
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 1000 + 100;
const double EPS = 1e-10;
const ll p = 1e7+9;
const ll mod = 1e9+7;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
inline int read(){
    int ret=0,f=0;char ch=getchar();
    while(ch>'9'||ch<'0') f^=ch=='-',ch=getchar();
    while(ch<='9'&&ch>='0') ret=ret*10+ch-'0',ch=getchar();
    return f?-ret:ret;
}
ll n, m; 
ll a[maxn]; 
/*
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> m;
	ll sum = 0;
	for(int i = 0; i < n; i++){
		cin >> a[i];
		sum += a[i];
	}
	sort(a, a + n);
	if(sum < m){
		cout << "NO" << endl;
	}else{
		ll cnt = 0;
		bool flag = false;
		for(int i = 0; i < n; i ++){
			if(cnt + 1 < a[i] && cnt + 1 <= m){
				flag = true;
				break;
			}else{
				cnt += a[i];
				continue;
			}
		}
		if(!flag)cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}*/
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> m;
	for(int i = 0; i < n; i++)cin >> a[i];
	sort(a, a + n);
	ll sum = 0;
	for(int i = 0; i < n; i ++){
		if(sum + 1 < a[i]){
			break;
		}else{
			sum += a[i];
		}
	}
	if(sum < m)cout << "NO" << endl;
	else cout << "YES" << endl;
}

要坚持几记录这种简单,但是有靠考想法,还考代码的严谨。个人觉得这个题很好;

猜你喜欢

转载自blog.csdn.net/ab1605014317/article/details/85018565
今日推荐