- Prepare
- Mathematics
- Combinatorics
- Number List
- Discussions
Number List
Number List
+ 0 comments 2 line Python solution, 100%
Could be even 1, but split into 2 for readability :)
segs = [len(list(j)) for i,j in groupby(a, lambda x:x>k) if not i] return math.comb(len(a)+1,2) - sum(math.comb(i+1,2) for i in segs)
Notes:
answer = count of all subarrays - count of subarrays where all numbers <=k
.- count of all subarrays is just N+1 choose 2
segs
(funny name ikr) array is the lengths of all subarrays where elements are <=k.
+ 0 comments include
using namespace std;
define ll long long
define no cout<<"No"<
define yes cout<<"Yes"<
define mod 1000000007
define nt 10 define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)void solve() { vectorv; sets; mapmp; ll n,m,j,i,y,z,l,r,d,x,k; cin>>n>>k; ll a[n+1]; ll c=0; for(i=0;i>a[i]; if(a[i]>k)c++; } x=0; ll mx=-1,ans=0; for(i=0;ik) { ans+=(i-mx)*(n-i); mx=i; } } cout<
} int main() { fast; int T=1; cin>>T; while(T--)solve(); return 0; }
+ 0 comments Hint: prefix array
+ 0 comments Just a humble request to all others who have solved questions like these, please refrain from showing the solutions or giving links to it here in the discussion as it destroys the purpose of solving such problems. Please try to give only intuitions to guide those who need assistance rather than giving them the code.
Thank you, Sincerely, akshay_123
+ 0 comments ruby:
gets.to_i.times do n,k = gets.strip.split.map &:to_i a = gets.strip.split.map &:to_i max, prev = (1..n).reduce(:+), 0 for i in 0...n do if a[i] > k max -= (1..prev).reduce(:+) || 0 prev = 0 else prev +=1 end i += 1 end max -= (1..prev).reduce(:+) || 0 puts max end
Sort 17 Discussions, By:
Please Login in order to post a comment