forked from opencodeiiita/CodeDigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d5c650
commit ed75593
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
6 3 | ||
1 5 3 4 2 6 | ||
|
||
Expected Output : 5 | ||
Code Output : 4 | ||
|
||
The given test case gives wrong output on provided code. | ||
|
||
Reason: | ||
The code does not cover all possible coverage of range removal. In given code we can remove the subarray [3,4,2] which leads to final array to be [1,5,6] whose median is 5 largest possible, while the given solution does not considers this case, it checks for final array [1,5,3] and [4,2,6] only. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,67 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
#define ll long long int | ||
#define sz(x) ((long long)(x).size()) | ||
#define vl vector<ll> | ||
|
||
#include<ext/pb_ds/assoc_container.hpp> | ||
using namespace __gnu_pbds; | ||
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset; | ||
// *s.find_by_order, *s.order_of_key --> O(logn) | ||
void myerase(indexed_multiset &t, int v){ | ||
int rank = t.order_of_key(v); | ||
indexed_multiset::iterator it = t.find_by_order(rank); | ||
t.erase(it); | ||
} | ||
|
||
ll n,k; | ||
int ans; | ||
|
||
void func(ll i,vl &a,indexed_multiset &s){ | ||
if(i>=sz(a)) return; | ||
if(i==sz(a)-1) | ||
{ | ||
s.insert(a[i]); | ||
ll n=sz(s); | ||
ans = max(ans,*s.find_by_order((n+1)/2 -1)); | ||
myerase(s,a[i]); | ||
|
||
if(k==1){ | ||
n=sz(s); | ||
ans = max(ans,*s.find_by_order((n+1)/2 -1)); | ||
} | ||
return; | ||
} | ||
|
||
//choose cur | ||
s.insert(a[i]); | ||
func(i+1,a,s); | ||
myerase(s,a[i]); | ||
|
||
//choose after removing | ||
func(i+k,a,s); | ||
} | ||
|
||
void solve() | ||
{ | ||
cin>>n>>k; | ||
vl a(n); | ||
for(ll i=0;i<n;i++){ | ||
cin>>a[i]; | ||
} | ||
indexed_multiset s; | ||
func(0,a,s); | ||
cout<<ans<<endl; | ||
} | ||
|
||
int main() | ||
{ | ||
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); | ||
|
||
int T=1; | ||
// cin>>T; | ||
while(T--) | ||
solve(); | ||
|
||
return 0; | ||
} |