mirror of
https://github.com/Hizenberg469/Algorithms-snippets.git
synced 2026-04-19 14:42:23 +03:00
update
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
7
|
||||
2000000000
|
||||
0
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
7
|
||||
2000000000
|
||||
0
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
3
|
||||
3 5
|
||||
1 2
|
||||
3 4
|
||||
2 1000000000
|
||||
1 1
|
||||
999999999 999999999
|
||||
10 3
|
||||
5 10
|
||||
7 8
|
||||
|
||||
@@ -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++
|
||||
|
||||
@@ -1,42 +1,126 @@
|
||||
#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#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<pair<int,int>>& list1, vector<pair<int,int>>& 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<pair<int,int>>& list1, vector<pair<int,int>>& 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<pair<int,int>> 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
BIN
playground/build/bruteForce
Normal file
BIN
playground/build/bruteForce
Normal file
Binary file not shown.
BIN
playground/build/generator
Normal file
BIN
playground/build/generator
Normal file
Binary file not shown.
1
playground/build/objs/bruteForce.d
Normal file
1
playground/build/objs/bruteForce.d
Normal file
@@ -0,0 +1 @@
|
||||
build/objs/bruteForce.o: Stress_Testing/bruteForce.cpp
|
||||
BIN
playground/build/objs/bruteForce.o
Normal file
BIN
playground/build/objs/bruteForce.o
Normal file
Binary file not shown.
1
playground/build/objs/generator.d
Normal file
1
playground/build/objs/generator.d
Normal file
@@ -0,0 +1 @@
|
||||
build/objs/generator.o: Stress_Testing/generator.cpp
|
||||
BIN
playground/build/objs/generator.o
Normal file
BIN
playground/build/objs/generator.o
Normal file
Binary file not shown.
1
playground/build/objs/solution.d
Normal file
1
playground/build/objs/solution.d
Normal file
@@ -0,0 +1 @@
|
||||
build/objs/solution.o: solution.cpp
|
||||
BIN
playground/build/objs/solution.o
Normal file
BIN
playground/build/objs/solution.o
Normal file
Binary file not shown.
BIN
playground/build/solution
Normal file
BIN
playground/build/solution
Normal file
Binary file not shown.
18
playground/include/debug.h
Normal file
18
playground/include/debug.h
Normal file
@@ -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
|
||||
@@ -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.
|
||||
|
||||
@@ -1,9 +1,106 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
int a,b;
|
||||
cin >> a >> b;
|
||||
cout << a << ' ' << b << '\n';
|
||||
#include <iostream>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user