题目链接:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=889
题解链接:http://bestcoder.hdu.edu.cn/
一道比较水的模拟
#include
using namespace std;
int main(void)
{
int t, n, m, x, y;
int res, num, k;
scanf("%d", &t);
while (t--) {
res = 0x3f3f3f3f;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
num = m / x;
if (m != num * x)
num++;
k = num * y;
res = min(res, k);
}
printf("%d\n", res);
}
return 0;
}
dp,和01背包有些相似
#include
using namespace std;
int sco[] = {95, 90, 85, 80, 75, 70, 67, 65, 62, 60, 0};
double gpa[] = {4.3, 4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 2.0, 1.7, 1.0, 0};
double dp[5][405];
int main(void)
{
int t, x, cnt;
for (int i = 0; i < 4; i++)
for (int j = 0; j <= 400; j++)
for (int k = 0; k < 11; k++)
if (j >= sco[k])
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - sco[k]] + gpa[k]);
scanf("%d", &t);
while (t--) {
scanf("%d", &x);
printf("%.1lf\n", dp[4][x]);
}
return 0;
}
dp,跟走网格的题有些类似
#include
using namespace std;
const int N = 1005;
int dp[N][N];
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
int main(void)
{
int t, a, b;
for (int i = 1; i <= 1000; i++) {
dp[i][1] = dp[1][i] = i;
}
int flag;
for (int i = 2; i <= 1000; i++) {
for (int j = 2; j <= 1000; j++) {
flag = (gcd(i, j) == 1 ? 1 : 0);
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + flag;
}
}
scanf("%d", &t);
while (t--) {
scanf("%d%d", &a, &b);
printf("%d\n", dp[a][b]);
}
return 0;
}