ACM刷题记录

P1056 [NOIP2008 普及组] 排座椅(排序)
#include <stdio.h>
#include <algorithm>

using namespace std;

typedef struct Line {
    int tag, cnt, idx;
    Line() {}
    Line(int _tag, int _cnt, int _idx) {
        tag = _tag; cnt = _cnt; idx = _idx;
    }
    bool operator < (const Line & line) const {
        return cnt > line.cnt;
    }
}Line;

const int maxn = 1005;
Line pass[maxn * 2];
int M, N, K, L, D;
int x, y, p, q;
int ansl[maxn], ansk[maxn];

int main() {

    scanf("%d%d%d%d%d", &M, &N, &K, &L, &D);
    for (int i = 1; i <= D; i++) {
        scanf("%d%d%d%d", &x, &y, &p, &q);
        if (x == p) {
            int t = min(y, q);
            ansl[t] = ansl[t] + 1;
        } else {
            int t = min(x, p);
            ansk[t] = ansk[t] + 1;
        }
    }
    int tot = 0, l = 0, k = 0;
    for (int i = 1; i <= M; i++) {
        pass[tot++] = Line(0, ansk[i], i);
    }
    for (int i = 1; i <= N; i++) {
        pass[tot++] = Line(1, ansl[i], i);
    }
    sort(pass, pass + tot);
    for (int i = 0; i < tot; i++) {
        if (pass[i].tag == 0 && K > 0) {
            K = K - 1;
            ansk[k++] = pass[i].idx;
        } else if (pass[i].tag == 1 && L > 0) {
            L = L - 1;
            ansl[l++] = pass[i].idx;
        }
    }
    sort(ansk, ansk + k);
    sort(ansl, ansl + l);

    for (int i = 0; i < k; i++) {
        printf("%d%c", ansk[i], i == k - 1 ? '\n' : ' ');
    }
    for (int i = 0; i < l; i++) {
        printf("%d%c", ansl[i], i == l - 1 ? '\n' : ' ');
    }
    return 0;
}
P1101 单词方阵(搜索)
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

const int maxn = 105;
int vis[maxn][maxn], dir[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
char input[maxn][maxn], yizhong[10] = "yizhong";
int n;

void dfs(int startx, int starty) {
    
    for (int i = 0; i < 8; i++) {
        int f = 1;
        int x = startx, y = starty;
        for (int j = 0; j < 7; j++) {
            if (input[x][y] == yizhong[j]) {
                x = x + dir[i][0]; y = y + dir[i][1];
            } else {
                f = 0; break;
            }
        }
        if (f == 1) {
            for (int j = 0; j < 7; j++) {
                x = x - dir[i][0]; y = y - dir[i][1];
                vis[x][y] = 1;
            }
        }
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", input[i]);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (input[i][j] == 'y') {
                dfs(i, j);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (vis[i][j] == 1) printf("%c", input[i][j]);
            else printf("*");
        }
        printf("\n");
    }
    return 0;
}
P1462 通往奥格瑞玛的道路(最短路,Dijkstra)
#include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

const long long inf = 1e9 + 7;
const int maxn = 10005;
const int maxm = 50005;
int n, m, l, r, ai, bi, tot, head[maxn], vis[maxn], f[maxn];
long long b, ci, dist[maxn];

struct node {
    int to, nex;
    long long cost;
}edge[2 * maxm];
struct G {
    int id;
    long long cost;
    G() {}
    G(int _id, long long _cost) {
        id = _id; cost = _cost;
    }
    bool operator < (const G & _G) const {
        return cost > _G.cost;
    }
}now;

inline void addedge(int u, int v, long long w) {
    edge[++tot].to = v;
    edge[tot].cost = w;
    edge[tot].nex = head[u];
    head[u] = tot;
}

int dijkstra(int c) {
    for (int i = 1; i <= n; i++) vis[i] = 0, dist[i] = inf;
    priority_queue<G> q;
    while (!q.empty()) q.pop();
    q.push(G(1, 0));
    dist[1] = 0;
    while (!q.empty()) {
        now = q.top();
        q.pop();
        if (vis[now.id] == 1) continue;
        vis[now.id] = 1;
        for (int i = head[now.id]; i > 0; i = edge[i].nex) {
            int cur = edge[i].to;
            if (vis[cur] == 0 && dist[cur] > dist[now.id] + edge[i].cost && f[cur] <= c) {
                dist[cur] = dist[now.id] + edge[i].cost;
                q.push(G(cur, dist[cur]));
            }
        }
    }
    if (dist[n] <= b) return 1;
    else return 0;
}

int main() {
    scanf("%d%d%lld", &n, &m, &b);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &f[i]);
        r = max(r, f[i]);
    }
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%lld", &ai, &bi, &ci);
        addedge(ai, bi, ci);
        addedge(bi, ai, ci);
    }
    int ans = 0;
    while (l <= r) {
        int mid = (l + r) / 2;
        int flag = dijkstra(mid);
        if (flag) ans = mid, r = mid - 1;
        else l = mid + 1;
    }
    if (ans == 0) printf("AFK\n");
    else {
        ans = max(ans, f[1]);
        printf("%d\n", ans);
    }
    return 0;
}
P1443 马的遍历(bfs)
#include <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn = 405;
int n, m, x, y, ans[maxn][maxn], vis[maxn][maxn];
int dir[8][2] = {{1, 2}, {1, -2}, {2, -1}, {2, 1}, {-1, 2}, {-1, -2}, {-2, -1}, {-2, 1}};

void bfs() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            ans[i][j] = -1;
            vis[i][j] = 0;
        }
    }
    ans[x][y] = 0;
    vis[x][y] = 1;
    queue<pair<int, int> > q;
    while (!q.empty()) q.pop();
    q.push(make_pair(x, y));
    while (!q.empty()) {
        int cur_x = q.front().first, cur_y = q.front().second;
        q.pop();
        for (int i = 0; i < 8; i++) {
            int nex_x = cur_x + dir[i][0], nex_y = cur_y + dir[i][1];
            if (nex_x >= 1 && nex_x <= n && nex_y >= 1 && nex_y <= m && vis[nex_x][nex_y] == 0) {
                ans[nex_x][nex_y] = ans[cur_x][cur_y] + 1;
                vis[nex_x][nex_y] = 1;
                q.push(make_pair(nex_x, nex_y));
            }
        }
    }
}

int main() {
    scanf("%d%d%d%d", &n, &m, &x, &y);
    bfs();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            printf("%-5d", ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}
P1433 吃奶酪(dfs)
#include <stdio.h>
#include <algorithm>
#include <cmath>

using namespace std;

int n, vis[20];
double ans, a[20], b[20], f[35000][20];

double calc_dist(int cur, int to) {
    return sqrt((a[cur] - a[to]) * (a[cur] - a[to]) + (b[cur] - b[to]) * (b[cur] - b[to]));
}

void dfs(int step, int cur, double cost, int state) {
    if (cost > ans) return;
    if (step == n) {
        ans = min(ans, cost);
        return;
    }
    for (int i = 1; i <= n; i++) {
        if (vis[i] == 0) {
            int cur_state = state + (1 << (i - 1));
            if (f[cur_state][i] != 0 && f[cur_state][i] <= cost + calc_dist(cur, i)) continue;
            vis[i] = 1;
            f[cur_state][i] = cost + calc_dist(cur, i);
            dfs(step + 1, i, f[cur_state][i], cur_state);
            vis[i] = 0;
        }
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lf%lf", &a[i], &b[i]);
    }
    ans = 1e9 + 7;
    dfs(0, 0, 0, 0);
    printf("%.2lf\n", ans);
    return 0;
}
P5733 【深基6.例1】自动修正(字符串)
#include <stdio.h>
#include <string>
#include <cstring>

using namespace std;

const int maxn = 105;
char s[maxn];

int main() {
    scanf("%s", s);
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        if (s[i] >= 'a' && s[i] <= 'z') {
            printf("%c", 'A' + (s[i] - 'a'));
        } else {
            printf("%c", s[i]);
        }
    }
    return 0;
}
P5015 [NOIP2018 普及组] 标题统计(字符串输入一行)
#include <stdio.h>
#include <cstring>
#include <string>
#include <iostream>

using namespace std;

string s;
int ans;

int main() {
    getline(cin, s);
    for (int i = 0; i < s.size(); i++) {
        if (s[i] != ' ' && s[i] != '\n') ans = ans + 1;
    }
    printf("%d\n", ans);
    return 0;
}
P5736 【深基7.例2】质数筛(素数筛)
#include <stdio.h>
#include <algorithm>

using namespace std;

const int maxn = 100005;
int is_prime[maxn], n, x, r = 100000;

void make_prime() {
    for (int i = 2; i <= r; i++) is_prime[i] = 1;
    for (int i = 2; i <= r; i++) {
        if (is_prime[i] == 1) {
            for (int j = i * 2; j <= r; j = j + i) {
                is_prime[j] = 0;
            }
        }
    }
}

int main() {
    make_prime();
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x);
        if (is_prime[x] == 1) printf("%d ", x);
    }
    return 0;
}
P3853 [TJOI2007]路标设置(二分)
#include <stdio.h>
#include <algorithm>

using namespace std;

const int maxn = 100005;
int l, n, k, a[maxn];

int check(int t) {
    if (t == 0) return 0;
    int cur_k = k;
    for (int i = 2; i <= n; i++) {
        if (a[i] - a[i - 1] == 0) continue;
        int cost = (a[i] - a[i - 1]) / t;
        if (cost * t == (a[i] - a[i - 1])) {
            cur_k = cur_k - (cost - 1);
        } else {
            cur_k = cur_k - cost;
        }
        if (cur_k < 0) break;
    }
    if (cur_k < 0) return 0;
    else return 1;
}

int main() {
    scanf("%d%d%d", &l, &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a + 1, a + 1 + n);
    int ans = l, bottom = 0, top = l;
    while (bottom <= top) {
        int mid = (bottom + top) / 2;
        if (check(mid) == 1) {
            ans = mid; top = mid - 1;
        } else {
            bottom = mid + 1;
        }
        // printf("%d %d\n", mid, check(mid));
    }
    printf("%d\n", ans);
    return 0;
}

posted on 2023-04-18 21:22  solvit  阅读(37)  评论(0)    收藏  举报

导航