Codechef December Challenge 2014 Chef and Apple Trees 水题

Chef and Apple Trees

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

Chef has N apple trees in his home garden. Each tree has 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 an unusual method of collecting apples. In a single minute, he can perform the following task:

  • Pick any subset of trees such that every tree in the subset has 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 number of apples on a tree not in the subset.

If all trees have the same number of apples left, 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 you tell him what the minimum time required is?

Input

The first line of the input contains a single integer T denoting the number of test cases. This will be followed by T test cases. The first line of each test case contains a single integer N denoting the number of apple trees in Chef‘s garden. The next line of each 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 single 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 <= 10 , 1 <= N <= 103: (27 pts)
  • Subtask 2 : 1 <= T <= 10 , 1 <= N <= 104: (25 pts)
  • Subtask 3 : 1 <= T <= 10 , 1 <= N <= 105: (48 pts)

Example

Input
2
3
3 3 3
4
1 2 3 3

Output
1
3

Explanation

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

For test 2, there are 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 the last 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>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100001
int a[maxn];
int flag[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--){
    int n;
    scanf("%d",&n);
    int ans=0;
    memset(flag,0,sizeof(flag));
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        if(flag[a[i]]==0)
            ans++;
        flag[a[i]]=1;
    }
    cout<<ans<<endl;
    }
    return 0;
}
时间: 2024-11-08 17:25:56

Codechef December Challenge 2014 Chef and Apple Trees 水题的相关文章

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake

https://www.codechef.com/DEC17/problems/GIT01 #include<cstdio> #include<algorithm> using namespace std; #define N 101 char s[N]; int main() { int T; scanf("%d",&T); int n,m; int OddG,OddR,EvenG,EvenR; int ans; while(T--) { OddG=O

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 100001 int a[N],b[N]; int sum[N],wh[N][2]; int num1[N],num2[N]; void read(int &x) { x=0; char c=getc

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

CodeChef November Challenge 2014

重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: 1 #include <cstdio> 2 #include <iostream> 3 #include <map> 4 #include <cstring> 5 #include <cstdlib> 6 #include <cmath> 7 #include <algorithm> 8 #

Codechef March Challenge 2014——The Street

The Street Problem Code: STREETTA https://www.codechef.com/problems/STREETTA Submit Tweet All submissions for this problem are available. Read problems statements in Mandarin Chineseand Russian. The String street is known as the busiest street in Cod

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Total Diamonds

https://www.codechef.com/DEC17/problems/VK18 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 1000001 long long sum[N*2],dp[N]; int num[11]; void read(int &x) { x=0; char c=getchar(); while(!isdi

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out

https://www.codechef.com/DEC17/problems/CPLAY #include<cstdio> #include<algorithm> using namespace std; char s[21]; int main() { int sumA,sumB; while(scanf("%s",s+1)!=EOF) { sumA=sumB=0; int i; for(i=1;i<=10;++i) { if(i&1) { i

Maximum number, GCD condition (codechef March Challenge 2014)

题目 : http://acm.bnu.edu.cn/v3/problem_show.php?pid=40489 最近做到的一道蛮有意思的题目(codechef现在的题目确实很赞了) 题意 :中文题面 (cc的一大好处就是有中文翻译,嘿嘿) 区间Max = max{a_i|gcd(a_i, g) > 1 && x <= i <= y} 做法 : 一开始我是用分块做的,复杂的O(m * sqrt(n) * C) C 是所含不同素数的个数, C大概最大只有6或7吧 然后裸裸的

[Codechef October Challenge 2014]刷漆

问题描述 Czy做完了所有的回答出了所有的询问,结果是,他因为脑力消耗过大而变得更虚了:).帮助Czy恢复身材的艰巨任务落到了你的肩上. 正巧,你的花园里有一个由N块排成一条直线的木板组成的栅栏,木板从左到右依次标号1到N.这N块木板中,有M块木板前面放着一桶油漆.油漆有不同的颜色,每种颜色可以由一个大写字母表示(A到Z).而你要求Czy用他的油漆刷子给栅栏刷上油漆. 已知Czy会选择一个前方放有油漆桶的木板开始他的任务.刷子蘸上油漆后,他开始随机地沿着栅栏走,他不会走出栅栏的范围.随机地走表示