题目描述:
题解:逆向的构造,数组中最大数为上一轮所有数字之和。利用好这个规律,用优先队列存这些数字,每次取出最大数now,将其还原为上一轮的数字,如果还原之后的数<1,那么构造失败
AC代码:
bool isPossible(vector<int>& target) { priority_queue<long long,vector<long long>,less<long long>> q; long long sum = 0; for(int x : target) { q.push(x); sum += x; } while(q.size()!=0) { long long now = q.top(); q.pop(); if(now == 1) continue; if(now - (sum-now) < 1) return false; long long tmp = now; now -= (sum-tmp); sum -= (tmp-now); q.push(now); } return true; }