其他分享
首页 > 其他分享> > Division

Division

作者:互联网

#include <iostream>
#include <cmath>
#include<vector>
#include<cstring>
#include<stack>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;

ll b[N], c[N];
int n, k;



void solve() {
	cin >> n>> k;
    memset (b, 0, sizeof b);
    memset (c, 0, sizeof c);
    for (int i = 1; i<= n; i++) {
        ll t; cin >> t;
        while (t > 1) t >>= 1,b[i] ++;
     }
    for (int i = n + 1; i >= 1; i --) b[i] -= b[ i -1];

    stack<int> s;
    vector<pair<int, int>> ans;
    for (int i = 1; i <= n + k; i ++) {
        if (i - k >= 1) {
            while(b[i - k]) {
                s.push(i - k);
                b[i - k] --;
            }
        }
        if (b[i] < 0) {
            while (b[i]) {
                if (!s.size()) {
                    puts("-1");
                    return ;
                }
                ans.push_back({s.top(), i - 1});
                s.pop();
                b[i] ++;
            }
        }
    }
    if (s.size()) {
        puts("-1");
        return ;
    }
    cout <<  ans.size() <<  endl;
    for (auto t : ans) cout << t.first << " " << t.second << endl;
}
 int main () {
    int T;
    cin >> T;
    while (T --) {
        solve();
    }
    return 0;
}

通过(loga[i] / log(2))转化成贪心题目. 每次选择一正一负数,a[i]--, a[j] ++; 最终整个数组为0,就成功了

标签:Division,return,int,++,--,while,include
来源: https://blog.csdn.net/qq_28409093/article/details/122618846