More than 2014 school connections (HDU 4972 HDU 4973 HDU 4974 HDU 4975)

Source: Internet
Author: User

HDU 4972 A Simple Dynamic Programming Problem

Question: How many possible scores are given in a basketball match with scores of 1, 2, and 3 (the difference between the two teams is also a score of 2 )?

Ideas:

A relatively simple idea can be used as a table. There are several situations in which there are only 1-> 2 and 2-> 1. It is easy to find that there will be more than one problem. in other cases, you only need to calculate this special score difference. Note that the final result should not be multiplied by 2. If the final score difference is 0, you do not need to use X: there is only one type of X, but the final score difference is not 0. It is necessary to take the form of X: y and y: X !! The Sb score member remembers the wrong score, so the number of wrong types is 0.

Code:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;int a[100010];int main(){    int t,n,i,l,flag,cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(i=1;i<=n;i++) scanf("%d",&a[i]);        a[0]=0;        l=1;        flag=1;        for(i=1;i<=n;i++)        {            if(abs(a[i]-a[i-1])>3||(a[i]!=1&&a[i]==a[i-1]))            {                flag=0;                break;            }            if(a[i-1]==1&&a[i]==2||a[i-1]==2&&a[i]==1) l++;        }        printf("Case #%d: ",cas++);        if(!flag)        {            printf("0\n");        }        else        {            if(a[n]==0)                printf("%d\n",l);            else printf("%d\n",l*2);        }    }    return 0;}

HDU 4973 a simple simulation problem

Question: The first interval is 1, 2, 3... N has m operations. D operations. Copy intervals. Q indicates the maximum number of identical elements in a query interval.

Ideas:

A line segment tree... Because the range will be replicated, we will naturally think of the node. Remember that the number of elements is more interesting. The searching method is different from the previous method. However, since the number of elements has been recorded, we can search for them by number. I am use (Head, num) indicates that the interval starts to operate the num element from the first element. In this way, you can locate the interval on the online segment tree. Note that the leaf node can be specially processed.

Code:


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef __int64 LL;#define N 50010#define L(x) (x<<1)#define R(x) ((x<<1)|1)struct node{    LL sum,max,lazy;    bool leaf;}f[N*4];int t,n,m;void up(int i){    f[i].sum=f[L(i)].sum+f[R(i)].sum;    f[i].max=max(f[L(i)].max,f[R(i)].max);}void down(int i){    if(f[i].lazy!=1)    {        f[L(i)].sum*=f[i].lazy;        f[L(i)].max*=f[i].lazy;        f[L(i)].lazy*=f[i].lazy;        f[R(i)].sum*=f[i].lazy;        f[R(i)].max*=f[i].lazy;        f[R(i)].lazy*=f[i].lazy;        f[i].lazy=1;    }}void init(int l,int r,int i){    f[i].leaf=false;    f[i].lazy=1;    if(l==r)    {        f[i].leaf=true;        f[i].sum=f[i].max=1;        return ;    }    int mid=(l+r)>>1;    init(l,mid,L(i));    init(mid+1,r,R(i));    up(i);}void update(LL head,LL num,int i){    if(head==1&&num==f[i].sum)    {        f[i].sum*=2;        f[i].max*=2;        f[i].lazy*=2;        return ;    }    if(f[i].leaf)    {        f[i].sum+=num;        f[i].max+=num;        return ;    }    down(i);    if(f[L(i)].sum>=head+num-1) update(head,num,L(i));    else if(head>f[L(i)].sum) update(head-f[L(i)].sum,num,R(i));    else    {        LL fzc=f[L(i)].sum-head+1;        update(head,fzc,L(i));        update(1,num-fzc,R(i));    }    up(i);}LL query(LL head,LL num,int i){    if(head==1&&num==f[i].sum) return f[i].max;    if(f[i].leaf) return num;    down(i);    LL res;    if(f[L(i)].sum>=head+num-1) res=query(head,num,L(i));    else if(head>f[L(i)].sum) res=query(head-f[L(i)].sum,num,R(i));    else    {        LL fzc=f[L(i)].sum-head+1;        res=query(head,fzc,L(i));        res=max(res,query(1,num-fzc,R(i)));    }    up(i);    return res;}int main(){    int i,cas=1;    LL l,r;    char op[3];    scanf("%d",&t);    while(t--)    {        printf("Case #%d:\n",cas++);        scanf("%d%d",&n,&m);        init(1,n,1);        while(m--)        {            scanf("%s%I64d%I64d",op,&l,&r);            if(op[0]=='D') update(l,r-l+1,1);            else printf("%I64d\n",query(l,r-l+1,1));        }    }    return 0;}

HDU 4974 a simple water problem

You can select one or two elements each time so that they can be added to the given sequence from all 0 to at least a few operations in the plus 1 column.

Ideas:

At the beginning, we must add two values at a time. If there is only one addition at a time, we can determine whether the maximum element will exceed the sum of other elements.

Code:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef __int64 LL;LL a,b,c,ans;int T,t,n;int main(){    int i;    scanf("%d",&T);    for(t=1;t<=T;t++)    {        scanf("%d",&n);        a=b=c=0;        for(i=1;i<=n;i++)        {            scanf("%I64d",&a);            c=max(c,a);            b+=a;        }        a=b-c;        if(c>=a) ans=c;        else        {            ans=b/2;            if(ans*2<b) ans++;        }        printf("Case #%d: %I64d\n",t,ans);    }    return 0;}

HDU 4975 is a wrong question... HDU 4888

Ideas:

The search for this question mark is wrong. You can use HDU 4888 data to hack how to solve the problem--B or you are the same as it... Or you may be able to search for brute force traffic faster !!

Code: (I wrote the same error as it... As long as the wrong code runs faster than the standard process... Do not do this question !!)

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 1010;const int MAXM = 1010 * 1010 * 2;const int INF = 2000000000;struct Node {    int from, to, next, cap;} edge[MAXM];int tol, n;int head[MAXN], dep[MAXN], gap[MAXN], sumx[MAXN], sumy[MAXN], vis[MAXN];void addedge(int u, int v, int w) {    edge[tol].from = u;    edge[tol].to = v;    edge[tol].cap = w;    edge[tol].next = head[u];    head[u] = tol++;    edge[tol].from = v;    edge[tol].to = u;    edge[tol].cap = 0;    edge[tol].next = head[v];    head[v] = tol++;}int que[MAXN];void BFS(int start, int end) {    memset(dep, -1, sizeof(dep));    memset(gap, 0, sizeof(gap));    gap[0] = 1;    int front, rear, u, v, i;    front = rear = 0;    dep[end] = 0;    que[rear++] = end;    while (front != rear) {        u = que[front++];        for (i = head[u]; i != -1; i = edge[i].next) {            v = edge[i].to;            if (dep[v] != -1)                continue;            que[rear++] = v;            dep[v] = dep[u] + 1;            ++gap[dep[v]];        }    }}int cur[MAXN], S[MAXN];int SAP(int start, int end) {    int i, u, res = 0, top = 0, temp, inser;    BFS(start, end);    memcpy(cur, head, sizeof(head));    u = start;    while (dep[start] < n) {        if (u == end) {            temp = INF;            for (i = 0; i < top; i++)                if (temp > edge[S[i]].cap) {                    temp = edge[S[i]].cap;                    inser = i;                }            for (i = 0; i < top; i++) {                edge[S[i]].cap -= temp;                edge[S[i] ^ 1].cap += temp;            }            res += temp;            top = inser;            u = edge[S[top]].from;        }        if (u != end && gap[dep[u] - 1] == 0)            break;        for (i = cur[u]; i != -1; i = edge[i].next)            if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1)                break;        if (i != -1) {            cur[u] = i;            S[top++] = i;            u = edge[i].to;        } else {            int min = n;            for (i = head[u]; i != -1; i = edge[i].next) {                if (edge[i].cap == 0)                    continue;                if (min > dep[edge[i].to]) {                    min = dep[edge[i].to];                    cur[u] = i;                }            }            --gap[dep[u]];            dep[u] = min + 1;            ++gap[dep[u]];            if (u != start)                u = edge[S[--top]].from;        }    }    return res;}bool sol(int u, int fa) {    int i, v;    vis[u] = 1;    for (i = head[u]; ~i; i = edge[i].next) {        v = edge[i].to;        if (!edge[i].cap || v == fa || vis[v] == 2)            continue;        if (vis[v] == 1 || sol(v, u))            return true;    }    vis[u] = 2;    return false;}bool findcircle(int fn) {    for (int i = 1; i <= fn; i++) {        vis[i] = 1;        if (sol(i, i))            return true;        vis[i] = 2;    }    return false;}int main() {    //freopen("1005.in", "r", stdin);    //freopen("1005.txt", "w", stdout);    int i, j, n1, n2, ans, End, CAS, fff = 1;    scanf("%d", &CAS);    while (CAS--) {        scanf("%d%d", &n1, &n2);        sumx[0] = sumy[0] = 0;        for (i = 1; i <= n1; i++) {            scanf("%d", &sumx[i]);            sumx[0] += sumx[i];        }        for (i = 1; i <= n2; i++) {            scanf("%d", &sumy[i]);            sumy[0] += sumy[i];        }        if (sumx[0] != sumy[0]) {            printf("Case #%d: So naive!\n", fff++);            continue;        }        End = n1 + n2 + 1;        tol = 0;        n = End + 1;        for (i = 0; i < n; i++) {            head[i] = -1;            vis[i] = 0;        }        for (i = 1; i <= n1; i++) {            addedge(0, i, sumx[i]);            for (j = 1; j <= n2; j++)                addedge(i, n1 + j, 9);        }        for (i = 1; i <= n2; i++)            addedge(n1 + i, End, sumy[i]);        ans = SAP(0, End);        if (ans != sumx[0])            printf("Case #%d: So naive!\n", fff++);        else {            if (findcircle(n1))                printf("Case #%d: So young!\n", fff++);            else                printf("Case #%d: So simple!\n", fff++);        }    }    return 0;}


More than 2014 school connections (HDU 4972 HDU 4973 HDU 4974 HDU 4975)

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.