1,爱奇艺节目采购(0-1背包)
题目保证n<=3000
我的代码:(通过7/8)
#include <iostream> #include<string.h> using namespace std; int value[1000001], volume[1000001], list[1000001]; int main() { int n = 0, v; cin >> v; memset(list, 0, sizeof(list)); while (cin >> volume[n] >> value[n])n++; for (int i = 0; i < n; i++)for (int j = v; j >= volume[i]; j--) if (list[j] < list[j - volume[i]] + value[i])list[j] = list[j - volume[i]] + value[i]; cout << list[v] << endl; return 0; }
2,切割木材(二分法)
题目保证n<=1000
我的代码:(通过9/10)
#include <iostream>
using namespace std;
int l[1000], n = 0;
int num(int len)
{
int s = 0;
for (int i = 0; i < n; i++)s += l[i] / len;
return s;
}
int main()
{
int k, m = 0;
cin >> k;
while (cin >> l[n])
{
if (m < l[n])m = l[n];
n++;
}
int low = 1, high = m, mid;
while (high>low + 1)
{
mid = (high + low) / 2;
if (num(mid) < k)high = mid;
else low = mid;
}
if (num(high) < k)high--;
if (high>0 && num(high) < k)high--;
cout << high;
return 0;
}
3,吹气球
我的代码:(通过2/5)
#include <iostream> using namespace std; int main() { int l[502], n, s = 0; cin >> n; for (int i = 1; i <= n; i++)cin >> l[i]; l[0] = l[n + 1] = 1; while (n) { int k = 1, m = 100, kk; for (int i = 1; i <= n; i++)if (m > l[i])m = l[i], k = i; kk = k; while (l[kk + 1] == l[kk] && kk<n)kk++; if (l[kk + 1] > l[k - 1])k = kk; s += l[k - 1] * l[k] * l[k + 1]; for (int i = k; i <= n; i++)l[i] = l[i + 1]; n--; } cout << s; return 0; }我是用贪心做的,不知道思路对不对
4,奇鹿