2016 Multi-University Training Contest 5

HDU【5783】——Divide the Sequence

Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfying that for each subsequence, every its prefix sum is not small than 0.

Input

The input consists of multiple test cases.
Each test case begin with an integer n in a single line.
The next line contains n integers A1,A2An.
1≤n≤1e6
−10000≤A[i]≤10000
You can assume that there is at least one solution.

Output

For each test case, output an integer indicates the maximum number of sequence division.

Sample Input

6
1 2 3 4 5 6
4
1 2 -3 0
5
0 0 0 0 0

Sample Output

6
2
5

Author

ZSTU

题意:给你一个序列,求可以最多分成多少段使的每一段的任何前缀和都不小于0,从后先前推一遍就行。

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int Max = 1e6+100;

LL arr[Max];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i  = 0;i<n;i++)
        {
            scanf("%lld",&arr[i]);
        }

        LL sum = 0;

        int ans = 0;

        for(int i = n-1;i>=0;i--)
        {
            sum+=arr[i];

            if(sum>=0)
            {
                ans++;

                sum = 0;
            }
        }

        printf("%d\n",ans);
    }
    return 0;
}

HDU【5784】——How Many Triangles

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Alice has n points in two-dimensional plane. She wants to know how many different acute triangles they can form. Two triangles are considered different if they differ in at least one point.

Input

The input contains multiple test cases.
For each test case, begin with an integer n,
next n lines each contains two integers xi and yi.
3≤n≤2000
0xi,yi109
Any two points will not coincide.

Output

For each test case output a line contains an integer.

Sample Input

3
1 1
2 2
2 3
3
1 1
2 3
3 2
4
1 1
3 1
4 1
2 3

Sample Output

0
1
2

Author

ZSTU

给你平面中的一些点,问能组成多少个锐角三角形。我们统计出有多少个个锐角和钝角(直角也归为钝角),因为一个钝角对应这两个锐角,所以锐角三角形的个数就是(锐角数目-2×钝角数目)/3.处理的时候,我们枚举点,将剩下的点以枚举的点为中心,然后枚举另外一个点,尺取的方式处理出锐角的分界线

#include <bits/stdc++.h>

using namespace std;

const int Max = 2200;

typedef long long LL;

typedef struct node
{
    LL x,y;

    node(){}

    node(LL _x,LL _y):x(_x),y(_y){}

    node operator + (const node &a)const
    {
        return node(x+a.x,y+a.y);
    }

    node operator - (const node &a) const
    {
        return node(x-a.x,y-a.y);
    }

    LL operator * (const node &a) const
    {
        return x*a.x+y*a.y;
    }

    LL operator ^ (const node &a) const
    {
        return x*a.y-y*a.x;
    }

    bool operator < (const node &a) const
    {
        if(y*a.y <= 0)
        {
            if(y>0||a.y>0) return y<a.y;

            if(y ==0 && a.y ==0) return x<a.x;
        }

        return ((*this)^a)>0;
    }
}Point;

Point p[Max];

int n;

int main()
{
    while(~scanf("%d",&n))
    {
        LL sum1 = 0,sum2 = 0;

        for(int i = 0;i<n;i++)
        {
            scanf("%lld %lld",&p[i].x,&p[i].y);
        }

        vector<Point>vp;

        for(int z = 0;z<n;z++)
        {
            vp.clear();

            for(int j = 0;j<n;j++)
            {
                if(z ==j) continue;

                vp.push_back(p[j]-p[z]);
            }

            sort(vp.begin(),vp.end());

            vp.insert(vp.end(),vp.begin(),vp.end());

            for(int i = 0,j = 0,k = 0,r = 0;i<n-1;i++)
            {
                while(j<i+n-1 && !(vp[i]^vp[j]) && vp[i]*vp[j]>0) j++;  

                k = max(k,j);

                while(k<i+n-1 &&((vp[i]^vp[k])>0) &&(vp[i]*vp[k])>0) k++;

                r = max(r,k);

                while(r<i+n-1&& ((vp[i]^vp[r])>0)) r++;

                sum1+=(k-j);

                sum2+=(r-k);
            }
        }

        printf("%lld\n",(sum1-2*sum2)/3);
    }
    return 0;
}

HDU【5787】——K-wolf Number

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Alice thinks an integer x is a K-wolf number, if every K adjacent digits in decimal representation of x is pairwised different.
Given (L,R,K), please count how many K-wolf numbers in range of [L,R].

Input

The input contains multiple test cases. There are about 10 test cases.

Each test case contains three integers L, R and K.

1≤L≤R≤1e18
2≤K≤5

Output

For each test case output a line contains an integer.

Sample Input

1 1 2
20 100 5

Sample Output

1
72

Author

ZSTU

Source

2016 Multi-University Training Contest 5

题意:询问区间[L,R]中有多少的数字相邻的k位数字都是不同的。数位dp的模板,我们定义dp[i][p1][p2][p3][p4] 由于k最大为5所以我们只需要开四维,然后就是模板了。

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL dp[20][11][11][11][11];

int  digt[20],K,len;

bool ok(int p1,int p2,int p3,int p4,int now)
{
    if(K == 2) return p4 != now;

    if(K == 3) return p3 != now && p4 != now;

    if(K == 4) return p2 != now && p3 != now && p4 != now;

    if(K == 5) return p1 != now && p2 != now && p3 != now && p4 != now;
}

LL dfs(int pos,int p1,int p2,int p3,int p4,bool limit)
{
    if(pos ==0) return 1;

    LL & ds = dp[pos][p1][p2][p3][p4];

    if(!limit && ds !=-1) return ds;

    int n = limit ? digt[pos]:9;

    LL res = 0;

    for(int i = 0;i<=n;i++)
    {
        if(p4 == 10 && i == 0)
        {
            res +=dfs(pos-1,p2,p3,p4,10,limit && i == n);
        }
        else if(ok(p1,p2,p3,p4,i))
        {
            res +=dfs(pos-1,p2,p3,p4,i,limit && i==n);
        }
    }

    if(!limit) ds= res;

    return res;
}

LL Cal(LL n)
{
    memset(dp,-1,sizeof(dp));

    len = 0;

    while(n)
    {
        digt[++len] = n%10;

        n/=10;
    }

    return dfs(len,10,10,10,10,1);
}

int main()
{
    LL L,R;

    while(~scanf("%lld %lld %d",&L,&R,&K))
    {
        printf("%lld\n",Cal(R)-Cal(L-1));
    }
    return 0;
}

HDU【5791】——Two

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A’ and sequence B’ are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A’ is a subsequence of A. B’ is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.

Input

The input contains multiple test cases.
For each test case, the first line cantains two integers N,M(1≤N,M≤1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.

Output

For each test case, output the answer mod 1000000007.

Sample Input

3 2
1 2 3
2 1
3 2
1 2 3
1 2

Sample Output

2
3

Author

ZSTU

给你两个序列,判断两个序列的子序列相同的组合有多少种。类似最大公共子序列的做法,dp[i][j]表示分别以i结尾和以j结尾的序列相同的子序列的组合,那么dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1],如果i和j的元素相同,那么dp[i][j]+=dp[i-1][j-1]+1

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const LL Mod = 1000000007;

const int Max = 1100;

LL dp[Max][Max];

int a[Max],b[Max];

int main()
{
    int n,m;

    while(~scanf("%d %d",&n,&m))
    {
        memset(dp,0,sizeof(dp));

        for(int i = 1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }

        for(int i = 1;i<=m;i++)
        {
            scanf("%d",&b[i]);
        }

        for(int i=  1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                dp[i][j]=  ((dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])%Mod+Mod)%Mod;

                if(a[i] == b[j])
                {
                    dp[i][j] = (dp[i][j]+dp[i-1][j-1]+1)%Mod;
                }
            }
        }

        printf("%lld\n",dp[n][m]);
    }
    return 0;
}

HDU【5792】——World is Exploding

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a < b ≤ n,1≤c < d≤n,Aa< Ab,Ac>Ad.

Input

The input consists of multiple test cases.
Each test case begin with an integer n in a single line.

The next line contains n integers A1,A2⋯An.
1≤n≤50000
0≤Ai≤1e9

Output

For each test case,output a line contains an integer.

Sample Input

4
2 4 1 3
4
1 2 3 4

Sample Output

1

题意:给你一个序列,问序列中如题意描述的组合有多少中。我们可以处理出以某个值为尾的上升组合,下降组合,以某个数为开始的上升组合和下降组合。我们可以算出总的组合(上升和下降的乘积),再去除不合法的。

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int Max = 55000;

int a[Max],b[Max];

LL Tr[Max];

LL lu[Max],ld[Max],ru[Max],rd[Max];

int num[Max];

int n,m;

int sum[Max];

int Lowbite(int x)
{
    return x&(-x);
}

void Up(int x)
{
    while(x<=m)
    {
        Tr[x]++;

        x+=Lowbite(x);
    }
}

LL Qy(int x)
{
    LL ans =0;

    while(x>0)
    {
        ans+=Tr[x];

        x-=Lowbite(x);
    }

    return ans;
}

LL Sum(LL *st,int l,int r)
{
    LL ans =0;

    for(int i = l;i<=r;i++) ans+=st[i];

    return ans;
}

void Out(LL *st,int l,int r)
{
    for(int i = l;i<=r;i++)
    {
        if(i!=l) printf(" ");

        printf("%lld",st[i]);
    }
    printf("\n");
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i = 0;i<n;i++)
        {
            scanf("%d",&a[i]);

            b[i] =a[i];
        }

        sort(a,a+n);

        m = 0;

        for(int i = 1;i<n;i++) if(a[m]!= a[i]) a[++m] =a[i];

        m++;

        for(int i = 1;i<=n;i++) num[i] = lower_bound(a,a+m,b[i-1])-a+1;

        memset(Tr,0,sizeof(Tr));

        memset(sum,0,sizeof(sum));

        for(int i = 1;i<=n;i++)
        {
            ru[i] = Qy(num[i]-1);

            rd[i] = (i-1-ru[i]-sum[num[i]]);

            Up(num[i]);

            sum[num[i]] ++;
        }

        memset(Tr,0,sizeof(Tr));

        memset(sum,0,sizeof(sum));

        for(int i = n;i>=1;i--)
        {
            lu[i] = Qy(num[i]-1);

            ld[i] = (n-i-lu[i]-sum[num[i]]);

            Up(num[i]);

            sum[num[i]]++;
        }

        LL ans =Sum(ru,1,n)*Sum(lu,1,n);


        for(int i = 1;i<=n;i++)
        {
            ans -=(ru[i]*rd[i]+lu[i]*ld[i]+ru[i]*lu[i]+rd[i]*ld[i]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(2016 Multi-University Training Contest 5)