Save the Queen!

  • + 0 comments

    This is my solution in C++:

    #include <cstdio>
    #include <algorithm>
    
    int main(){
    	int n, k;
    	FILE* fin = fopen("save.txt", "rt");
    	fin = stdin;
    	
    	fscanf(fin, "%d %d\n", &n, &k);
    	
    	long double times[k];
    	
    	for(long long i = 0; i < k; i++){
    		fscanf(fin, "%Lf", &times[i]);
    	}
    	
    	std::sort(times, times + k);
    	
    	long long d1;
    	long long i = 0; long long j;
    	long double dif; long double a;
    	long long d = 1;
    	i = k - n;
    	
    	j = i-1;
    	while(j >= 0 && times[j] == times[i]){
    		d++;
    		j--;
    		if(i > 0) i--;
    	}
    	j = k - n + 1;
    	while(j < k && times[j] == times[i]){
    		d++;
    		j++;
    	}
    	long double total = 0;
    	while(times[k - n] >= 0.0000000001){
    		if(i > 0){
    			dif = times[i] - times[i - 1];
    			j = i - 1;
    			while(j >= 0 && times[j] == times[i]){
    				d++;
    				j--;
    				if(i > 0) i--;
    			}
    		}
    		else{
    			dif = times[0];
    			i = 0;
    		}
    		
    		d1 = i + d - k + n;
    
    		if(d > d1 && d < k - 1) a = (times[i+d] - times[i])*d1/(d-d1);
    		else a = dif+1;
    
    		if(!(a < dif)) a = dif;
    		total += a*d/d1;
    		for(j = i; j <= i+d-1; j++){
    			times[j] -= a;
    		}
    		for(j = i+d; j < k; j++){
    			times[j] -= a*d/d1;
    		}
    		
    		j = i+d;
    		while(j < k && times[j] == times[i]){
    			d++;
    			j++;
    		}
    	}
    
    	printf("%.9Lf\n", total);
    	
    	return 0;
    }