zoukankan      html  css  js  c++  java
  • gym100676 [小熊骑士限定]2015 ACM Arabella Collegiate Programming Contest

    Kuma Rider久违的第二场训练,这场很水,又在vj的榜单上看到第一场的大哥了,2小时ak,大哥牛啤!

    A.水

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    const double eps = 1e-5;
    
    int T;
    char s[1000005];
    int main(void) 
    {
        scanf("%d", &T);
        getchar();
        while (T--)
        {
            gets(s);
            int len = strlen(s);
            int p;
            char luo[12];
    for(int i=1;i<=2;i++)luo[i]=0;
            bool fu = false;
            int num1 = 0;
            for (p = 0; p < len; p++)
            {
                //printf("%d  %c
    ", p, s[p]);
                if (s[p] == '-')fu = true;
                else if (s[p] >= '0'&&s[p] <= '9')
                {
                    num1 *= 10;
                    num1 += s[p] - '0';
                }
                else if (s[p] == ' ')continue;
                else
                {
                    int tot = 1;
                    while (s[p] != ' ')
                    {
                        luo[tot++] = s[p]; p++;
                        
                    }
                    break;
                }
                //printf("%d  %c
    ", p, s[p]);
            }
    
            if (fu)num1 = -num1;
            int num2 = 0;
            fu = false;
            for (p = p + 1; p < len; p++)
            {
                if (s[p] == '-')fu = true;
                else if (s[p] >= '0'&&s[p] <= '9')
                {
                    num2 *= 10;
                    num2 += s[p] - '0';
                }
            }
            if (fu)num2 = -num2;
    
            if (luo[1] == '<')
            {
                if (luo[2] == '=')
                {
                    if (num1 <= num2)printf("true
    ");
                    else printf("false
    ");
                }
                else
                {
                    if (num1 < num2)printf("true
    ");
                    else printf("false
    ");
                }
            }
            else if (luo[1] == '>')
            {
                if (luo[2] == '=')
                {
                    if (num1 >= num2)printf("true
    ");
                    else printf("false
    ");
                }
                else
                {
                    if (num1 > num2)printf("true
    ");
                    else printf("false
    ");
                }
            }
            else if (luo[1] == '!')
            {
                if (num1 != num2)printf("true
    ");
                else printf("false
    ");
            }
            else
            {
                if (num1 == num2)printf("true
    ");
                else printf("false
    ");
            }
    
        }
    
    
    
    }
    View Code

    B.水

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    const double eps = 1e-5;
    
    int main(void) {
        int t;
        scanf("%d", &t);
        while (t--) {
            int a;
            int sum = 0;
            int fla = 0;
            for (int i = 0; i < 3; i++) {
                scanf("%d", &a);
                if (a <= 0) fla = 1;
                sum += a;
            }
            if (fla || sum != 180) printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
    View Code

    C.背包

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    const double eps = 1e-5;
    const int inf = 0x3f3f3f3f;
    
    int a[105];
    int dp[33768];
    int sum;
    int main(void) {
        int t;
        scanf("%d", &t);
        while (t--) 
        {
            int K, m, n;
            sum = 0;
            scanf("%d%d%d", &n, &m, &K);
            for (int i =1; i <= K; i++) 
            {
                scanf("%d", &a[i]);
                sum += a[i];
            }
            int sh = n - sum;
            if (m <= sh)printf("0
    ");
            else
            {
                int need = m - sh;
                memset(dp, inf, sizeof(dp));
                dp[0] = 0;
                for (int i = 1; i <= K; i++)
                {
                    for (int j = sum; j >= a[i]; j--)
                    {
                        dp[j] = min(dp[j - a[i]] + 1, dp[j]);
                    }
                }
                int ans = inf;
                for (int i = need; i <= sum; i++)
                {
                    ans = min(ans, dp[i]);
                }
                printf("%d
    ", ans);
    
            }
    
        }
    }
    View Code

    D.判断数独对不对(队友没有用函数,暴力得就很真实)

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    const double eps = 1e-5;
    const int inf = 0x3f3f3f3f;
    //
    //int a[105];
    //int dp[33768];
    //int sum;
    //int main(void) {
    //    int t;
    //    scanf("%d", &t);
    //    while (t--) 
    //    {
    //        int K, m, n;
    //        sum = 0;
    //        scanf("%d%d%d", &n, &m, &K);
    //        for (int i =1; i <= K; i++) 
    //        {
    //            scanf("%d", &a[i]);
    //            sum += a[i];
    //        }
    //        int sh = n - sum;
    //        if (m <= sh)printf("0
    ");
    //        else
    //        {
    //            int need = m - sh;
    //            memset(dp, inf, sizeof(dp));
    //            dp[0] = 0;
    //            for (int i = 1; i <= K; i++)
    //            {
    //                for (int j = need; j >= 0; j--)
    //                {
    //                    dp[j] = min(dp[j - a[i]] + 1, dp[j]);
    //                }
    //            }
    //            printf("%d
    ", dp[need]);
    //
    //        }
    //
    //    }
    //}
    //
    
    char ch[100][100];
    int vis[10];
    int main(void) {
        int t;
        scanf("%d", &t);
        while (t--) {
            getchar();
            int fla = 0;
            for (int i = 1; i <= 9; i++) {
                scanf("%s", ch[i]+1);
            }
            for (int i = 1; i <= 9; i++) {
                memset(vis, 0, sizeof(vis));
                int sum = 0;
                for (int j = 1; j <= 9; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
                if (sum != 9) fla = 1;
            }
            for (int i = 1; i <= 9; i++) {
                memset(vis, 0, sizeof(vis));
                int sum = 0;
                for (int j = 1; j <= 9; j++) {
                    if (!vis[ch[j][i] - '0']) {
                        vis[ch[j][i] - '0'] = 1;
                        sum++;
                    }
                }
                if (sum != 9) fla = 1;
            }
            int sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= 3; i++) {
                for (int j = 1; j <= 3; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 4; i <= 6; i++) {
                for (int j = 1; j <= 3; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 7; i <= 9; i++) {
                for (int j = 1; j <= 3; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <=3; i++) {
                for (int j = 4; j <= 6; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 1; i <= 3; i++) {
                for (int j = 7; j <= 9; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 4; i <= 6; i++) {
                for (int j = 4; j <= 6; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 4; i <= 6; i++) {
                for (int j = 7; j <= 9; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 7; i <= 9; i++) {
                for (int j = 7; j <= 9; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            sum = 0;
            memset(vis, 0, sizeof(vis));
            for (int i = 7; i <= 9; i++) {
                for (int j = 4; j <= 6; j++) {
                    if (!vis[ch[i][j] - '0']) {
                        vis[ch[i][j] - '0'] = 1;
                        sum++;
                    }
                }
            }
            if (sum != 9) fla = 1;
            if (fla) printf("Invalid
    ");
            else {
                printf("Valid
    ");
            }
        }
    }
    View Code

    E.树状数组

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    
    const int maxn = 11000;
    int c[maxn], a[maxn];
    int n;
    int lowbit(int x) {
        return x&(-x);
    }
    void update_onepos(int pos, int x) {
        while (pos <= n) {
            c[pos] += x;
            pos += lowbit(pos);
        }
    }
    int getsum_onepos(int pos) {
        int sum = 0;
        while (pos > 0) {
            sum += c[pos];
            pos -= lowbit(pos);
        }
        return sum;
    }
    int getsum_range(int x1, int x2) {
        return getsum_onepos(x2) - getsum_onepos(x1-1);
    }
    int ax[11000];
    int main(void) {
        int t;
        scanf("%d", &t);
        while (t--) {
            int nn;
            n = -1;
            scanf("%d", &nn);
            memset(c, 0, sizeof(c));
            for (int i = 0; i < nn; i++) {
                scanf("%d", &ax[i]);
                n = max(ax[i], n);
            }
            n += 32;
            for (int i = 0; i < nn; i++) {
                update_onepos(ax[i], 1);
            }
            int sum = 0;
            for (int i = 0; i < nn; i++) {
                sum += getsum_range(max(0, ax[i] - 31), ax[i])-1;
                sum += getsum_range(ax[i] + 1, ax[i] + 31);
            }
            printf("%d
    ", sum / 2);
        }
        return 0;
    }
    View Code

    F.并查集,最后统计根节点的问号数量

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    typedef long long LL;
    const int maxv = 50050;
    const LL mod = 1e9 + 7;
    
    int T, n, m;
    char s[maxv];
    bool hav ;
    struct Node
    {
        int pos;
        int fa;
        char ch;
    }tree[50005];
    
    int find(int nx)
    {
        if (tree[nx].fa == nx)return nx;
    
        int ff = find(tree[nx].fa);
        tree[nx].ch = tree[ff].ch;
        return tree[nx].fa=ff;
    }
    
    bool vis[50005];
    
    void init()
    {
        hav = false;
        scanf("%d %d", &n, &m);
        scanf("%s", s + 1);
        for (int i = 1; i <= n; i++)
        {
            if (s[i] == '?')hav = true;
            tree[i].fa = i;
            tree[i].ch = s[i];
            tree[i].pos = i;
            vis[i] = false;
        }
    }
    
    
    int main(void) 
    {
        scanf("%d", &T);
        while (T--)
        {
            init();
            bool can = true;
            
            for (int i = 1; i <= m; i++)
            {
                int u, v;
                scanf("%d %d", &u, &v);
                int fx = find(u), fy = find(v);
                if (fx != fy)
                {
                    //printf("%d %d
    ", fx, fy);
                    if (tree[fx].ch!='?'&&tree[fy].ch!='?')
                    {
                        if (tree[fx].ch != tree[fy].ch)can = false;
                    }
                    else if (tree[fx].ch == '?'&&tree[fy].ch != '?')
                    {
                        tree[fx].fa = fy;
                        tree[fx].ch = tree[fy].ch;
                    }
                    else if (tree[fx].ch != '?'&&tree[fy].ch == '?')
                    {
                        tree[fy].fa = fx;
                        tree[fy].ch = tree[fx].ch;
                    }
                    else
                    {
                        tree[fx].fa = fy;
                        tree[fx].ch = tree[fy].ch;
                    }
                }
            }
    
            int l = 1, r = n;
            while (l <= r)
            {
                int fx = find(l), fy = find(r);
                if (fx != fy)
                {
                    //printf("%d %d
    ", fx, fy);
                    if (tree[fx].ch != '?'&&tree[fy].ch != '?')
                    {
                        if (tree[fx].ch != tree[fy].ch)can = false;
                    }
                    else if (tree[fx].ch == '?'&&tree[fy].ch != '?')
                    {
                        tree[fx].fa = fy;
                        tree[fx].ch = tree[fy].ch;
                    }
                    else if (tree[fx].ch != '?'&&tree[fy].ch == '?')
                    {
                        tree[fy].fa = fx;
                        tree[fy].ch = tree[fx].ch;
                    }
                    else
                    {
                        tree[fx].fa = fy;
                        tree[fx].ch = tree[fy].ch;
                    }
                }
                l++, r--;
            }
    
            if (!can){printf("0
    "); continue;}
    
            for (int i = 1; i <= n; i++)
            {
                int fx = find(i);
                s[i] = tree[fx].ch;
            }
    
            //printf("%s
    ", s + 1);
    
            LL ans = 1;
            bool tag = true;
            
    
            for (int i = 1; i <= n; i++)
            {
                int fx = find(i);
                if (!vis[fx])
                {
                    if (tree[fx].ch == '?')
                    {
                        ans *= 26;
                        ans %= mod;
                    }
                    vis[fx] = true;
                }
            }
    
            if (tag)
            {
                if (hav&&ans == 0)printf("1
    ");
                else if(!hav)printf("0
    ");
                else printf("%lld
    ", ans);
            }
            else printf("0
    ");
    
        }
        return 0;
    }
    View Code

    G.状压dp

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<functional>
    #include<vector>
    using namespace std;
    const int maxv = 110;
    
    int T, n, m;
    string s;
    map<string, int>mp;
    int in[maxv], w[maxv];
    int dp[1 << 19];
    
    int main()
    {
        cin >> T;
        while (T--)
        {
            cin >> n >> m;
            memset(dp, -1, sizeof(dp));
            getchar();
            for (int i = 1; i <= n; i++)
            {
                in[i] = 0;
                w[i] = 0;
            }
            mp.clear();
            for (int i = 0; i < n; i++)
            {
                getline(cin, s);
                int pos = -1;
                for (int j = 0; j < s.size(); j++)
                {
                    if (s[j] <= '9'&&s[j] >= '0')
                    {
                        pos = j;
                        break;
                    }
                }
                string tmp = s.substr(0, pos - 1);
                int ww = 0;
                string shuzi = s.substr(pos, s.size() - pos);
                for (int j = 0; j < shuzi.size(); j++)
                {
                    ww = ww * 10 + shuzi[j] - '0';
                }
                mp[tmp] = i;
                w[i] = ww;
                //cout << tmp << " " << ww << endl;
            }
            for (int i = 1; i <= m; i++)
            {
                getline(cin, s);
                int pos1 = -1;
                for (int i = 0; i < s.size(); i++)
                {
                    if (s[i] == '-')
                    {
                        pos1 = i;
                        break;
                    }
                }
                string t1 = s.substr(0, pos1 - 1);
                string t2 = s.substr(pos1 + 4, s.size() - pos1 - 4);
                int id1 = mp[t1];
                int id2 = mp[t2];
                in[id2] |= (1 << id1);
            }
            dp[0] = 0;
            for (int j = 0; j < (1 << n) - 1; j++)
            {
                if (dp[j] < 0)
                {
                    continue;
                }
                int num = 0;
                for (int i = 0; i < n; i++)
                {
                    num += (j >> i & 1);
                }
                for (int i = 0; i < n; i++)
                {
                    int now = j | (1 << i);
                    if (j != now && (in[i] & j) == in[i])
                    {
                        dp[now] = max(dp[now], dp[j] + (num + 1)*w[i]);
                    }
                }
            }
            cout << dp[(1 << n) - 1] << endl;
        }
        return 0;
    }
    
    /*
    1
    3 2
    Implementation 3
    Dynamic Programming 10
    Greedy 7
    Greedy --> Dynamic Programming
    Implementation --> Dynamic Programming
    */
    View Code

    H.模板题,待施工

  • 相关阅读:
    fill 全解(转)
    解题报告 疯狂的馒头
    解题报告 报数
    解题报告 noi 2005 智慧珠游戏(BT 搜索)
    解题报告 跑步
    解题报告 Punch
    解题报告 纪念日
    解题报告 新兵站队
    转载:让理科生沉默,让文科生流泪的综合题
    解题报告 信号放大器
  • 原文地址:https://www.cnblogs.com/fishdog/p/10472527.html
Copyright © 2011-2022 走看看