求助大佬,我的代码错误在最后的循环中,请问和正确答案有什么区别呀。
查看原帖
求助大佬,我的代码错误在最后的循环中,请问和正确答案有什么区别呀。
293607
Pureqyu楼主2020/11/7 10:59
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = 2610;
struct COW{
    int low, high;
}cow[N];
struct SPF{
    int v, sum;
}spf[N];

bool cmp(COW a, COW b){
    if(a.high != b.high)
        return a.high < b.high;
    else
        return a.low < b.low;
}
bool cmp1(SPF a, SPF b){    
    return a.v < b.v;
}
int main(){
    int c, l;
    cin >> c >> l;
    for(int i = 1; i <= c; i ++) 
        cin >> cow[i].low >> cow[i].high;
    for(int i = 1; i <= l; i ++)
        cin >> spf[i].v >> spf[i].sum;
    
    int cnt = 0;
    sort(cow + 1, cow + c + 1, cmp);
    sort(spf + 1, spf + l + 1, cmp1);

    for(int i = 1, j = 1; i <= c && j <= l; i ++){
        if(spf[j].v >= cow[i].low && spf[j].v <= cow[i].high && spf[j].sum != 0){
            cnt ++;
            spf[j].sum --;
            if(spf[j].sum == 0)
                j ++;
        }
        else if(spf[j].v < cow[i].low)
            j ++;
    }
    cout << cnt << endl;

    return 0;
}
2020/11/7 10:59
加载中...