A.250
水题,求一个数字到距离他最近的斐波那契数的距离。
Problem Statement | |||||||||||||
The Fibonacci sequence is defined as follows:
You're given an int N. You want to change N into a Fibonacci number. This change will consist of zero or more steps. In each step, you can either increment or decrement the number you currently have. That is, in each step you can change your current number X either to X+1 or to X-1. Return the smallest number of steps needed to change N into a Fibonacci number. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | N will be between 1 and 1,000,000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
///#define LL __int64
#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898
using namespace std;
const int maxn = 510;
class FibonacciDiv2
{
public:
int find(int N)
{
LL num[45];
int s = 0;
num[0] = 0;
num[1] = 1;
for(int i = 2; i <= 40; i++)
num[i] = num[i-1]+num[i-2];
for(int i = 0; i <= 40; i++)
{
if(num[i] >= N)
{
s = i;
break;
}
}
int Max;
int t = INF;
int tt = INF;
t = num[s]-N;
if(s > 0)
tt = N-num[s-1];
Max = min(t, tt);
return Max;
}
};
B.500
给你n个数字,让你用体积为2^i的盒子把每一个装起来,并且体积相同的盒子可以换一个大盒子。求出最后的盒子最小为多大。我是当做2048来做的,枚举前两个,然后去掉前两个把新的体积加进来,然后sort再枚举前两个。。。
Problem Statement | |||||||||||||
Today is Fox Ciel's birthday. You want to give her a box of candies as a present. You have candies of several different types. You are given a int[] candyCounts. Each element ofcandyCounts is the number of candies of one particular type. For each non-negative integer i, you have an unlimited supply of boxes with volume 2^i. That is, you have boxes with volume 1, 2, 4, 8, and so on. You are going to pack the candies into boxes. Each type of candies has to be packed into a single box, and you have to use different boxes for different types of candy. The volume of a box must be greater than or equal to the number of candies it contains. Once you have each type of candies in a box, you want to pack those boxes into larger boxes, until only one box remains. You can only pack two boxes at a time. That is, you can take any two boxes you currently have, get a new box, and put the two old boxes into the new box. This is possible if and only if the volume of the new box is greater than or equal to the sum of volumes of the two old boxes. You always get to choose which two boxes you want to pack together, and how large the new box should be. To summarize:
Compute and return the smallest possible volume of the box obtained at the end of packing. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | You may assume that the return value always fits into a signed 32-bit integer variable. | ||||||||||||
Constraints | |||||||||||||
- | candyCounts will contain between 1 and 100 elements, inclusive. | ||||||||||||
- | Each element of candyCounts will be between 1 and 1000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
///#define LL __int64
#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898
using namespace std;
const int maxn = 510;
class Subsets
{
vector<int> num
public:
int findSubset(vector <int> numbers)
{
int num[1010];
int f[1010];
int n = candyCounts.size();
for(int i = 0; i < n; i++)
num[i] = candyCounts[i];
for(int i = 0; i < n ; i++)
{
for(int j = 0; j <= 10; j++)
{
if((1<<j) >= num[i])
{
f[i] = (1<<j);
break;
}
}
}
while(1)
{
sort(f, f+n);
if(n == 1)
{
break;
}
int s = (max(f[0], f[1]))*2;
num[0] = s;
for(int i = 2; i < n; i++)
num[i-1] = f[i];
n--;
for(int i = 0; i < n; i++)
f[i] = num[i];
}
return f[0];
}
};
C.1000
给你n个数字,让你求出这里面有多少种组合可以使得,这几个数的和大于他们的乘积。
先sort排序然后dfs一遍,注意相同的数字的处理。
Problem Statement | |||||||||||||
You have a bag with some balls. There is a positive integer written on each of the balls. Balls with the same integer are identical. A bag with balls is nice if the sum of numbers on all balls is strictly greater than the product of those numbers. For example, if the numbers on balls are {1,1,2,3}, the bag is nice because 1+1+2+3 > 1*1*2*3. You are given a vector <int> numbers. Each element of numbers is a number written on one of the balls in your bag. You are going to remove some (possibly none, but not all) balls from the bag. After you do so, the bag must be nice. Return the number of different nice bags you can obtain. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | You may assume that the return value always fits into a signed 32-bit integer variable. | ||||||||||||
Constraints | |||||||||||||
- | numbers will contain between 1 and 1000 elements, inclusive. | ||||||||||||
- | Each element of numbers will be between 1 and 1000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
| |||||||||||||
5) | |||||||||||||
| |||||||||||||
6) | |||||||||||||
|
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
///#define LL __int64
#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898
using namespace std;
const int maxn = 510;
class Subsets
{
vector<int> num;
int n;
int ans;
void dfs(int x, int sum, int pro)
{
if(x == n)
return;
for(int i = x; i < n; i++)
{
if((x == i || num[i] != num[i-1]) && sum+num[i] > pro*num[i])
{
ans ++;
dfs(i+1, sum+num[i], pro*num[i]);
}
}
}
public:
int findSubset(vector <int> numbers)
{
num = numbers;
n = num.size();
ans = 0;
sort(num.begin(), num.end());
dfs(1, num[0], num[0]);
return ans;
}
};