Zoj monthly, November 2012

Source: Internet
Author: User
Tags strtok


A. zoj 3666 Alice and Bob

Combined game, SG Function Application
#include<vector>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 10000 + 100;int SG[maxn];vector<int> g[maxn];int mex(int u) { //minimal excludant    if(SG[u]!=-1) return SG[u];    int i;    bool vis[maxn];    memset(vis, 0, sizeof vis );    for(i=0; i<g[u].size(); ++i) {        vis[mex(g[u][i])] = true;    }    for(i=0; vis[i]; ++i);    return SG[u] = i;}int main() {    int n, c, x, q, m;    int cas= 1;    while(~scanf("%d", &n)) {        for(int i=0; i<=n; ++i) g[i].clear();        for(int i=1; i<n; ++i) {            scanf("%d", &c);            while(c--) {                scanf("%d", &x);                g[i].push_back(x);            }        }        memset(SG, -1, sizeof SG );        printf("Case %d:\n", cas++);        scanf("%d", &q);        while(q--) {            scanf("%d", &m);            int ans = 0;            while(m--) {                scanf("%d", &x);                ans ^= mex(x);            }            if(ans) puts("Alice");            else puts("Bob");        }    }    return 0;}



C. zoj3668 launching the spacious
Constraint: F [I] indicates the total energy of the first rock.
F [R]-f [L-1]> =
F [R]-f [L-1] <= B
F [I]-f [I-1] <= 10000
F [I]-f [I-1]> =-10000
Why is the Lexicographic Order... See the following on the Internet:
To sum up, the difference constraint system has two solutions:
1. Short Circuit Model.All constraints are in the form of f (x) <= f (y) + B, and B is positive or negative. In this way, the addedge (Y, X, B) edge in the directed graph is obtained. Based on the nature of the shortest path, we can obtain that the shortest path of X satisfies all f (x) values) <= f (yi) + bi, and make a (some) f (x) = f (yk) + BK. It was originally f (x) <= f (yk) + bk, And now f (x) = f (yk) + bk, Therefore, the F value obtained using the shortest model is the maximum value.
2. Longest path model.
All constraints are in the form of f (x)> = f (y) + B, and B is positive or negative. In this way, the addedge (Y, X, B) edge in the directed graph. Based on the nature of the longest path solution, we can obtain that the longest PATH value of X satisfies all f (x)> = f (yi) + bi, and make a (some) f (x) = f (yk) + BK. It was originally f (x)> = f (yk) + bk, but now only f (x) = f (yk) + bk, Therefore, the F value obtained using the longest path model is the minimum value.
After returning to this question, all values are the maximum values, which are determined by sum [0] and sum [N]. Therefore, the largest Lexicographic Order can be obtained.
 
#include<bits/stdc++.h>using namespace std;const int maxn = 1010;const int maxm =  40000 + 100;const int INF = 1e9;struct Edge{    int to, next;    int w;}edge[maxm];int head[maxn], tot;void init(){    tot = 0;    memset(head, -1, sizeof head );}void addedge(int u, int v, int w){    edge[tot].to = v;    edge[tot].next = head[u];    edge[tot].w = w;    head[u] = tot++;}bool vis[maxn];int cnt[maxn];int dist[maxn];bool spfa(int n){    memset(vis, false, sizeof vis );    for(int i=0; i<=n; ++i) dist[i] = INF;    memset(cnt, 0, sizeof cnt );    queue<int> q;    q.push(0);    vis[0] = true;    cnt[0] = 1;    dist[0] = 0;    while(!q.empty()){        int u = q.front();        q.pop();        vis[u] = false;        for(int i=head[u]; i!=-1; i=edge[i].next){            int v = edge[i].to;            int w = edge[i].w;            if(dist[v] > dist[u] + w){                dist[v] = dist[u] + w;                if(!vis[v]){                    vis[v] = true;                    q.push(v);                    if(++cnt[v] > n+1) return false;                }            }        }    }    return true;}int main(){    int n, m;    while(~scanf("%d%d", &n, &m)){        init();        for(int i=1; i<=n; ++i){            addedge(i-1, i, 10000);            addedge(i, i-1, 10000);        }        int l, r, a, b;        while(m--){            scanf("%d%d%d%d", &l, &r, &a, &b);            addedge(l-1, r, b);            addedge(r, l-1, -a);        }        if(!spfa(n)){            printf("The spacecraft is broken!\n");            continue;        }        for(int i=1; i<=n; ++i){            printf("%d", dist[i]-dist[i-1]);            if(i<n) printf(" ");            else printf("\n");        }    }    return 0;}



D. zoj 3669 japan ese Mahjong I
...


F. zoj 3671 japan ese Mahjong III
Simple simulation
#include<bits/stdc++.h>using namespace std;const int maxn = 9;int z[maxn+10], s[maxn+10], p[maxn+10], m[maxn+10];char str[100];bool seven(){    int cnt = 0;    for(int i=1; i<=9; ++i)    {        if(z[i]==2) cnt++;        if(s[i]==2) cnt++;        if(p[i]==2) cnt++;        if(m[i]==2) cnt++;    }    return cnt==7;}bool thirteen(){    int cnt = m[1] + m[9] + p[1] + p[9] + s[1] + s[9] + z[1] + z[2] + z[3] + z[4] + z[5]    + z[6] + z[7];    if(m[1]>=1&&m[9]>=1&&p[1]>=1&&p[9]>=1&&s[1]>=1&&s[9]>=1&&z[1]>=1&&z[2]>=1&&z[3]>=1&&z[4]>=1       &&z[5]>=1&&z[6]>=1&&z[7]>=1){        return cnt == 14;    }    return 0;}int main(){    while(~scanf("%s", str)) {        int n = strlen(str);        memset(z, 0, sizeof z );        memset(s, 0, sizeof s );        memset(p, 0, sizeof p );        memset(m, 0, sizeof m );        for(int i=0; i<n; i+=2) {            int num = str[i] - '0';            if(str[i+1]=='z')                z[num]++;            else if(str[i+1]=='s')                s[num]++;            else if(str[i+1]=='m')                m[num]++;            else if(str[i+1]=='p')                p[num]++;        }        if(seven()) {            printf("Seven Pairs\n");        } else if(thirteen()) {            printf("Thirteen Orphans\n");        } else printf("Neither!\n");    }    return 0;}



G. zoj 3672 Gao the sequence
1. Each operation minus two deltas is an even number. Therefore, the final increment and sum must be an even number.
2. If there is 2 * (a [I]-B [I])> sum, this is also not acceptable. In this case, the array minus 2 * (a [I]-B [I]), where a [I] minus (A [I]-B [I]), and the other numbers minus the sum (A [I]-B [I, but the increment is obviously insufficient.

#include<bits/stdc++.h>using namespace std;typedef long long LL;int main () {    int n;    while (~scanf("%d",&n)) {        LL maxx = 0, sum = 0;        for (int i=1; i<=n; i++) {            LL a,b;            scanf("%I64d%I64d",&a, &b);            maxx=max(maxx,a-b);            sum+=a-b;        }        cout<<sum<<" "<<maxx<<endl;        if ((sum%2==1) ||maxx*2>sum) printf("NO\n");        else printf("YES\n");    }    return 0;}


H.zoj 3673 1729



I. zoj 3674 search in the Wiki
STL application set, map
#include<bits/stdc++.h>using namespace std;const int maxn = 100 + 10;set<string> S[maxn];map<string,int> M;char buf[1001], str[1001];vector<int> vc;int n, m;int main() {    while(~scanf("%d", &n)) {        M.clear();        string ss;        for(int i=0; i<n; ++i) {            S[i].clear();            cin>>ss;            M[ss] = i;            getchar();            gets(buf);            char *p = strtok(buf," ");            while(p) {                sscanf(p, "%s", str );                S[i].insert(string(str));                p = strtok(NULL, " ");            }        }        scanf("%d", &m);        getchar();        while(m--) {            vc.clear();            gets(buf);            char *p = strtok(buf, " ");            while(p) {                sscanf(p, "%s", str );                vc.push_back( M[string(str)] );                p = strtok(NULL, " ");            }            vector<string> ans;            for(set<string>::iterator it = S[vc[0]].begin(); it!=S[vc[0]].end(); ++it) {                string tmp = *it;                bool found = true;                for(int i=1; i<vc.size(); ++i) {                    if(S[vc[i]].find(tmp) == S[vc[i]].end()) {                        found = false;                        break;                    }                }                if(found) ans.push_back(tmp);            }            sort(ans.begin(), ans.end());            if(ans.size()==0) puts("NO");            else {                for(int i=0; i<ans.size(); ++i) {                    cout<<ans[i];                    if(i<ans.size()-1) cout<<" ";                    else cout<<endl;                }            }        }    }    return 0;}



J. zoj 3675 trim the nails
BFS: each time you use a useless method to cut your nails until the result is 0, that is, the nail clipper ends.

State compression DP: State: DP [1 <M-1], result is DP [0], state transfer is enumerating the position and order of the climbing scissors, and then generate a new state.

#include<bits/stdc++.h>using namespace std;const int INF = 1e9;int dp[1<<21];int main() {    int n, m;    char s[20];    while(~scanf("%d", &n)) {        scanf("%s", s);        scanf("%d", &m);        int clr = 0, rclr = 0;        for(int i=0; i<n; ++i) {            clr = (clr<<1) + (s[i]=='*');            rclr = (rclr<<1) + (s[n-i-1]=='*');        }        for(int i=0; i<=(1<<m); ++i) dp[i] = INF;        dp[(1<<m)-1] = 0;        for(int i=(1<<m)-1; i>=0; --i)            if(dp[i]!=INF) {                for(int j=0; j<m; ++j) {                    dp[i& (~(clr<<j))] = min(dp[i&(~(clr<<j))], dp[i] + 1);                    dp[i& (~(rclr<<j))] = min(dp[i&(~(rclr<<j))], dp[i]+1);                    dp[i& (~(clr>>j))] = min(dp[i&(~(clr>>j))], dp[i] + 1);                    dp[i& (~(rclr>>j))] = min(dp[i&(~(rclr>>j))], dp[i]+1);                }            }        if(dp[0]!=INF) {            printf("%d\n", dp[0]);        } else printf("-1\n");    }    return 0;}


Zoj monthly, November 2012

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.