diff --git a/playground/IOFiles/correct_answer.txt b/playground/IOFiles/correct_answer.txt index e69de29..ebbdab6 100644 --- a/playground/IOFiles/correct_answer.txt +++ b/playground/IOFiles/correct_answer.txt @@ -0,0 +1,3 @@ +7 +2000000000 +0 diff --git a/playground/IOFiles/output.txt b/playground/IOFiles/output.txt index e69de29..ebbdab6 100644 --- a/playground/IOFiles/output.txt +++ b/playground/IOFiles/output.txt @@ -0,0 +1,3 @@ +7 +2000000000 +0 diff --git a/playground/IOFiles/testcase.txt b/playground/IOFiles/testcase.txt index e69de29..3e2b269 100644 --- a/playground/IOFiles/testcase.txt +++ b/playground/IOFiles/testcase.txt @@ -0,0 +1,10 @@ +3 +3 5 +1 2 +3 4 +2 1000000000 +1 1 +999999999 999999999 +10 3 +5 10 +7 8 diff --git a/playground/Makefile b/playground/Makefile index 445f3dd..09ef85a 100644 --- a/playground/Makefile +++ b/playground/Makefile @@ -5,7 +5,7 @@ SOLUTION_BINARY = $(BUILD_DIR)/solution BRUTEFORCE_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/bruteForce) GENERATOR_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/generator) CODEDIRS = . ./Stress_Testing -INCDIRS = . +INCDIRS = . ./include CXX = g++ diff --git a/playground/Stress_Testing/bruteForce.cpp b/playground/Stress_Testing/bruteForce.cpp index c81c6dc..03b25b2 100644 --- a/playground/Stress_Testing/bruteForce.cpp +++ b/playground/Stress_Testing/bruteForce.cpp @@ -1,42 +1,126 @@ -#include +#include +#include + +#define ll long long int +#define DEPTH (ll)13 using namespace std; -const int N = int(2e5) + 9; +ll ans = (ll)1e16+1; -int n, k, h; -int need = int(1e9); -int cnt[N]; +ll intersection(int l1, int r1, int l2, int r2){ + return 1LL * ( min( r1, r2) - max(l1 , l2) ); +} -int main() { - scanf("%d %d", &n, &k); - for(int i = 0; i < n; ++i){ - int x; - scanf("%d", &x); - h = max(h, x); - need = min(need, x); - ++cnt[x]; - } - - int pos = N - 1; - int res = 0; - long long sum = 0; - int c = 0; - while(true){ - long long x = sum - c * 1LL * (pos - 1); - if(x > k){ - ++res; - h = pos; - sum = pos * 1LL * c; - } - - --pos; - if(pos == need) break; - c += cnt[pos]; - sum += cnt[pos] * 1LL * pos; - } - - if(h != need) ++res; - - cout << res << endl; +ll total_intersection(vector>& list1, vector>& list2){ + int n = (int)list1.size(); + ll I = 0; + for(int i = 0 ; i < n ; i++ ) + I += intersection(list1[i].first , list1[i].second, list2[i].first, list2[i].second ); + + return I; +} + +void move(vector>& list1, vector>& list2, int k, ll total_moves){ + + int n = (int)list1.size(); + + ll I = 0; + for(int i = 0 ; i < n ; i++ ) + I += max( 0LL , intersection(list1[i].first , list1[i].second, list2[i].first, list2[i].second ) ); + + // cout << "I: " << I << '\n'; + if( I >= k ){ + ans = min( ans , total_moves ); + return; + } + + if( total_moves > DEPTH ) + return; + + // cout << "total_moves: " << total_moves << '\n'; + + int curr_I = total_intersection(list1,list2); + int imprv_I = 0; + for(int i = 0 ; i < n ; i++ ){ + + list1[i].first--; + + imprv_I = total_intersection(list1,list2); + + if( imprv_I > curr_I ) + move(list1, list2, k, total_moves + 1); + list1[i].first++; + + list1[i].second++; + + imprv_I = total_intersection(list1,list2); + + if( imprv_I > curr_I ) + move(list1, list2, k, total_moves + 1); + list1[i].second--; + + list2[i].first--; + + imprv_I = total_intersection(list1,list2); + + if( imprv_I > curr_I ) + move(list1, list2, k, total_moves + 1); + list2[i].first++; + + list2[i].second++; + + imprv_I = total_intersection(list1,list2); + + if( imprv_I > curr_I ) + move(list1, list2, k, total_moves + 1); + list2[i].second--; + + + list1[i].first--, list2[i].first--; + + imprv_I = total_intersection(list1,list2); + + if( imprv_I > curr_I ) + move(list1, list2, k , total_moves+2); + list1[i].first++, list2[i].first++; + + list1[i].second++, list2[i].second++; + + imprv_I = total_intersection(list1, list2); + + if( imprv_I > curr_I ) + move(list1, list2, k ,total_moves+2); + + list1[i].second--, list2[i].second--; + + } +} + +int main(){ + + int t; + cin >> t; + + int n, k, l1, r1, l2, r2; + + while(t--){ + cin >> n >> k; + cin >> l1 >> r1; + cin >> l2 >> r2; + + vector> list1, list2; + + for(int i = 0 ; i < n ; i++ ) + list1.push_back({l1,r1}); + + for(int i = 0 ; i < n ; i++ ) + list2.push_back({l2,r2}); + + move(list1, list2, k, 0); + + cout << ans << '\n'; + } + + return 0; } \ No newline at end of file diff --git a/playground/Stress_Testing/generator.cpp b/playground/Stress_Testing/generator.cpp index 45cc19f..e14a883 100644 --- a/playground/Stress_Testing/generator.cpp +++ b/playground/Stress_Testing/generator.cpp @@ -12,20 +12,28 @@ int main(int argc, char* argv[]){ srand(seed); - int n = rnd(1,20); + int n = rnd(1,3); - int k = rnd(20, 40); + int k = rnd(1,6); - cout << n << ' ' << k << '\n'; + int l1 = rnd(1,4); - int val = 0; - for(int i = 0 ; i < n ; i++ ){ + int r1 = rnd(l1,8); - val = rnd(0,50); + int l2 = rnd(l1, 8); - cout << val << ' '; + int r2 = rnd(l2,10); + + if( rand() % 2 == 0 ){ + swap(l1,l2); + swap(r1,r2); } + + cout << 1 << '\n'; + cout << n << ' ' << k << '\n'; + cout << l1 << ' ' << r1 << '\n'; + cout << l2 << ' ' << r2 << '\n'; - cout << '\n'; + // cout << '\n'; return 0; } \ No newline at end of file diff --git a/playground/build/bruteForce b/playground/build/bruteForce new file mode 100644 index 0000000..a183470 Binary files /dev/null and b/playground/build/bruteForce differ diff --git a/playground/build/generator b/playground/build/generator new file mode 100644 index 0000000..384a89a Binary files /dev/null and b/playground/build/generator differ diff --git a/playground/build/objs/bruteForce.d b/playground/build/objs/bruteForce.d new file mode 100644 index 0000000..f546c37 --- /dev/null +++ b/playground/build/objs/bruteForce.d @@ -0,0 +1 @@ +build/objs/bruteForce.o: Stress_Testing/bruteForce.cpp diff --git a/playground/build/objs/bruteForce.o b/playground/build/objs/bruteForce.o new file mode 100644 index 0000000..ab28c78 Binary files /dev/null and b/playground/build/objs/bruteForce.o differ diff --git a/playground/build/objs/generator.d b/playground/build/objs/generator.d new file mode 100644 index 0000000..b28f937 --- /dev/null +++ b/playground/build/objs/generator.d @@ -0,0 +1 @@ +build/objs/generator.o: Stress_Testing/generator.cpp diff --git a/playground/build/objs/generator.o b/playground/build/objs/generator.o new file mode 100644 index 0000000..554a26c Binary files /dev/null and b/playground/build/objs/generator.o differ diff --git a/playground/build/objs/solution.d b/playground/build/objs/solution.d new file mode 100644 index 0000000..52ad141 --- /dev/null +++ b/playground/build/objs/solution.d @@ -0,0 +1 @@ +build/objs/solution.o: solution.cpp diff --git a/playground/build/objs/solution.o b/playground/build/objs/solution.o new file mode 100644 index 0000000..581357a Binary files /dev/null and b/playground/build/objs/solution.o differ diff --git a/playground/build/solution b/playground/build/solution new file mode 100644 index 0000000..6e9e4dd Binary files /dev/null and b/playground/build/solution differ diff --git a/playground/include/debug.h b/playground/include/debug.h new file mode 100644 index 0000000..29c1799 --- /dev/null +++ b/playground/include/debug.h @@ -0,0 +1,18 @@ +#ifndef __DEBUG__ +#define __DEBUG__ + +/* +* var_name: to display variable name used +* var_value: to display variable value with variable name. +*/ + +#define var_name(arg) #arg + + +#ifdef NPRINT_VAR + #define var_value(arg) ((void)0) +#else + #define var_value(arg) cout << var_name(arg) << ": " << arg << endl; +#endif + +#endif \ No newline at end of file diff --git a/playground/run.sh b/playground/run.sh index e2125d8..5c0cddd 100644 --- a/playground/run.sh +++ b/playground/run.sh @@ -93,7 +93,7 @@ do test_output=$(<"$OUTPUT_FILE") - if diff -q -Z <(echo $test_output) <(echo $expected_output) > /dev/null; then + if diff -q -Z <(echo -e $test_output) <(echo -e $expected_output) > /dev/null; then echo -e "\033[0;32mTest #$(($test_num+1)): PASS\033[0m" #Debug mode - in case to see the output. diff --git a/playground/solution.cpp b/playground/solution.cpp index 0b9524b..3441094 100644 --- a/playground/solution.cpp +++ b/playground/solution.cpp @@ -1,9 +1,106 @@ -#include - -using namespace std; - -int main(){ - int a,b; - cin >> a >> b; - cout << a << ' ' << b << '\n'; +#include + +#define NPRINT_VAR +#define var_name(arg) #arg + + +#ifdef NPRINT_VAR + #define var_value(arg) ((void)0) +#else + #define var_value(arg) cout << var_name(arg) << ": " << arg << endl; +#endif + +#define ll long long int + +using namespace std; + +int main(){ + + int t; + cin >> t; + + ll n , k , al , ar, bl , br , value , extend, cover + , factor , min_step ; + + ll new_ans = 0 , ans = 0 , I = 0 , adder; + + while( t-- ){ + ans = 0; + new_ans = 0; + adder = 0; + cin >> n >> k >> al >> ar >> bl >> br; + + I = 1LL * n * ( min(ar , br) - max(al , bl) ); + + var_value(I); + + if( I >= k ){ + cout << ans << '\n'; + continue; + } + + value = k; + + if( I > 0 ) + value -= I; + + var_value(value); + + extend = (max(al , bl) - min(al , bl)) + + (max(ar , br) - min(ar , br)); + + var_value(extend); + + cover = max(ar , br) - min(al , bl); + + if( I > 0 ) + cover -= I / n; + + var_value(cover); + + if( cover > 0 ) + factor = value / cover; + else + factor = n + 1; + + var_value(factor); + + ans += extend * min(factor,n); + + var_value(ans); + + if( min(factor,n) * cover == value ){ + cout << ans << '\n'; + continue; + } + + value -= min(factor,n) * cover; + + var_value(value); + + if( factor < n && factor > 0 ) + adder = 2 * (value); + else + adder = (ll)1e17+1; + + var_value(adder); + + if( n > factor ){ + + min_step = value - (( min(ar,br) - max(al,bl) ) - max(I/n,0LL)); + + new_ans = ans + min_step; + + var_value(new_ans); + + ans = min(new_ans, ans + adder); + } + else{ + ans += 2 * value; + } + + cout << ans << '\n'; + } + + return 0; } \ No newline at end of file diff --git a/playground/stress.sh b/playground/stress.sh index 0cc0a4b..e27b338 100644 --- a/playground/stress.sh +++ b/playground/stress.sh @@ -22,7 +22,7 @@ while getopts l:nh param; do echo "Provide the limit of stress testing" echo "$0 'test case limit'" echo "$0 -1 (for unlimited)" - echo "OPTIONAL: $0 -n (for running with bruteforce)" + echo "OPTIONAL: $0 -n (for not running with bruteforce)" exit 0 ;; esac @@ -59,7 +59,8 @@ do # cat $CORRECT_ANSWER_FILE #Use cat for diff... - if ! diff -q <(cat "$OUTPUT_FILE") <(cat "$CORRECT_ANSWER_FILE") > /dev/null; then + #head -n 1 - for single line comparison + if ! diff -Z -q <(cat "$OUTPUT_FILE") <(cat "$CORRECT_ANSWER_FILE") > /dev/null; then echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m" echo "Test case:" cat $TESTCASE_FILE