2019.7.9

2019.7.9

cf #572 div2 A

给你一个只有0和1的字符串,如果0和1的数目不相等,那么就是good,求用最少次数拆分字符串,使得他所有子串都是good。

思路:如果字符串是奇数位的,显然该字符串是good,不用拆分。如果是偶数位,先判断字符串是否是good,如果不是good,那就把第一个字符拆出来。

#include <cstdio>

int main() {
    int n;
    char a[110];
    while(~scanf("%d %s", &n, a)) {
        int cnt = 0;
        if(n % 2) printf("1\n%s\n", a);
        else {
            for(int i = 0; i < n; i++) {
                if(a[i] == '0') cnt++;
            }
            if(cnt != n / 2) printf("1\n%s\n", a);
            else {
                printf("2\n%c ", a[0]);
                for(int i = 1; i < n; i++) printf("%c", a[i]);
                printf("\n");
            }
        }
    }
    return 0;
}

cf #572 div2 B

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
    int n;
    while(~scanf("%d", &n)) {
        int a[100010];
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);
        sort(a,a + n);
        if(a[n-1] >= a[n - 2] + a[n - 3]) printf("NO\n");
        else {
            printf("YES\n");
            for(int i = 0; i < n; i += 2) printf("%d ", a[i]);
            for(int i = n - 1 - (n%2); i >= 0; i -= 2) printf("%d ", a[i]);
            printf("\n");
        }
    }
    return 0;
}

cf #572 div2 C

https://www.cnblogs.com/wyboooo/p/11153973.html

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
    int n;
    while(~scanf("%d", &n)) {
        int a[100010];
        a[0] = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            a[i] += a[i-1];
        }
        int q;
        scanf("%d", &q);
        while(q--) {
            int l, r;
            scanf("%d %d", &l, &r);
            printf("%d\n", (a[r] - a[l-1]) / 10);
        }
    }
    return 0;
}

cf #572 div2 D1

判断是否存在度为二的节点,如果有就NO,否则YES。

题意:能否通过改变两个叶子之间所有路径的值,使得每条路径可以任意改变其值。如果有度为二的节点,那么就有两条边的值是一定相等的。(注:题目中只有4567是叶子)

#include <cstdio>
#include <cstring>
int main() {
    int n;
    while(~scanf("%d", &n)) {
        int a[100010], l, r, flag = 0;
        memset(a, 0, sizeof(a));
        for(int i = 0; i < n - 1; i++) {
            scanf("%d %d", &l, &r);
            a[l]++, a[r]++;
        }
        for(int i = 0; i < 100010; i++) {
            if(a[i] == 2) {
                flag = 1;
                break;
            }
        }
        if(flag) printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

cf #572 div2 E

https://www.cnblogs.com/wyboooo/p/11155925.html

#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;

int a[300010];
LL c[300010];

int main() {
    c[0] = 0;
    for(int i = 1; i < 300010; i++) c[i] = c[i-1] + i;
    LL n, p, k;
    while(~scanf("%lld %lld %lld", &n, &p, &k)) {
        memset(a, 0, sizeof(a));
        LL ans = 0, b;
        for(int i = 0; i < n; i++) {
            scanf("%lld", &b);
            b = (b % p * b % p * b % p * b % p) + p - ((b % p) * (k % p) ) % p;
            b = b % p;
            a[i] = b;
        }
        sort(a, a+n);
        a[n] = p;
        int cnt = 1;
        for(int i = 0; i < n; i++) {
            if(a[i] == a[i+1]) {
                cnt++;
            }
            else {
                ans += c[cnt-1];
                cnt = 1;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

cf #571 div2 A

水题

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    if(min(b,c)>=a) printf("Yes\n");
    else printf("No\n");
    return 0;
}

cf #571 div2 D

注意9.000只能是9,而不能是10

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
    int n;
    while(~scanf("%d", &n)) {
        double a[100010];
        long long ans = 0;
        for(int i = 0; i < n; i++) {
            scanf("%lf", &a[i]);
            ans += (int)a[i];
        }
        //printf("ans=%d\n",ans);
        if(ans >= 0) {
            for(int i = 0; i < n; i++) {
                if(ans > 0 && a[i] <= 0 &&( (long long)(a[i] * 100000) % 100000 )!= 0) {
                    printf("%d\n",(int) a[i] - 1);
                    ans--;
                }
                else printf("%d\n",(int)a[i]);
            }
        }
        else {
            for(int i = 0; i < n; i++) {
                if(ans < 0 && a[i] >= 0 &&( (long long)(a[i] * 100000) % 100000 )!= 0) {
                    printf("%d\n", (int)a[i] + 1);
                    ans++;
                }
                else printf("%d\n",(int)a[i]);
            }
        }
    }
    return 0;
}
posted @ 2019-07-10 14:36  小饭hhh  阅读(137)  评论(0编辑  收藏  举报