A题:poj 2828 Buy Tickets
水题线段树,插空。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Tree {
int l, r, sum;
};
Tree tree[MAXN<<2];
void PushUp(int o) {
tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void Build(int o, int l, int r) {
tree[o].l = l; tree[o].r = r;
tree[o].sum = r - l + 1;
if(l == r) {
return ;
}
int mid = (l + r) >> 1;
Build(ll, l, mid); Build(rr, mid+1, r);
}
int Query(int o, int pos) {
if(tree[o].l == tree[o].r) {
tree[o].sum = 0;
return tree[o].l;
}
int ans;
if(pos < tree[ll].sum) {
ans = Query(ll, pos);
}
else {
ans = Query(rr, pos - tree[ll].sum);
}
PushUp(o);
return ans;
}
int ans[MAXN];
int x[MAXN], y[MAXN];
int main()
{
int n;
while(scanf("%d", &n) != EOF) {
for(int i = 1; i <= n; i++) {
scanf("%d%d", &x[i], &y[i]);
}
Build(1, 1, n);
for(int i = n; i >= 1; i--) {
int pos = Query(1, x[i]);
ans[pos] = y[i];
//Update(1, 1, n, pos);
}
for(int i = 1; i <= n; i++) {
if(i > 1) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
B题:poj 2411 Mondriaan’s Dream
状压dp什么的,我是菜鸡。
C题:poj 1166 The Clocks
题意:翻转时钟,问你怎么翻转才能使得所有时钟全变为12点。
高斯消元会WA死的,还不如暴力划算。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
int a[10], ans[10], v[10];
int main()
{
scanf("%d%d%d%d%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
for(ans[1] = 0; ans[1] <= 3; ans[1]++)
for(ans[2] = 0; ans[2] <= 3; ans[2]++)
for(ans[3] = 0; ans[3] <= 3; ans[3]++)
for(ans[4] = 0; ans[4] <= 3; ans[4]++)
for(ans[5] = 0; ans[5] <= 3; ans[5]++)
for(ans[6] = 0; ans[6] <= 3; ans[6]++)
for(ans[7] = 0; ans[7] <= 3; ans[7]++)
for(ans[8] = 0; ans[8] <= 3; ans[8]++)
for(ans[9] = 0; ans[9] <= 3; ans[9]++) {
v[1] = (a[1] + ans[1] + ans[2] + ans[4]) % 4;
v[2] = (a[2] + ans[1] + ans[2] + ans[3] + ans[5]) % 4;
v[3] = (a[3] + ans[2] + ans[3] + ans[6]) % 4;
v[4] = (a[4] + ans[1] + ans[4] + ans[5] + ans[7]) % 4;
v[5] = (a[5] + ans[1] + ans[3] + ans[5] + ans[7] + ans[9]) % 4;
v[6] = (a[6] + ans[3] + ans[5] + ans[6] + ans[9]) % 4;
v[7] = (a[7] + ans[4] + ans[7] + ans[8]) % 4;
v[8] = (a[8] + ans[5] + ans[7] + ans[8] + ans[9]) % 4;
v[9] = (a[9] + ans[6] + ans[8] + ans[9]) % 4;
if(v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] + v[8] + v[9] == 0) {
int cnt = 0;
for(int i = 1; i <= 9; i++) {
if(ans[i] == 0) continue;
for(int j = 0; j < ans[i]; j++) {
if(cnt) printf(" ");
printf("%d", i);cnt++;
}
}
printf("\n");
return 0;
}
}
return 0;
}
D题:zoj 1372 Networking
果然比手速。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e3 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Edge {
int from, to, val;
};
Edge edge[50*50*10];
bool cmp(Edge a, Edge b) {
return a.val < b.val;
}
int father[60];
int Find(int p){
int t, child = p;
while(p != father[p]) {
p = father[p];
}
while(child != p) {
t = father[child];
father[child] = p;
child = t;
}
return p;
}
void Merge(int x, int y) {
int fx = Find(x);
int fy = Find(y);
if(fx != fy) {
father[fx] = fy;
}
}
int main()
{
int n, m;
while(scanf("%d", &n), n) {
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &edge[i].from, &edge[i].to, &edge[i].val);
}
for(int i = 1; i <= n; i++) father[i] = i;
sort(edge, edge+m, cmp);
int ans = 0;
for(int i = 0; i < m; i++) {
int u = edge[i].from;
int v = edge[i].to;
if(Find(u) == Find(v)) continue;
ans += edge[i].val;
Merge(u, v);
}
printf("%d\n", ans);
}
return 0;
}
E题:hdoj 1260 Tickets
水dp
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e3 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
int dp[MAXN], a[MAXN], b[MAXN];
int main()
{
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
dp[i] = 0;
}
for(int i = 2; i <= n; i++) {
scanf("%d", &b[i]);
}
dp[1] = a[1];
for(int i = 2; i <= n; i++) {
dp[i] = min(dp[i-1] + a[i], dp[i-2] + b[i]);
}
int sum = 28800 + dp[n];
int h = sum / 3600;
int m = sum % 3600 / 60;
int s = sum % 3600 % 60;
if(sum >= 43200) {
if(h > 12) h -= 12;
printf("%02d:%02d:%02d pm\n", h, m, s);
}
else {
printf("%02d:%02d:%02d am\n", h, m, s);
}
}
return 0;
}
题意:求解f[n] - n^3 = f[n-2] - (n-3)^3。
打表试试水。循环节4018。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL pow_mod(LL a, int n) {
LL ans = 1LL;
while(n) {
if(n & 1) {
ans = ans * a % MOD;
}
a = a * a % MOD;
n >>= 1;
}
return ans;
}
LL f[101001];
void get() {
f[1] = 1; f[2] = 7;
LL p1 = 7, p2;
for(int i = 3; i <= 4020; i++) {
f[i] = (f[i-2] - pow_mod(i-1, 3) + pow_mod(i, 3) + MOD) % MOD;
// p2 = f[i];
// if(p1 == 1 && p2 == 7) {
// printf("%d\n", i);
// break;
// }
// p1 = p2;
}
//cout << f[4019] << ' ' << f[4020] << endl;
//i = 4020
}
int main()
{
get();
int n;
while(scanf("%d", &n), n) {
printf("%lld\n", f[n%4018]);
}
return 0;
}
题意:迷宫有门有钥匙,问你能否出去。
思路:状压记录好结果就是BFS了。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Node {
int x, y, step, key;
friend bool operator < (Node a, Node b) {
return a.step > b.step;
}
};
bool vis[110][110][1<<4];
int n, m;
char Map[110][110];
bool judge(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != '#';
}
int Move[4][2] = {0,1, 0,-1, 1,0, -1,0};
bool Door(char op) {
return op >= 'A' && op <= 'Z' && op != 'X';
}
bool Key(char op) {
return op >= 'a' && op <= 'z';
}
int val(char op) {
if(op == 'B' || op == 'b') return 0;
if(op == 'Y' || op == 'y') return 1;
if(op == 'R' || op == 'r') return 2;
if(op == 'G' || op == 'g') return 3;
}
void BFS(int x, int y) {
CLR(vis, false);
Node now, next;
now.x = x, now.y = y, now.step = 0, now.key = 0;
vis[x][y][now.key] = true; Map[x][y] = '.';
priority_queue<Node> Q; Q.push(now);
while(!Q.empty())
{
now = Q.top(); Q.pop();
//cout << now.x << ' ' << now.y << endl;
if(Map[now.x][now.y] == 'X') {
printf("Escape possible in %d steps.\n", now.step);
return ;
}
for(int k = 0; k < 4; k++) {
next.x = now.x + Move[k][0];
next.y = now.y + Move[k][1];
next.step = now.step + 1;
next.key = now.key;
if(judge(next.x, next.y)) {
char op = Map[next.x][next.y];
if(Door(op)) {
if(next.key & (1 << val(op))) {
if(!vis[next.x][next.y][next.key]) {
vis[next.x][next.y][next.key] = true;
Q.push(next);
}
}
}
else if(Key(op)) {
next.key |= (1 << val(op));
if(!vis[next.x][next.y][next.key]) {
vis[next.x][next.y][next.key] = true;
Q.push(next);
}
}
else if(op == '.' || op == 'X') {
if(!vis[next.x][next.y][next.key]) {
vis[next.x][next.y][next.key] = true;
Q.push(next);
}
}
}
}
}
printf("The poor student is trapped!\n");
}
int main()
{
while(scanf("%d%d", &n, &m), n||m) {
int sx, sy;
for(int i = 0; i < n; i++) {
scanf("%s", Map[i]);
for(int j = 0; j < m; j++) {
if(Map[i][j] == '*') {
sx = i;
sy = j;
}
}
}
BFS(sx, sy);
}
return 0;
}
H题:zoj 2797 106 miles to Chicago
题意:最大安全概率。最短路。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
double dist[110][110];
int n, m;
void Floyd() {
for(int k = 1; k <= n; k++) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
dist[i][j] = max(dist[i][j], dist[i][k] * dist[k][j]);
}
}
}
}
int main()
{
while(scanf("%d", &n), n) {
scanf("%d", &m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
dist[i][j] = 0;
}
}
for(int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
dist[u][v] = dist[v][u] = w * 1.0 / 100;
}
Floyd();
printf("%.6lf percent\n", dist[1][n]*100);
}
return 0;
}
I题:hdoj 2626 Samuel’s Schedule
纯模拟。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
int main()
{
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
int day = 26 - n;
printf("Samuel will ");
if(day == 13) {
printf("take test.\n");
}
else if(day == 14) {
printf("go home.\n");
}
else if(day == 15 || day == 18 || day == 23 || day == 28 || day == 31) {
printf("stay at home.\n");
}
else if(day == 16) {
printf("buy clothes.\n");
}
else if(day == 17) {
printf("buy food.\n");
}
else if(day == 19) {
printf("visit teachers.\n");
}
else if(day == 20) {
printf("go to KTV.\n");
}
else if(day == 21) {
printf("meet friends.\n");
}
else if(day == 22) {
printf("go to movies.\n");
}
else if(day == 24 || day == 25) {
printf("decorate.\n");
}
else if(day == 26) {
printf("go out.\n");
}
else if(day == 27) {
printf("be in the park.\n");
}
else if(day == 29 || day == 30) {
printf("visit relative.\n");
}
else if(day == 32) {
printf("prepare dinner.\n");
}
}
return 0;
}
J题:lightoj 1044 Palindrome Partitioning
题意:让你分割字符串,每次只能分割回文串。问最少分割的次数。
思路:区间dp直接搞会T到死。时间复杂度O(n^3)。
设置dp[i]为1-i的最优解,那么转移为dp[i] = min(dp[j-1]+1),j-i回文。这样时间复杂度O(n^2)。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 2*1e5 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 2009;
void add(LL &x, LL y) { x += y; x %= MOD; }
char str[1010];
int dp[1010], match[1010][1010];
bool judge(int i, int j) {
if(match[i][j] != -1) return match[i][j];
if(i > j) return match[i][j] = 0;
if(i == j) return match[i][j] = 1;
if(i + 1 == j) {
if(str[i] == str[j]) return match[i][j] = 1;
else return match[i][j] = 0;
}
if(str[i] != str[j]) return match[i][j] = 0;
else return match[i][j] = judge(i+1, j-1);
}
int DFS(int pos) {
if(dp[pos] != -1) return dp[pos];
if(pos == 0) return 0;
dp[pos] = pos;
for(int i = 1; i <= pos; i++) {
if(str[i] == str[pos] && judge(i, pos)) {
dp[pos] = min(dp[pos], DFS(i-1) + 1);
}
}
return dp[pos];
}
int main()
{
int t, kcase = 1; scanf("%d", &t);
while(t--) {
scanf("%s", str+1); int len = strlen(str+1);
CLR(dp, -1); CLR(match, -1);
printf("Case %d: %d\n", kcase++, DFS(len));
}
return 0;
}