CF797E

關於E題, 我本來想著 把所有數字取模後排序, 然後二分找答案, 但是因為每次選擇某個數字後的刪除操作 需要用O(n), 就不知道怎麼實現了. 而且還可以用二分去找匹配的剩餘價值.

原來有multiset這個好工具, 基本能自動排序, 和支持刪除操作, stl大法好, 而且還支持lowerbound 和upperbound 操作,

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include<deque>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> nums;
#include<iostream>
#include<set>
#include<vector>
using namespace std;
void work(){
    ll n, k, x, ans = 0;
    cin >> n >> k;
    multiset<ll> s;
    for(int i = 1; i<= n; i++){
        cin >> x;
        if(x >= k){
        	ans += x / k;
        }
        s.insert(x%k);
    }
    while(s.size()){
        int v = *s.begin();
        s.erase(s.begin());
        
        if(s.end() != s.lower_bound(k - v)){
            ans++;
            s.erase(s.lower_bound(k - v));
        }
    }
    cout << ans << endl;
}
int main(){
    int t;
    cin >> t;
    while(t--){
        work();
    }
    return 0;
}

實在方便, erase 操作, 會一次性把所有等於那個數值的東西刪除, 我們只需要刪除用了那個數, 所以每次拿begin()的位置, 然後刪除begin()的位置, 拿了的數字都要刪除, 然後就是記得用long long.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值