Basketball tournament

Sort by

recency

|

12 Discussions

|

  • + 0 comments

    I love basketball very much! I have been wacthing different basketball tournaments since my school life. Now I am working as a senior and help people with having nudits chat rooms around the world so that they could find a way to have a wonderful time! Thanks for sharing this post.

  • + 0 comments

    Hello, the below is my solution : I get error on this line : temp_li.append(list(itertools.combinations(varying_range, r=i))) Memory Error. Please help me in resolving the same.

    ''' https://www.hackerrank.com/contests/hourrank-31/challenges/basketball-tournament-1/problem '''

    import itertools
    from functools import reduce
    n, m = list(map(int, input().split()))
    heights = list(map(int, input().split()))
    heights = sorted(heights)
    
    def list_of_l_r(l, r):
        temp_li = []
        temp_li.clear()
        varying_range = list(range(l,r+1))
        len_ = len(varying_range)
        for i in range(1, len_+1):
            temp_li.append(list(itertools.combinations(varying_range, r=i)))
            print(len(temp_li))
            # yield list(itertools.combinations(varying_range, r=i))
            # li1 = list(itertools.combinations(varying_range, r=i))
    
        print(temp_li)
        return temp_li
    
    def f(i, j):
        return i+j
    def calculate_power(collection):
        # retrieve the heights from the index
        # index comes from collections
        selected_height = []
        for i in collection:
            selected_height.append(heights[i-1])
        # calculate power for them
        combinations = list(itertools.product(selected_height, repeat=2))
        # print(combinations)
    
        power = 0
        for items in combinations:
            power += sum(items)
        # print(power)
        return power
    
    def returnMin(matrix_of_indexes, x):
        height_max = []
        for matrix_items in matrix_of_indexes:
            for list_items in matrix_items:
                # print(list_items)
                power = calculate_power(list_items)
                if power >= x:
                    # check for max value of height
    
                    for i in list_items :
                        height_max.append(heights[i-1])
                    # print("final_heights", height_max)
                    # print("minimum is :")
                    # print(height_max)
                    # print("before returning height_max:", list_items)
                    return max(height_max);
        if height_max == []:
            return -1
    for i in range(m):
        l, r, x = list(map(int, input().split()))
        # print(l, r, x)
    
        # reproduce the sequences
        matrix_of_indexes = list_of_l_r(l, r)
        print(returnMin(matrix_of_indexes, x))
    
  • + 0 comments

    k=2

  • + 0 comments
    #include <cstdio>
    My C++ program. It does not pass all test cases due to excessive time but it took 50 points. As far as the algorithm is concerned is right.
    int main(){
    	long long n = 0; long long m = 0;
    	FILE* fin = stdin; //fopen("basket.txt", "rt");
    	
    	fscanf(fin, "%lld %lld", &n, &m);
    	
    	long long height[n+1]; height[0] = 0;
    	long long total_sum[n+1]; total_sum[0] = 0;
    	long long P[n+1]; P[0] = 0;
    	
    	for(long long i = 1; i < n+1; i++){
    		fscanf(fin, "%lld", &height[i]);
    		total_sum[i] = total_sum[i - 1] + height[i];
    	}
    	
    	long long k = 100000000; long long subk; long long subk_i;
    	long long i, j;
    	long long l, r, x;
    	bool flag1 = true; bool flag2 = true;
    	long long power;
    	
    	for(long long a = 0; a < m; a++){
    		fscanf(fin, "%lld %lld %lld\n", &l, &r, &x);
    		flag1 = true; flag2 = true;
    		k = 100000000;
    		power = 0;
    		i = l; j = l;
    		long long max_power;
    			flag2 = true;
    			power = 0;
    			subk = 0;
    			subk_i = -1;
    			while(j <= r && flag2){
    				//printf("\t%lld\n", j);
    				if(height[j] > k){
    					i = j+1;
    					j++;
    					power = 0;
    					subk = 0;
    					subk_i = -1;
    				}
    				else{
    					power += 2*(total_sum[j - 1] - total_sum[i - 1] + (j - i)*height[j]) + 2*height[j];
    					if(height[j] >= subk){
    						subk = height[j];
    						subk_i = j;
    					}
    					if(power >= x){
    						k = subk;
    						power -= 2*height[i] + 2*(total_sum[j] - total_sum[i] + (j - i)*height[i]);
    						i++;
    						if(subk_i < i){
    							subk = 0;
    							for(int b = i; b <= j; b++){
    								if(height[b] > subk){
    									subk = height[b];
    									subk_i = b;
    								}
    							}
    						}
    						if(i > j) j=i;
    						else power -= 2*(total_sum[j - 1] - total_sum[i - 1] + (j - i)*height[j]) + 2*height[j];
    					}
    					else{
    						j++;
    					}
    					if(j > r && power < x){
    						flag1 = false;
    						flag2 = false;
    						j--;
    					}
    				}
    			}
    			power -= 2*height[i] + 2*(total_sum[j] - total_sum[i] + (j - i)*height[i]);
    			i++;
    			if(subk_i < i){
    				subk = 0;
    				for(int b = i; b <= j; b++){
    					if(height[b] > subk){
    						subk = height[b];
    						subk_i = b;
    					}
    				}
    			}
    			while(i <= r){
    				if(power >= x){
    					k = subk;
    				}
    				else break;
    				power -= 2*height[i] + 2*(total_sum[j] - total_sum[i] + (j - i)*height[i]);
    				i++;
    				if(subk_i < i){
    					subk = 0;
    					for(int b = i; b <= j; b++){
    						if(height[b] > subk){
    							subk = height[b];
    							subk_i = b;
    						}
    					}
    				}
    			}
    		
    		if(k == 100000000) k = -1;
    		printf("%lld\n", k);
    	}
    	
    	return 0;
    }
    
  • + 0 comments

    the problem is understanding the problem itself.