add: 220624

This commit is contained in:
Eat-Swap 2022-06-24 20:59:03 +08:00
parent ad881f1dcd
commit 9c050ebcb5
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 44 additions and 1 deletions

43
cpp/2206/220624.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <vector>
#include <queue>
#include <numeric>
#include <iostream>
/**
* 1354. Construct Target Array With Multiple Sums
* You are given an array target of n integers. From a starting array arr consisting of n 1's, you may perform the following procedure :
* - let x be the sum of all elements currently in your array.
* - choose index i, such that 0 <= i < n and set the value of arr at index i to x.
* - You may repeat this procedure as many times as needed.
* Return true if it is possible to construct the target array from arr, otherwise, return false.
*/
class Solution {
public:
static bool isPossible(const std::vector<int>& target) {
if (target.size() == 1 && target[0] != 1)
return false;
std::priority_queue<int> q(target.begin(), target.end());
auto sum = std::accumulate(target.begin(), target.end(), 0LL);
while (q.top() > 1) {
long long x = q.top();
q.pop();
long long y = q.top(), delta = sum - x;
if (x - delta < 1)
return false;
long long nx = x - ((x - y) / delta + ((x - y) % delta ? 1 : 0)) * delta;
if (nx < 1)
nx += delta;
q.push(nx);
sum = sum - x + nx;
}
return true;
}
};
int main() {
std::cout << Solution::isPossible({1,1,10});
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2206)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2206 220623.cpp) ADD_EXECUTABLE(2206 220624.cpp)