Codechef December Challenge Chef and Apple Trees water problem

Source: Internet
Author: User

Chef and Apple Trees

Chef loves to prepare delicious dishes. This time, Chef have decided to prepare a special dish for you, and needs to gather several apples.

Chef has N apple trees in his home garden. Each tree have a certain (Non-zero) number of apples on it. In order to create his dish, Chef wants to pluck every apple from every tree.

Chef has a unusual method of collecting apples. In a single minute, he can perform the following task:

    • Pick any subset of trees such, the every tree in the subset have the same number of apples.
    • From each tree in the subset, pluck any number of apples, as long as the number of apples left on the tree equals the numb Er of apples on a tree isn't in the subset.

If All trees has the same number of apples left and Chef can pluck all of the apples remaining in a single minute.

Chef does not want to keep you waiting, so wants to achieve this task in the minimum possible time. Can-tell-him what's the minimum time required is?

Input

The first line of the input contains a single integer T denoting the number of the test cases. This is being followed by T test Cases. The first line of all test case contains a single integer N denoting the number of apple trees in Chef's garden. The next line of all test case contains N space separated integers denoting the number of apples on each tree.

Output

For each of the T -Test cases, output a line-the minimum time to pluck all apples from all trees.

Constraints
    • 1 <= T <= 10
    • 1 <= N <= 105
    • 1 <= number of apples on a tree <= 105
Scoring
    • Subtask 1:1 <= T <=, 1 <= N <= 103: (pts)
    • Subtask 2:1 <= T <=, 1 <= N <= 104: (pts)
    • Subtask 3:1 <= T <=, 1 <= N <=: (pts)
Example
Input233 3 341 2 3 3Output13
Explanation

For test 1, Chef can select all the trees and can pluck all the apples in 1 minute.

For Test 2, there is many ways Chef can pluck all of the apples in 3 minutes. Here is one example:

    • First minute:select the third and fourth trees. Pluck 1 apple from the third tree, and 2 apples from the fourth tree.
    • Second Minute:select the Second and third tree. Pluck 1 apple from each tree.
    • Third Minute:select all of the trees and pluck, apple from each tree.
#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 100001intA[MAXN];intFLAG[MAXN];intMain () {intT; CIN>>T;  while(t--){    intN; scanf ("%d",&N); intans=0; memset (Flag,0,sizeof(flag));  for(intI=0; i<n;i++) {scanf ("%d",&A[i]); if(flag[a[i]]==0) ans++; Flag[a[i]]=1; } cout<<ans<<Endl; }    return 0;}

Codechef December Challenge Chef and Apple Trees water problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.