Problem B - Bsueh- and Gold Medals
题意:
多组输入,每组样例先输入两个值n, m:金牌的数量和容器最多可以装多少金牌。我们需要选择一些金牌来填满这个容器。金牌的摆放是叠成塔状的,这要求每个位于下层的金牌面积要比上层的金牌至少大 p, 求最大的p。
知识点:
贪心,二分
题解:
本题可以运用贪心的思想来对结果直接二分求答案,先从较小值的那一端进行查找,若最后找到的值的数量小于容器的容量,chk函数就返回false。
代码:
#include <bits/stdc++.h>
using namespace std;
#define _rep(i,a,b) for( int i = (a); i <= (b); i ++ )
const int N = 1010;
int n, m, ans;
int q[N];
bool chk(int mid) {
ans = 0;
int temp = q[1];
_rep (i, 2, n) {
if(q[i] - temp >= mid) {
temp = q[i];
ans ++;
}
}
if(ans >= m - 1) return true;
else return false;
}
void solve() {
scanf ("%d %d", &n, &m);
_rep (i, 1, n) scanf("%d", &q[i]);
sort(q + 1, q + n + 1);
int l = 0, r = q[n];
while(l < r) {
int mid = l + r + 1 >> 1;
if(chk(mid)) l = mid;
else r = mid - 1;
}
printf("%d\n", l);
}
int main() {
int tt;
cin >> tt;
while(tt -- ) solve();
system ("pause");
return 0;
}
Problem E - Ewo Slices of Bread with Cheese
题意:
输入第一行表示奶酪数量n,第二行输入n个数表示每个奶酪的新鲜度,问你他至少需要多少天吃完这些奶酪,注意他每天最多吃一个奶酪,并且只吃新鲜度为偶数的奶酪,每过一天,所有奶酪的新鲜度减一。
知识点:
模拟
题解:
对奶酪数量分为3种情况,奇数等于、大于或小于偶数的数量,分别模拟每天的消耗量得到公式。
代码:
#include <bits/stdc++.h>
using namespace std;
#define _rep(i,a,b) for( int i = (a); i <= (b); i ++ )
int n;
int q[100010];
int cnt1, cnt2, ans;
int main() {
cin >> n;
_rep (i , 1, n) cin >> q[i];
if(cnt1 == cnt2) ans = n;
else if(cnt2 > cnt1) ans = 2 * cnt1 + 2 * (cnt2 - cnt1) - 1;
else if(cnt1 > cnt2) ans = 2 * cnt2 + (cnt1 - cnt2) * 2;
cout << ans << endl;
// system ("pause");
return 0;
}