一、Counting Kangaroos is Fun
There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.
Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.
The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.
Input
The first line contains a single integer — n (1 ≤ n ≤ 5·105). Each of the next n lines contains an integer si — the size of the i-th kangaroo (1 ≤ si ≤ 105).
Output
Output a single integer — the optimal number of visible kangaroos.
Examples
Input
8 2 5 7 6 9 8 4 2
Output
5
Input
8 9 1 6 2 6 5 8 3
Output
5
题意:大袋鼠可以把小于自己1/2的袋鼠装进自己的袋子里,且只能装一个,问最少能看见几只袋鼠。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int mapp[600000];
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
{
scanf("%d", &mapp[i]);
}
sort(mapp, mapp+n);
int m = n-1;
int ans = n;
for(int i=n/2-1; i>=0; i--)//最少能看见的袋鼠数量是n/2, 所以从袋鼠的中间分成两段,前一段的最后与后一段的最后比较,满足条件就把前后两段的位置都移向前一只袋鼠,否则,只把前一段的位置移向前一只;
{
if(mapp[i]*2<=mapp[m])
{
ans--;
m--;
}
}
cout<<ans<<endl;
}
}
二、Geometric Progression
Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.
He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.
A subsequence of length three is a combination of three such indexes i1, i2, i3, that 1 ≤ i1 < i2 < i3 ≤ n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.
A geometric progression with common ratio k is a sequence of numbers of the form b·k0, b·k1, ..., b·kr - 1.
Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.
Input
The first line of the input contains two integers, n and k (1 ≤ n, k ≤ 2·105), showing how many numbers Polycarp's sequence has and his favorite number.
The second line contains n integers a1, a2, ..., an( - 109 ≤ ai ≤ 109) — elements of the sequence.
Output
Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.
Examples
Input
5 2 1 1 2 2 4
Output
4
Input
3 1 1 1 1
Output
1
Input
10 3 1 2 6 2 3 6 9 18 3 9
Output
6
Note
In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
题意:输入n个数,然后给出一个数k(代表的是等比数列中的那个公比),然后第二行给出n个数,代表的是这个序列。
出在这个序列中满足公比为k的三个数有几种。并输出方案总数。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
map<int , ll>s1, s2;
int n, k;
int main()
{
while(scanf("%d%d", &n, &k)!=EOF)
{
ll ans = 0;
for(int i=0; i<n; i++)
{
int x;
scanf("%d", &x);
if(x%k==0)
{
ans+=s2[x/k];//先更新连续3个等比的值;
s2[x]+=s1[x/k];//再更新连续2个等比的值;
}
s1[x]++;//最后更新不需要整除条件的第一个值
}
printf("%lld\n", ans);
}
return 0;
}
三、Game 23
Polycarp plays "Game 23". Initially he has a number nn and his goal is to transform it to mm. In one move, he can multiply nn by 22 or multiply nnby 33. He can perform any number of moves.
Print the number of moves needed to transform nnto mm. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform nnto mm contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).
Input
The only line of the input contains two integers nn and mm (1≤n≤m≤5⋅1081≤n≤m≤5⋅108).
Output
Print the number of moves to transform nn to mm, or -1 if there is no solution.
Examples
Input
120 51840
Output
7
Input
42 42
Output
0
Input
48 72
Output
-1
Note
In the first example, the possible sequence of moves is: 120→240→720→1440→4320→12960→25920→51840.120→240→720→1440→4320→12960→25920→51840.The are 77 steps in total.
In the second example, no moves are needed. Thus, the answer is 00.
In the third example, it is impossible to transform 4848 to 7272.
题意:输入n, m两个正整数,n只能通过乘以2或者乘以3得到m,输出最少几次操作才可以;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int n, m, sum;
int main()
{
while(scanf("%d%d",&n, &m)!=EOF)
{
sum = 0;
int x = m/n;
int y = m%n;
if(y!=0)
printf("-1\n");
else if(n==m)
printf("0\n");
else
{
int flag = 0;
while(x>1)
{
if(x%3==0)
{
x = x/3;
sum++;
}
else if(x%2==0)
{
x = x/2;
sum++;
}
else
{
flag = 1;
break;
}
}
if(flag==0)
printf("%d\n", sum);
else
printf("-1\n");
}
}
return 0;
}
四、MUH - Sticks
Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way:
- Four sticks represent the animal's legs, these sticks should have the same length.
- Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks.
Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it.
Input
The single line contains six space-separated integers li (1 ≤ li ≤ 9) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks.
Output
If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes).
Examples
Input
4 2 5 4 4 4
Output
Bear
Input
4 4 5 4 4 5
Output
Elephant
Input
1 2 3 4 5 6
Output
Alien
Note
If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue.
题意:给你六根木棍的长度,熊需要头比身体短,大象需要头和身体一样,四肢要一样长,否则就是外星人。请你判断能组成哪一个。一共有六根木棍,如果有: 4个长度一样 && 2个长度一样(长度不同于之前四个) || 6个长度一样 即为大象, 如果有:5个长度一样 || 4个长度一样 即为熊, 如果以上两个条件都不满足:即为外星人;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int x, vis, vis2, vise;
int flag[10];
int main()
{
for(int i=0; i<6; i++)
{
scanf("%d", &x);
flag[x]++;
}
for(int i=1; i<=9; i++)
{
if(flag[i]>=4)
{
vis = 1;
flag[i] = flag[i]-4;
i--;
}
else if(flag[i]==2)
{
vis2 = 1;;
}
}
if(vis==1&&vis2==1)
printf("Elephant\n");
else if(vis==1)
printf("Bear\n");
else
printf("Alien\n");
return 0;
}
五、Jzzhu and Children
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm:
- Give m candies to the first child of the line.
- If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home.
- Repeat the first two steps while the line is not empty.
Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
Input
The first line contains two integers n, m (1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains nintegers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output
Output a single integer, representing the number of the last child.
Examples
Input
5 2 1 3 1 4 2
Output
4
Input
6 4 1 1 2 2 3 3
Output
6
Note
Let's consider the first sample.
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.
Child 4 is the last one who goes home.
题意: 输入两个数n, m, 代表n个学生,每次老师只发m块糖果(如果还想要,就去队伍最后再排), 以下n个数分别代表 每个学生想要的糖果数 ,输出最后收到糖果的学生是最开始队伍的第几个。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
using namespace std;
int n, m;
int mapp[110];
int num[110], numm;
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(num, 0, sizeof(num));
for(int i=1; i<=n; i++)
{
scanf("%d", &mapp[i]);
while(mapp[i]>0)
{
mapp[i]-=m;
num[i]++;
}
}
int maxx = 0, numm = 1;
for(int i=1; i<=n; i++)
{
if(num[i]>=maxx)
{
maxx = num[i];
numm = i;
}
}
printf("%d\n", numm);
}
return 0;
}