只有脚踩上去才知其远近和曲折

题解代码

HZ570

#include <iostream>
using namespace std;

#define MAXN 80005
int q[MAXN], cnt = 0;

int main() {
    int n, x, ans = 0;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> x;
        while (cnt && x >= q[cnt]) --cnt;
        ans += cnt;
        q[++cnt] = x;
    }
    cout << ans << endl;
    return 0;
}

1009

#include <iostream>
using namespace std;

#define INF 0x3f3f3f3f
#define MAXN 1000005
typedef long long LL;

int arr[MAXN];
int s, e, n, x;

int main() {
    cin >> s >> e >> n >> x;
    for (int i = 1; i <= n; i++) cin >> arr[i];
    int t = -1; // 上一次开始的时间 
    int d = 0; // 等待时间 
    int ans = INF;
    arr[0] = -1;
    arr[++n] = e + 1;

    for (int i = 1; i <= n; i++) {
        int now = arr[i] - 1;
        if (arr[i] != arr[i - 1]) {
            d -= now - t;
            if (d < 0) d = 0;

            if(!d) t = arr[i];
            else t = now;

            if (now + d > e) break;
            if(now >= s) {
                ans = min(ans, d);
            }
        }
        d += x;
    }
    if (ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}

1013

#include <iostream>
#include <cstdio>
using namespace std;

int check(int x, int y) {
    for (int n = x ; n <= y ; n++) {
        int temp = n * n + n + 41;
        for (int i = 2 ; i * i <= temp; i++) {
            if(temp % i == 0) return 0; 
        }
    }
    return 1;
}

int main() {
    int x, y;
    while (scanf("%d %d", &x, &y) != EOF) {
        if(x == 0 && y == 0) break;

        printf("%s\n", check(x, y) ? "OK" : "Sorry");
    }
    return 0;
} 

1014

#include <iostream>
#include <cstdio>
using namespace std;


int main() {
    int y, m, d;
    while(scanf("%d/%d/%d", &y, &m, &d) != EOF) {
        int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        int ans = d;

        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) arr[2] = 29;
        for (int i = 1; i < m; i++) ans += arr[i];
        printf("%d\n", ans);
    }
    return 0;
}

1015

#include<iostream>
#include<cmath>
using namespace std;

#define MAXN 1005
typedef long long LL;
int num1[MAXN], num2[MAXN];
int n, m;

LL solve(int ind) {
    LL ret = 0;
    for (int i = 0; i < n; i++) {
        LL dif = (num1[i] - num2[i + ind]);
        ret += dif * dif;
    }
    return ret;
}

int main() {
    LL ans = 0;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> num1[i];
    cin >> m;
    for (int i = 0; i < m; i++) cin >> num2[i];

    for (int i = 0; i <= m - n; i++) {
        LL temp = solve(i);
        if (i == 0) ans = temp;
        ans = min(temp, ans);
    }
    cout << ans << endl;
    return 0;
}

1016

#include<iostream>
#include<cstdio>
using namespace std;

int arr[13] = {0, 31, 28, 31, 30, 31 , 30, 31, 31, 30, 31, 30, 31};
int y, m, x;

int find(int n) {
    int cnt = 0;
    if (n < 10 && x == 0) cnt = 1;
    while (n) {
        if (n % 10 == x) cnt++;
        n /= 10;
    }
    return cnt;
}


int main() {    
    while (scanf("%d %d %d", &y, &m, &x) != EOF) {
        int sum = 0;
        if ((y % 4 == 0 && y % 100 != 0) || (y % 400) == 0) arr[2] = 29;
        else arr[2] = 28;

        int ycnt = find(y), mcnt = find(m);
        for (int i = 1; i <= arr[m]; i++) {
            sum += ycnt + mcnt + find(i);
        }
        printf("%d\n", sum);
    }
    return 0;
}

邪恶的jack

#include <iostream>
using namespace std;

int vis[105] = {0};

int main() {
    //freopen("protect.in", "r", stdin);
    //freopen("protect.out", "w", stdout);
    int n, k;
    cin >> n >> k;

    int m = n, cnt = 0, i = 1;
    while (m > 1) {
        if (!vis[i] && ++cnt == k) {
            vis[i] = 1;
            cnt = 0, ++k, --m;
        }
        if (++i > n) i -= n;
    }

    for (i = 1; i <= n; i++) {
        if (!vis[i]) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

保卫羊村

#include <iostream>
#include <cstdio>
using namespace std;

#define MAXN 1000005
#define MAXM 100005
#define PI 3.1416
int t[MAXM]; 
double dp[MAXN], d[MAXN];

int main() {
    //freopen("protect.in", "r", stdin);
    //freopen("protect.out", "w", stdout);
    double sum = 0.0;
    int n, c;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> d[i] >> t[i];
        d[i] = d[i] * d[i] * PI / 4.0;
        sum += d[i];
    }
    cin >> c;

    for (int i = 0; i < n; i++) {
        for (int j = c; j >= t[i]; j--) {
            dp[j] = max(dp[j], dp[j - t[i]] + d[i]);
        }
    }
    printf("%.4f\n", sum - dp[c]);
    return 0;
}

逃出城堡

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;

#define MAXN 505
int arr[MAXN][MAXN];
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};

int main() {
    int n, m, ans = 0, s, e;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%d", &arr[i][j]);
        }
    }
    scanf("%d %d", &s, &e);

    queue<pair<int, int> > q;
    q.push(make_pair(s, e));
    arr[s][e] = 1;
    while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        if (x == n && y == m) break;
        for (int i = 0; i < 4; i++) {
            int tx = x + dir[i][0], ty = y + dir[i][1];
            if (tx < 1 || tx > n || ty < 1 || ty > m || arr[tx][ty]) continue;
            arr[tx][ty] = arr[x][y] + 1;
            q.push(make_pair(tx, ty));
        }
    }
    printf("%d\n", arr[n][m] - 1);
    return 0;
}
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注