1. 程式人生 > >牛客練習賽34D(數字處理)

牛客練習賽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;
}

要堅持幾記錄這種簡單,但是有靠考想法,還考程式碼的嚴謹。個人覺得這個題很好;