A:把第一个字符串预处理一下,记录字母,和每个字母的出现次数,然后在第一个字符串枚举开始的位置,然后和第二个字符串进行比较,对于第一个字母,要特殊处理,因为可能是从该字母的第x个位置开始算,这样时间复杂度为O(n^2)
B:简单的树状数组应用,每次取出当前第x + 1大的数字即可
C:第一问:答案为(度数为1的结点+1)/ 2,第二问,其实也就是贪心,每次都尽量找最长链,看这个子树有x条链,答案就加上(x + 1) / 2
D:先BFS预处理出宝藏(起点也当成一个宝藏)之间的最短路,然后进行状压DP,dp[i][j]表示拥有宝藏的状态i和当前在j宝藏的最小代价,最后在枚举一下记录答案即可,有个坑就是一开始起点可以是墙,答案是-1
E:线段树处理面积并的模板题。
F:这题数据小,记忆化广搜一下即可,d[i][j][2]表示,左岸i只羊j只狼,0表示当前在左岸,1表示当前在右岸,然后进行状态转移,注意细节比较多,比如如果羊数量是0,那么狼有几只都没问题
G:这题数据大了,不能搜了,就直接贪心,对于n <= 3还是去搜就行了,这时候m大于6的就没答案了,所以可以搜,然后对于n > 3时,在羊和狼都比较多的时候,可以每次运n只过去(狼羊对半开),然后运一对羊狼回来,直到数量少了,进行贪心,这里贪心分为3类和一种特殊情况(这个一开始没考虑到,靠前一个程序打表发现的- -),3种情况就是n == m时候,要5次,m * 2 <= n时候要1次,在这之间的要3次。特殊情况为:举个例子,羊和狼都有6只,船能运5只,那么第一次可以运3只狼2只羊,然后运1只狼回来,这时候羊只有4只了,就可以尽量多的把羊都运过去,羊变成0了,这样对左岸就不在有影响了,也就是说,船在奇数情况下,能充分利用这多出来的一个位置,答案会更优,这种情况下,直接在多5次就可以了(而不是先常规运一次在判断,这样可能要7次)。
代码:
A:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1005;
char a[N], b[N];
int cnt[N];
void build() {
int n = strlen(a);
int an = 0;
for (int i = 0 ; i < n; i++) {
if (a[i] >= 'a' && a[i] <= 'z') {
a[an] = a[i];
cnt[an] = 1;
if (a[i + 1] == '[') {
int num = 0;
for (int j = i + 2; j < n; j++) {
if (a[j] == ']') {
cnt[an] = num;
i = j;
break;
}
num = num * 10 + a[j] - '0';
}
}
an++;
}
}
a[an] = '\0';
}
bool judge() {
int n = strlen(a);
int m = strlen(b);
for (int i = 0; i < n; i++) {
int x;
if (b[0] != a[i]) continue;
for (x = 0; x < cnt[i] && x < m; x++) {
if (b[x] != a[i])
break;
}
for (int u = i + 1; u < n && x < m; u++) {
int flag = 0;
for (int j = 0; j < cnt[u] && x < m; j++) {
if (b[x] != a[u]) {
flag = 1;
break;
}
x++;
}
if (flag) break;
}
if (x == m)
return true;
}
return false;
}
int main() {
while (~scanf("%s%s", a, b)) {
build();
if (judge()) printf("True\n");
else printf("False\n");
}
return 0;
}
B:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline int lowbit(int x) {
return x & (-x);
}
const int N = 1005;
int n;
int bit[N];
void add(int x, int v) {
while (x <= n) {
bit[x] += v;
x += lowbit(x);
}
}
int get(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
}
int find(int x) {
int l = 0, r = n;
while (l < r) {
int mid = (l + r) / 2;
if (get(mid) >= x) r = mid;
else l = mid + 1;
}
return r;
}
int main() {
while (~scanf("%d", &n)) {
memset(bit, 0, sizeof(bit));
for (int i = 1; i <= n; i++)
add(i, 1);
int x;
int bo = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
int tmp = find(x + 1);
if (bo) printf(" ");
else bo = 1;
printf("%d", tmp);
add(tmp, -1);
}
printf("\n");
}
return 0;
}
C:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 100005;
int n, f, du[N], ans;
vector<int> g[N];
void dfs(int u, int p, int flag) {
int sum = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (v == p) continue;
sum++;
dfs(v, u, 1);
}
ans += (sum - flag + 1) / 2;
}
int t;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
ans = 0;
for (int i = 0; i <= n - 1; i++) {
g[i].clear();
du[i] = 0;
}
for (int i = 1; i <= n - 1; i++) {
scanf("%d", &f);
du[f]++;
du[i]++;
g[f].push_back(i);
g[i].push_back(f);
}
int cnt = 0;
for (int i = 0; i <= n - 1; i++)
if (du[i] == 1) cnt++;
if (n == 1) cnt = 0;
else cnt = (cnt + 1) / 2;
dfs(0, -1, 0);
printf("%d %d\n", cnt, ans);
}
return 0;
}
D:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;
const int d[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n, m, G[N][N], g[15][15];
int x[15], y[15], cnt;
struct State {
int x, y;
State() {}
State(int x, int y) {
this->x = x;
this->y = y;
}
};
int dis[105][105], vis[105][105];
void build(int u) {
queue<State> Q;
Q.push(State(x[u], y[u]));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
for(int j = 0; j < m; j++) dis[i][j] = INF;
dis[x[u]][y[u]] = 0;
vis[x[u]][y[u]] = 1;
while (!Q.empty()) {
State uu = Q.front();
Q.pop();
vis[uu.x][uu.y] = 0;
for (int i = 0; i < 4; i++) {
State v;
v.x = uu.x + d[i][0];
v.y = uu.y + d[i][1];
if (v.x < 0 || v.x > n || v.y <0 || v.y> m || G[v.x][v.y] < 0) continue;
if (dis[v.x][v.y] > dis[uu.x][uu.y] + 1) {
dis[v.x][v.y] = dis[uu.x][uu.y] + 1;
if (!vis[v.x][v.y]) {
vis[v.x][v.y] = 1;
Q.push(v);
}
}
}
}
for (int i = 1; i <= cnt; i++)
g[u][i] = dis[x[i]][y[i]];
}
int dp[2100][15];
int main() {
while (~scanf("%d%d", &n, &m)) {
cnt = 0;
int cao = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
scanf("%d", &G[i][j]);
if (i == 0 && j == 0) {
if (G[i][j] < 0) {
cao = 1;
}
}
if ((i == 0 && j == 0) || G[i][j] > 0) {
++cnt;
x[cnt] = i;
y[cnt] = j;
G[i][j] = cnt;
}
}
if (cao == 1) {
printf("-1\n");
continue;
}
for (int i = 1; i <= cnt; i++) {
for (int j = 1; j <= cnt; j++)
g[i][j] = INF;
}
for (int i = 1; i <= cnt; i++)
build(i);
dp[1][0] = 0;
for (int i = 1; i < (1<<cnt); i++) {
for (int j = 0; j < cnt; j++) {
if (i == 1 && j == 0)continue;
dp[i][j] = INF;
if (i&(1<<j)) {
for (int k = 0; k < cnt; k++) {
if (j == k) continue;
if (g[j + 1][k + 1] == INF) continue;
if (i&(1<<k))
dp[i][j] = min(dp[i][j], dp[i^(1<<j)][k] + g[k + 1][j + 1]);
}
}
}
}
int ans = INF;
for (int i = 1; i <= cnt; i++) {
ans = min(ans, dp[(1<<cnt) - 1][i - 1] + g[1][i]);
}
if (ans == INF) ans = -1;
printf("%d\n", ans);
}
return 0;
}
E:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 100005;
typedef long long ll;
int t, n;
struct Line {
int l, r, y, flag;
Line() {}
Line(int l, int r, int y, int flag) {
this->l = l; this->r = r;
this->y = y; this->flag = flag;
}
} line[N * 2];
bool cmp(Line a, Line b) {
return a.y < b.y;
}
int Hash[N * 2], ln, hn;
int find(int x) {
return lower_bound(Hash, Hash + hn, x) - Hash;
}
#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)
struct Node {
int l, r, cover, len[3];
void init(int l, int r) {
this->l = l; this->r = r;
cover = 0;
memset(len, 0, sizeof(len));
}
} node[N * 8];
void build(int l, int r, int x = 0) {
node[x].init(l, r);
node[x].len[0] = Hash[r + 1] - Hash[l];
if (l == r) return;
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
}
void pushup(int x) {
memset(node[x].len, 0, sizeof(node[x].len));
if (node[x].l == node[x].r) {
node[x].len[min(2, node[x].cover)] = Hash[node[x].r + 1] - Hash[node[x].l];
return;
}
for (int i = 0; i <= 2; i++)
node[x].len[min(2, node[x].cover + i)] += (node[lson(x)].len[i] + node[rson(x)].len[i]);
}
void add(int l, int r, int v, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
node[x].cover += v;
pushup(x);
return;
}
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
}
ll solve() {
ln = hn = 0;
int x1, y1, x2, y2;
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
line[ln++] = Line(x1, x2, y1, 1);
line[ln++] = Line(x1, x2, y2, -1);
Hash[hn++] = x1; Hash[hn++] = x2;
}
sort(line, line + ln, cmp);
sort(Hash, Hash + hn);
hn = unique(Hash, Hash + hn) - Hash;
ll ans = 0;
build(0, hn - 2);
for (int i = 0; i < ln; i++) {
if (i) ans += (ll)node[0].len[1] * (ll)(line[i].y - line[i - 1].y);
add(find(line[i].l), find(line[i].r) - 1, line[i].flag);
}
return ans;
}
int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
printf("Case %d: %I64d\n", ++cas, solve());
}
return 0;
}
F:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 205;
int x, y, n;
bool vis[N][N][2];
struct State {
int x, y, flag, d;
State() {}
State(int x, int y, int flag, int d) {
this->x = x;
this->y = y;
this->flag = flag;
this->d = d;
}
};
int solve() {
if (y > x && x) return -1;
memset(vis, false, sizeof(vis));
queue<State> Q;
vis[x][y][0] = true;
Q.push(State(x, y, 0, 0));
while (!Q.empty()) {
State u = Q.front();
Q.pop();
if (u.x == 0 && u.y == 0) return u.d;
int yang[2], lang[2];
yang[0] = u.x; yang[1] = x - u.x;
lang[0] = u.y; lang[1] = y - u.y;
for (int i = 0; i <= n && i <= lang[u.flag]; i++) {
for (int j = 0; j + i <= n && j <= yang[u.flag]; j++) {
if (i + j == 0) continue;
if (lang[u.flag] - i > yang[u.flag] - j && yang[u.flag] - j) continue;
if (lang[!u.flag] + i > yang[!u.flag] + j && yang[!u.flag] + j) continue;
if (i > j && j) continue;
State v;
if (u.flag) v.x = yang[0] + j, v.y = lang[0] + i;
else v.x = yang[0] - j, v.y = lang[0] - i;
v.flag = !u.flag;
v.d = u.d + 1;
if (vis[v.x][v.y][v.flag]) continue;
vis[v.x][v.y][v.flag] = true;
Q.push(State(v.x, v.y, v.flag, v.d));
}
}
}
return -1;
}
int main() {
while (~scanf("%d%d%d", &x, &y, &n)) {
printf("%d\n", solve());
}
return 0;
}
G:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 205;
int x, y, m, n;
bool vis[N][N][2];
struct State {
int x, y, flag, d;
State() {}
State(int x, int y, int flag, int d) {
this->x = x;
this->y = y;
this->flag = flag;
this->d = d;
}
};
int solve() {
if (x > 20) return -1;
if (y > x && x) return -1;
memset(vis, false, sizeof(vis));
queue<State> Q;
vis[x][y][0] = true;
Q.push(State(x, y, 0, 0));
while (!Q.empty()) {
State u = Q.front();
Q.pop();
if (u.x == 0 && u.y == 0) return u.d;
int yang[2], lang[2];
yang[0] = u.x; yang[1] = x - u.x;
lang[0] = u.y; lang[1] = y - u.y;
for (int i = 0; i <= n && i <= lang[u.flag]; i++) {
for (int j = 0; j + i <= n && j <= yang[u.flag]; j++) {
if (i + j == 0) continue;
if (lang[u.flag] - i > yang[u.flag] - j && yang[u.flag] - j) continue;
if (lang[!u.flag] + i > yang[!u.flag] + j && yang[!u.flag] + j) continue;
if (i > j && j) continue;
State v;
if (u.flag) v.x = yang[0] + j, v.y = lang[0] + i;
else v.x = yang[0] - j, v.y = lang[0] - i;
v.flag = !u.flag;
v.d = u.d + 1;
if (vis[v.x][v.y][v.flag]) continue;
vis[v.x][v.y][v.flag] = true;
Q.push(State(v.x, v.y, v.flag, v.d));
}
}
}
return -1;
}
int main() {
while (~scanf("%d%d", &m, &n)) {
if (n <= 3) {
x = y = m;
printf("%d\n", solve());
} else {
int ans = 0;
while (m - ((n / 2) - 1) > n) {
m -= (n / 2 - 1);
ans += 2;
}
if (m > n) {
if (n % 2) {
if (n >= m - n / 2) {
ans += 5;
printf("%d\n", ans);
continue;
}
}
}
while (m > n) {
m -= (n / 2 - 1);
ans += 2;
}
if (m * 2 <= n) ans += 1;
else if (m == n) ans += 5;
else ans += 3;
printf("%d\n", ans);
}
}
return 0;
}