当前位置: 代码迷 >> 综合 >> CF1614A Divan and a Store
  详细解决方案

CF1614A Divan and a Store

热度:97   发布时间:2023-10-14 00:33:53.0

原题链接

题意

CF1614A Divan and a Store
来源:洛谷

思路

排序后,只加选在范围内的数,如果总和大于k就退出

代码

#include<bits/stdc++.h> 
#define int long long
using namespace std;
const int N = 150;
int a[N];
signed main()
{
    int t; cin >> t;while (t -- ){
    int n, l ,r, k;cin >> n >> l >> r >> k;for (int i = 1; i <= n; i ++ ){
    cin >> a[i];}sort(a + 1, a + 1 + n);int sum = 0, ans = 0;for (int i = 1; i <= n; i ++ ){
    if (a[i] >= l && a[i] <= r){
    sum += a[i];if (sum > k) break;ans ++;}}cout << ans << endl;}return 0;
}
  相关解决方案