2022 年 TPC 腾讯程序设计竞赛 赛题题库 1B 负载均衡

image.png
#include
#include
#include

using namespace std;
inline int round(int i, int size){
    if(i < size){
        return i;
    }
    return i - size;
}

bool canServer(vector const &r,  vector const &s){
    int n = (int)r.size();
    vector newS = s;
    size_t size = s.size();
    vector a;
    vector b;
    a.resize(r.size());
    b.resize(r.size());
 
    for (int i = 0; i < n; ++i) {
        if(r[i] <= newS[i]){
            newS[i] -= r[i];
            a[i] = r[i];
        }else{
            a[i] = newS[i];
            newS[i] = 0;
            
            int preAdd = r[i] - a[i];
            b[i] = preAdd;
           
            int const nextI = round(i + 1,n);
            if(preAdd > newS[nextI]){
                int const end = i + (int)size;
                for(int j = i + 1; j < end; ++j){
                    int const readJIndex = round(j,n);
                    if(a[readJIndex] + b[readJIndex] != r[readJIndex]){
                        if(preAdd > newS[readJIndex]){
                            return false;
                        }
                        newS[readJIndex] -= preAdd;
                        preAdd = 0;
                        break;
                    }else{
                        if (preAdd <= newS[readJIndex]) {
                            newS[readJIndex] -= preAdd;
                            preAdd = 0;
                            break;
                        }
                        
                        preAdd = preAdd - newS[readJIndex];
                        newS[readJIndex] = 0;
                        
                        if(a[readJIndex] < preAdd){
                            return false;
                        }
                        a[readJIndex] -= preAdd;
                        b[readJIndex] += preAdd;
                    }
                }
            }else{
                newS[nextI] -= preAdd;
                preAdd = 0;
            }
            if(preAdd != 0){
                return false;
            }
        }
    }
    return true;
}
/*
 1
 8
 5  15 10 25 35 35 35 55
 10 15 10 25 30 35 40 45
 
 
 1
 8
 5  15 10 25 35 35 35 50
 10 15 10 25 30 35 40 45
 
 
 1
 8
 5  15 10 25 35 35 36 50
 10 15 10 25 30 35 40 45
 */
int main(){
    int row;
    cin>> row;
    for (int i = 0; i < row; ++i) {
        int n = 0;
        cin>>n;
        vector r;
        vectors;
        r.resize(n);
        s.resize(n);
        for (int j = 0; j < n; ++j) {
            cin>>r[j];
        }
        
        for (int j = 0; j < n; ++j) {
            cin>>s[j];
        }
        if (i != 0) {
            cout<<"\n";
        }
        if (canServer(r, s)) {
            cout<<"Yes";
        }else{
            cout<<"No";
        }
    }

    return 0;
}


你可能感兴趣的:(2022 年 TPC 腾讯程序设计竞赛 赛题题库 1B 负载均衡)