题目传送:BestCoder Round #42
1、Shaking hands
思路:简单题,ans = n * 2 + 矩阵上的1的个数
AC代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int main() {
int n;
while(scanf("%d", &n) != EOF) {
int ans = n * 2;
int tmp = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j ++) {
int t;
scanf("%d", &t);
tmp += t;
}
}
printf("%d\n", ans + tmp);
}
return 0;
}
2、Gunner II
思路:map+queue模拟过去,存下鸟的位置,然而我比赛的代码TLE了,然后同样的代码在OJ上交过啦,也是醉了,这真的是太不爽了,希望以后BC能有所改进吧,毕竟给中国选手提供了一个比赛环境。
AC代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;
inline int readint()
{
char c = getchar();
while(!isdigit(c)) c = getchar();
int x = 0;
while(isdigit(c))
{
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
int buf[10];
inline void writeint(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p-1; j >= 0; j--) putchar('0' + buf[j]);
}
int main() {
int n, m;
while(scanf("%d %d", &n, &m) != EOF) {
map<int, queue<int> > mp;
for(int i = 1; i <= n; i ++) {
int t;
t = readint();
mp[t].push(i);
}
for(int i = 1; i <= m; i ++) {
int t;
t = readint();
if(mp.find(t) == mp.end() || mp[t].empty()) {
puts("-1");
}
else {
writeint(mp[t].front());
printf("\n");
mp[t].pop();
}
}
}
return 0;
}
3、Happy birthday
思路:dp,dp[i][j][k]表示走到i和j这个位置容量是否可以为k
AC代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int dp[105][105][105];
int a[105][105];
int main() {
int n, m, k;
while(scanf("%d %d %d", &n, &m, &k) != EOF) {
memset(dp, 0, sizeof(dp));
dp[0][0][0] = 1;
for(int i = 1; i <= n; i ++) {
dp[i][0][0] = 1;
}
for(int i = 1; i <= m; i ++) {
dp[0][i][0] = 1;
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
scanf("%d", &a[i][j]);
dp[i][j][0] = 1;
}
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= m; j ++) {
for(int l = 0; l <= k; l ++) {
if(dp[i-1][j][l] == 1 || dp[i][j-1][l] == 1
|| (l >= a[i][j] && dp[i-1][j][l-a[i][j]] == 1)
|| (l >= a[i][j] && dp[i][j-1][l-a[i][j]] == 1)) dp[i][j][l] = 1;
}
}
}
for(int t = k; t >= 0; t --) {
if(dp[n][m][t] == 1) {
printf("%d\n", t);
break;
}
}
}
return 0;
}