I can not solve this problem, it does not pass in time.
Vlad and regular k-squares
Time limit 1 second
Memory limit 64Mb
Input standard input or input.txt
Output standard output or output.txtVlad is a perfectionist. Like any perfectionist, he adores regular polygons. Knowing this, his parents presented him with a set consisting of n segments for the 18th anniversary, so that their son could construct any desired polygons. After receiving a gift, Vlad asked himself a question: so how many total correct k-gons can he make using each segment of the set no more than once? Having downcast a little, he addressed to you, the best programmer of the universe, with this task. Help Vlad solve it.
Input format
The first line contains two numbers n (3 ≤ n ≤ 10 6 ) and k (3 ≤ k ≤ n), the number of segments in the set and the number of vertices in the desired polygons, respectively. The second line contains n numbers a i (1 ≤ a i ≤ 10 9 ) separated by spaces, where a i is the length of the i-th segment.Output format
Print one number - the number of regular k-gons that can be composed of a given set of segments, using each no more than once.
Help please, here is my code:
#include <fstream> using namespace std; ifstream in("input.txt"); ofstream out("output.txt"); void selectSort(int *arr, long size) { for (long i = 0; i < size; i++) for (long j = i + 1; j < size; j++) if (arr[j] < arr[i]) swap(arr[i], arr[j]); } int main() { int n, k; in >> n >> k; int arr[n]; for(int i = 0; i < n; i++) in >> arr[i]; selectSort(arr, n); int answer = 0, count = 0; for(int i = 0; i < n - (k - 1); i++) { for(int j = i; j < n; j++) { if(arr[i] == arr[j]) count++; if(count == k) { answer++; i += k - 1; break; } } count = 0; } out << answer; return 0; }
selectionSortshould be replaced with a faster sorting. Since the task itself is not available (I do not want to register) - so far everything I can say. - Harrymapto get the number of identical segments, and then just count the number of records whose value is not less than the required k. Since this is still a competition, I’ll confine myself to an idea :) - Harry