1、Codeforces 455A Boredom
参考:http://codeforces.com/contest/455/standings
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int n;
int a[maxn];
int vis[maxn];
ll dp[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
++vis[a[i]];
}
for (int i = 1; i < maxn; ++i) {
dp[i] = (ll)i * vis[i];
if (i >= 2) {
dp[i] += dp[i - 2];
}
if (dp[i - 1] > dp[i]) {
dp[i] = dp[i - 1];
}
}
printf("%I64d\n", dp[maxn-1]);
return 0;
}
2、Codeforces 189A Cut Ribbon
可以暴力
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 4000 + 10;
int n;
int a[10];
int dp[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
scanf("%d", &n);
for (int i = 0; i < 3; ++i) {
scanf("%d", &a[i]);
dp[a[i]] = 1;
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < 3; ++j) {
if (dp[i] && i + a[j] <= n) {
dp[i + a[j]] = max(dp[i + a[j]], dp[i] + 1);
}
}
}
printf("%d\n", dp[n]);
return 0;
}
3、Codeforces 698A Vacations
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int n;
int a[maxn], dp[maxn][10];
int main()
{
#ifdef Floyd
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][2]);
dp[i][2] = max(dp[i - 1][2], dp[i - 1][1]);
if (a[i] == 1) {
dp[i][1] = max(dp[i][1], dp[i - 1][2] + 1);
} else if (a[i] == 2) {
dp[i][2] = max(dp[i][2], dp[i - 1][1] + 1);
} else if (a[i] == 3) {
dp[i][1] = max(dp[i][1], dp[i - 1][2] + 1);
dp[i][2] = max(dp[i][2], dp[i - 1][1] + 1);
}
}
int ans = n - max(dp[n][1], dp[n][2]);
printf("%d\n", ans);
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int n;
int a[maxn], dp[10][maxn];
int main()
{
#ifdef Floyd
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
dp[1][i] = dp[2][i] = INF;
}
for (int i = 1; i <= n; ++i) {
dp[1][i] = dp[2][i] = min(dp[1][i - 1], dp[2][i - 1]) + 1;
if (a[i] == 1) {
dp[1][i] = min(dp[1][i], dp[2][i - 1]);
} else if (a[i] == 2) {
dp[2][i] = min(dp[2][i], dp[1][i - 1]);
} else if (a[i] == 3) {
dp[1][i] = min(dp[1][i], dp[2][i - 1]);
dp[2][i] = min(dp[2][i], dp[1][i - 1]);
}
}
int ans = min(dp[1][n], dp[2][n]);
printf("%d\n", ans);
return 0;
}
4、HDU 5965 扫雷
参考:http://www.itdadao.com/articles/c15a706645p0.html
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
const int mod = 1e8 + 7;
const int maxn = 1e4 + 10;
int T, n;
char s[maxn];
int num[maxn];
int dp[maxn][3][6]; //dp[i][j][k]表示第i列放了j个后剩下k个的种类数
int key[] = {1, 2, 1};
int main() {
#ifdef Floyd
freopen("in.txt", "r", stdin);
#endif // Floyd
scanf("%d", &T);
while (T--) {
scanf("%s", s);
n = strlen(s);
bool flag = true;
for (int i = 0; i < n; ++i) {
num[i + 1] = s[i] - '0';
}
for (int i = 1; i <= n; ++i) {
if ((i == 1 || i == n) && num[i] > 4) {
flag = false;
break;
}
if (num[i] > 6) {
flag = false;
break;
}
}
if (!flag) {
printf("0\n");
} else {
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= 2; ++i) {
if (num[1] - i > 2) {
continue;
}
if (num[1] - i < 0) {
break;
}
dp[1][i][num[1] - i] = key[i];
}
for (int i = 2; i <= n; ++i) {
for (int j = 0; j <= 2; ++j) {
for (int k = 0; k <= 2; ++k) {
if (num[i] - j - k > 2) {
continue;
}
if (num[i] - j - k < 0) {
break;
}
dp[i][k][num[i] - j - k] += key[k] * dp[i - 1][j][k];
dp[i][k][num[i] - j - k] %= mod;
}
}
}
int ans = 0;
for (int i = 0; i <= 2; ++i) {
ans += dp[n][i][0]; ans %= mod;
}
printf("%d\n", ans);
}
}
return 0;
}
5、nyist 16 嵌套矩形
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9
typedef long long ll;
typedef pair<int, int> Pair;
const int INF = 0x7fffffff;
const int maxn = 1e6;
int N, n, a, b;
int head[maxn], Next[maxn], to[maxn], edge;
int ans[1010];
void init(void);
inline void add_edge(int u, int v);
int dfs(int u);
int main() {
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
scanf("%d", &N);
while (N--) {
init();
scanf("%d", &n);
vector<Pair> v;
REP(i, n) {
scanf("%d %d", &a, &b);
if (a > b) { swap(a, b); }
v.push_back(make_pair(a, b));
}
sort(v.begin(), v.end());
REP(i, n - 1) for (int j = i + 1; j < n; ++j) {
if (v[i].first < v[j].first && v[i].second < v[j].second) {
add_edge(j + 1, i + 1);
}
}
memset(ans, 0, sizeof(ans));
int Max = 0;
for (int i = n; i >= 1; --i) {
Max = max(Max, dfs(i));
}
printf("%d\n", Max);
}
#ifdef __AiR_H
printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
return 0;
}
int dfs(int u) {
if (ans[u]) { return ans[u]; }
int Max = 0, v = 0;
for (int i = head[u]; i != -1; i = Next[i]) {
v = to[i];
Max = max(Max, dfs(v));
}
return ans[u] = Max + 1;
}
inline void add_edge(int u, int v) {
to[edge] = v; Next[edge] = head[u]; head[u] = edge++;
}
void init(void) {
memset(head, -1, sizeof(head));
edge = 0;
}