[ZJOI2007]矩阵游戏 题解
题目地址:洛谷:【P1129】[ZJOI2007]矩阵游戏 – 洛谷、BZOJ …
May all the beauty be blessed.
题目地址:洛谷:【P2698】[USACO12MAR]花盆Flowerpot – 洛谷
老板需要你帮忙浇花。给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置。
每滴水以每秒1个单位长度的速度下落。你需要把花盆放在x轴上的某个位置,使得从被花盆接着的第1滴水开始,到被花盆接着的最后1滴水结束,之间的时间差至少为D。
我们认为,只要水滴落到x轴上,与花盆的边沿对齐,就认为被接住。给出N滴水的坐标和D的大小,请算出最小的花盆的宽度W。
输入格式:
第一行2个整数 N 和 D。
第2.. N+1行每行2个整数,表示水滴的坐标(x,y)。
输出格式:
仅一行1个整数,表示最小的花盆的宽度。如果无法构造出足够宽的花盆,使得在D单位的时间接住满足要求的水滴,则输出-1。
输入样例#1:
4 5 6 3 2 4 4 10 12 15
输出样例#1:
2
【样例解释】
有4滴水, (6,3), (2,4), (4,10), (12,15).水滴必须用至少5秒时间落入花盆。花盆的宽度为2是必须且足够的。把花盆放在x=4..6的位置,它可以接到1和3水滴, 之间的时间差为10-3 = 7满足条件。
【数据范围】
40%的数据:1 ≤ N ≤ 1000,1 ≤ D ≤ 2000;
100%的数据:1 ≤ N ≤ 100000,1 ≤ D ≤ 1000000,0≤x,y≤10^6。
按照x对水滴排个序,维护一个单增和单降的单调队列,每次更新就从队首弹元素弹到刚好大于D,然后用队首队尾的元素x之差更新答案即可。
// Code by KSkun, 2018/5
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF
: *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(!isdigit(c)) {
if(c == '-') neg = -1;
c = fgc();
}
while(isdigit(c)) {
res = (res << 1) + (res << 3) + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 100005;
int n, d;
int que[MAXN], ql, qr;
struct Node {
int x, y;
} drop[MAXN];
inline bool cmp(Node a, Node b) {
return a.x < b.x;
}
int main() {
n = readint(); d = readint();
for(int i = 1; i <= n; i++) {
drop[i].x = readint(); drop[i].y = readint();
}
std::sort(drop + 1, drop + n + 1, cmp);
int ans = 1e9;
ql = qr = 0;
for(int i = 1; i <= n; i++) {
while(ql < qr && drop[que[qr - 1]].y <= drop[i].y) qr--;
que[qr++] = i;
while(ql < qr && drop[que[ql + 1]].y - drop[que[qr - 1]].y >= d) ql++;
if(drop[que[ql]].y - drop[que[qr - 1]].y >= d)
ans = std::min(ans, drop[que[qr - 1]].x - drop[que[ql]].x);
}
ql = qr = 0;
for(int i = 1; i <= n; i++) {
while(ql < qr && drop[que[qr - 1]].y >= drop[i].y) qr--;
que[qr++] = i;
while(ql < qr && drop[que[qr - 1]].y - drop[que[ql + 1]].y >= d) ql++;
if(drop[que[qr - 1]].y - drop[que[ql]].y >= d) ans =
std::min(ans, drop[que[qr - 1]].x - drop[que[ql]].x);
}
printf("%d", ans == 1e9 ? -1 : ans);
return 0;
}
题目地址:洛谷:【P2153】[SDOI2009]晨跑 – 洛谷、BZOJ:Problem 1877. — [SDOI2009]晨跑
Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑、仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑。现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街道,Elaxia只能从 一个十字路口跑向另外一个十字路口,街道之间只在十字路口处相交。Elaxia每天从寝室出发 跑到学校,保证寝室编号为1,学校编号为N。 Elaxia的晨跑计划是按周期(包含若干天)进行的,由于他不喜欢走重复的路线,所以 在一个周期内,每天的晨跑路线都不会相交(在十字路口处),寝室和学校不算十字路口。Elaxia耐力不太好,他希望在一个周期内跑的路程尽量短,但是又希望训练周期包含的天 数尽量长。 除了练空手道,Elaxia其他时间都花在了学习和找MM上面,所有他想请你帮忙为他设计 一套满足他要求的晨跑计划。
存在 1→n 的边存在。这种情况下,这条边只能走一次。
输入格式:
第一行:两个数N,M。表示十字路口数和街道数。 接下来M行,每行3个数a,b,c,表示路口a和路口b之间有条长度为c的街道(单向)。
输出格式:
两个数,第一个数为最长周期的天数,第二个数为满足最长天数的条件下最短的路程长 度。
输入样例#1:
7 10 1 2 1 1 3 1 2 4 1 3 4 1 4 5 1 4 6 1 2 5 5 3 6 6 5 7 1 6 7 1
输出样例#1:
2 11
对于30%的数据,N ≤ 20,M ≤ 120。
对于100%的数据,N ≤ 200,M ≤ 20000。
打个MCMF板子练练手。
最小费用最大流的裸题,对每个点拆点限制流量为1即可,天数就是最大流,路程长度就是费用。注意1和n两个点不能拆点,加边的时候特判一下。
// Code by KSkun, 2018/5
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <queue>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(!isdigit(c)) {
if(c == '-') neg = -1;
c = fgc();
}
while(isdigit(c)) {
res = (res << 1) + (res << 3) + c - '0';
c = fgc();
}
return res * neg;
}
const int MAXN = 100005, INF = 1e9;
struct Edge {
int to, cap, cost, nxt;
} gra[MAXN << 1];
int head[MAXN], tot;
inline void addedge(int u, int v, int cap, int cost) {
gra[tot] = Edge {v, cap, cost, head[u]}; head[u] = tot++;
gra[tot] = Edge {u, 0, -cost, head[v]}; head[v] = tot++;
}
int n, m;
int dis[MAXN], f[MAXN], pre[MAXN], pree[MAXN];
std::queue<int> que;
bool inque[MAXN];
inline bool spfa(int s, int t) {
memset(dis, 0x3f, sizeof(dis));
memset(f, 0, sizeof(f));
dis[s] = 0; f[s] = INF; inque[s] = true; que.push(s);
while(!que.empty()) {
int u = que.front(); que.pop(); inque[u] = false;
for(int i = head[u]; ~i; i = gra[i].nxt) {
int v = gra[i].to;
if(gra[i].cap > 0 && dis[v] > dis[u] + gra[i].cost) {
dis[v] = dis[u] + gra[i].cost;
f[v] = std::min(gra[i].cap, f[u]);
pre[v] = u; pree[v] = i;
if(!inque[v]) {
inque[v] = true; que.push(v);
}
}
}
}
return f[t] != 0;
}
int flow, cost;
inline void mcmf(int s, int t) {
while(spfa(s, t)) {
for(int i = t; i != s; i = pre[i]) {
gra[pree[i]].cap -= f[t];
gra[pree[i] ^ 1].cap += f[t];
}
flow += f[t]; cost += dis[t] * f[t];
}
}
int a, b, c;
int main() {
memset(head, -1, sizeof(head));
n = readint(); m = readint();
for(int i = 2; i < n; i++) {
addedge(i, i + n, 1, 0);
}
for(int i = 1; i <= m; i++) {
a = readint(); b = readint(); c = readint();
if(a == 1) addedge(1, b, 1, c);
else addedge(a + n, b, 1, c);
}
mcmf(1, n);
printf("%d %d", flow, cost);
return 0;
}
题目地址:洛谷:【P1896】[SCOI2005]互不侵犯 – 洛谷、BZOJ:Problem 1087. — [SCOI2005]互不侵犯King
在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。
输入格式:
只有一行,包含两个数N,K ( 1 <=N <=9, 0 <= K <= N * N)
输出格式:
所得的方案数
输入样例#1:
3 2
输出样例#1:
16
听说是状压入门?我不知道,我的第一道状压并不是这个呀。
注意到n的范围极小,意味着可以状压来表示状态,但是状态数量很多,不能全搜一遍,考虑动态规划,只状压一行的状态。dp[i][S][j]表示第i行,状态为S且用去了j个国王的方案数,枚举上一行的状态转移过来即可。
判断是否冲突可以用左移一位、右移一位和原来的状态值去做与运算,若得到的结果非0,说明有冲突。
// Code by KSkun, 2018/5
#include <cstdio>
#include <cctype>
typedef long long LL;
inline char fgc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
inline LL readint() {
register LL res = 0, neg = 1;
register char c = fgc();
while(!isdigit(c)) {
if(c == '-') neg = -1;
c = fgc();
}
while(isdigit(c)) {
res = (res << 1) + (res << 3) + c - '0';
c = fgc();
}
return res * neg;
}
int n, k, cnt[1 << 9];
LL dp[10][1 << 9][85];
bool can[1 << 9];
int main() {
n = readint(); k = readint();
for(int i = 1; i < 1 << n; i++) {
cnt[i] = cnt[i >> 1] + (i & 1);
}
for(int i = 0; i < 1 << n; i++) {
if((i & (i << 1)) || (i & (i >> 1))) continue;
can[i] = true; dp[1][i][cnt[i]] = 1;
}
for(int i = 2; i <= n; i++) {
for(int s = 0; s < 1 << n; s++) {
if(!can[s]) continue;
for(int t = 0; t < 1 << n; t++) {
if(!can[t]) continue;
if((s & t) || (s & (t >> 1)) || (s & (t << 1))) continue;
for(int j = cnt[t]; j <= k; j++) {
dp[i][s][j + cnt[s]] += dp[i - 1][t][j];
}
}
}
}
LL ans = 0;
for(int i = 0; i < 1 << n; i++) {
ans += dp[n][i][k];
}
printf("%lld", ans);
return 0;
}