|
|
1) кучи должны быть одинаковыми? 2) если нет то почему нельзя взять самое маленькое число и вычесть из него остальные? 1 тест 13+14+8-27-5=3 или 27+8-13+14+5=3 Can somebody tell me how to implement dp for this probelm? dp cant be applied to every problem Can somebody tell me how to implement dp for this probelm? Edited by author 15.07.2023 07:47 не могу найти ошибку,возможно ли узнать какие числа вбивает 8-й тест? Только это называется не "ошибка в 8-м тесте" (сам тест корректен), а "ошибка *в решении* НА 8-м тесте". Тест - просто случайный тест, направленный на убивание жадных решений. Эту задачу неверно решать жадным алгоритмом. тест не случайный, так как при разных алгоритмах ошибки всегда под одними и теми же номерами #include <iostream> #include<vector> #include<algorithm> using namespace std; int main() { int n, a, razn, k1 = 0, k2 = 0; cin >> n; vector<int>num; for (int i = 0; i < n; i++) { cin >> a; num.push_back(a); } sort(num.begin(), num.end()); int kol = 0; for (int i = 0; i < n - 1; i++) {//все камни кроме максимума kol += num[i]; } if (kol < num.back()) {//если все камни меньше самого максимального cout << num.back() - kol; } else if (kol == num.back()) {//если все камни pавны максимальному cout << 0; } else if (kol > num.back()) {//если все камни больше максимального k2 = num.back(); for(int i =n-2;i>=0;i--){ if (k1 < k2) { k1 += num[i]; } else if (k1 > k2) { k2 += num[i]; } else if (k1 == k2) { k1 = num[i]; k2 = 0; } } cout << abs(k1 - k2); } } //my program is fine solves any of my values. what is in test 5? здесь надо делать перебор битовыми масками Edited by author 28.01.2023 12:25 I don’t understand what the problem is when passing the test 8. everything works for me Edited by author 22.06.2022 22:21 try this: 8 6 7 9 13 18 24 31 50 expected: 0 My program do this #include <iostream> int main() { int temp = 0, maxtemp=0, even = 0, midle = 0, different = 0, maxdifferent = 0; int arrayA[100]; int sizeArray; std::cin >> sizeArray; for (int i = 0; i < sizeArray; i++) { std::cin >> arrayA[i]; } for (int i = 0; i < sizeArray; i++) { for (int j = i; j < sizeArray; j++) { if (arrayA[i] < arrayA[j]) { temp = arrayA[i]; arrayA[i] = arrayA[j]; arrayA[j] = temp; }; }; }; for (int i = 0; i < sizeArray; i++) { midle += arrayA[i]; }; even = midle % 2; maxdifferent= different= midle = midle / 2 +even; if (sizeArray != 1) { for (int i = 0; i < sizeArray; i++) { different = midle; for (int j = i; j < sizeArray; j++) { if (different >= arrayA[j]) { temp += arrayA[j]; different -= arrayA[j]; }; }; if (maxdifferent > different) { maxdifferent = different; }; }; }; maxdifferent = 2* maxdifferent -even; std::cout << maxdifferent; } My Python program solved this problem using 0.7+ seconds and 64000+KB memory. However, I saw some people solved it with Python using just about 0.1 secs and 500KB mem. Can anyone give me a hint about how to reach that efficiency? Try to use bitmasks instead of using recursion;) thx!! You can solve this task with (n/2) * 2^(n/2) complexity using meet in the middle Since the requirements became stronger, and time was reduced from 2 sec down to 1 sec, now it is not possible to resolve the task using bruteforce method, python is very slow for such hard requirements. Even I do empty loops the taken time is already about 1.3 sec. from time import time time_before = time() len_lst = 20 for i in range(1, ((2 ** len_lst) // 2) - 1):
for j in range(len_lst): pass
print(time() - time_before) >>> 1.3192360401153564 I think for Python the time should be rolled back to 2 sec. Of cause if I use bruteforce on "C" it takes about 0.2 sec, but Python is not "C". Finally I could resolve the task on Python using DP for 0.65 sec. Hi there! Here is my code for this task: https://notabug.org/thrashzone_ua/c_examples/src/master/rock_heap.c Short notes: 1) Cases when there is one or two rocks from input are separated 2) In case amount of rocks is more then 2, I'm checking if there is a sublist in list of rocks with sum(sublist) = sum(list) / 2. If there is such a sublist and the number of sum is even - 0 will be printed, if number is not even - 1; 3) In case there no such sublist - it's time for greedy algorithm. Could you please take a look and comment my code? Or maybe provide with test set, on which it will fail? Thank you in advance. So, long story short - do not use greedy algorithm, read and think more about dynamic programming. After going through all the discussion topics, I found that this problem can be solved using different techniques. I have made a list of keywords, and I would like to know exactly how many different ways are there to solve this problem? 1) Brute force 2) Bit mask 3) Dynamic Programming 4) Backtracking 5) Balanced Partition 6) Partition problem 7) Greedy algorithm If you can solve the question with DP, you should avoid Brute Force. Although, I could not come up with a Greedy algorithm. I solved it using DP. try this: 8 6 7 9 13 18 24 31 50 expected: 0 8 6 7 9 13 18 24 31 50 My output : 4 expected: 0(How) 8 6 7 9 13 18 24 31 50 (18+6+24+31)-(50+7+13+9)=0 Who knows what is the test 7? I failed at this test. [code deleted] Edited by moderator 29.01.2022 18:45 6 1 2 3 4 100 100 Will fail this program The solution is not correct sort(a + 1, a + 1 + n); reverse(a + 1, a + 1 + n); for(int i = 1; i <= n; i++){ if(sum1 > sum2){ sum2 += a[i]; } else{ sum1 += a[i]; } } cout << abs(sum1 - sum2); let me give you an example 13 14 27 if I understand your solution you are sorting it and then add to left or to the right. in this example the diff is 0 what your program returns? The same thing. I tried to enter almost everything, it works correctly, but i always have WA on 5 test Test Case : 5 5 5 4 3 3 2 Test Case : 5 5 5 4 3 3 Edited by author 15.01.2019 16:49 Edited by author 06.11.2018 20:49 This problem can be solved using brute force. The asymptotics is O(n*2^n) but still the time limit is not hit, provided you use bit operations instead of generating arrays. For you beginners, I post my code here, but I strongly recommend to write this on your own first. The language I use is Java; nextInt() function returns the next integer from the input. [code deleted] Worst time is 0.187 sec, as reported by Timus. Edited by moderator 21.10.2019 22:59 Hi ! Would you mind explaining the if on the second for ? I mean, how is this putting all different combination of blocks on each pile ? Thanks :D man u're awesome :) solution is great for that kinda bruteforce! just made all those things in cpp myself and got ACd :D but this problem's still far too hard for the "beginners" tag on which it is right now :) Can anybody translate me this code on C++ or Pascal Thanks, got AC converting it into C++. input test 2 is: 1 1 Edited by author 06.02.2016 22:45 Thanks, mate! Serves me right for being too impatient and not considering the basic corner cases! :-) Hello, Does anybody know what is the 2nd test? I can't get my head round this issue. I don't know what is the second test I have Accepted status Because I am assuming that there is the only way to solve that task And this is BRUTE FORCE I don't know why I got TLE on test 3. I use bruteforce with Python. But in my PC, it runs so fast with n = 20 |
|
|