2019牛客ACM暑期多校第八场

B-Beauty Values 

 

题目描述

 


  Gromah and LZR have entered the second level. There is a sequence a1,a2,⋯ ,ana_1, a_2, \cdots, a_na1​,a2​,⋯,an​ on the wall.           There is also a note board saying "the beauty value of a sequence is the number of different elements in the sequence".            LZR soon comes up with the password of this level, which is the sum of the beauty values of all successive subintervals of the sequence on the wall.          Please help them determine the password!      
输入描述:
The first line contains one positive integer nn_{}n​, denoting the length of the sequence.The second line contains nn_{}n​ positive integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_na1​,a2​,⋯,an​, denoting the sequence.1≤ai≤n≤1051 \le a_i \le n \le 10^51≤ai​≤n≤105
输出描述:
Print a non-negative integer in a single line, denoting the answer.


示例1

 

输入
复制

4
1 2 1 3

 

输出
复制

18

 

说明

The beauty values of subintervals [1,1],[2,2],[3,3],[4,4][1,1], [2,2], [3,3], [4,4]_{}[1,1],[2,2],[3,3],[4,4]​ are all 11_{}1​.The beauty values of subintervals [1,2],[1,3],[2,3],[3,4][1,2], [1,3], [2,3], [3,4]_{}[1,2],[1,3],[2,3],[3,4]​ are all 22_{}2​.The beauty values of subintervals [1,4],[2,4][1,4], [2,4]_{}[1,4],[2,4]​ are all 33_{}3​.As a result, the sum of all beauty values are 1×4+2×4+3×2=181\times 4 + 2\times 4 + 3\times 2 = 181×4+2×4+3×2=18.

题意:给定一个序列,求所有子序列中不同元素个数的总和

遍历每个数,当去掉这个数时减去的是该数距它下一个出现的位置差。输入时储存一下后继就行

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+20;
int nex[N],a[N];
long long sum[N];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        map<int,int>mp;
        set<int>st;
        memset(nex,0,sizeof(nex));
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            st.insert(a[i]);
            sum[1]+=st.size();
            if(mp[a[i]]==0)
                mp[a[i]]=i;
            else
            {
                nex[mp[a[i]]]=i;
                mp[a[i]]=i;
            }
        }
//        for(int i=1;i<=n;i++)
//            cout<<nex[i]<<' ';
//        cout<<'\n';
//        cout<<sum[1]<<'\n';
        long long ans=sum[1];
        for(int i=2;i<=n;i++)
        {
            if(nex[i-1]!=0)
                sum[i]=sum[i-1]-(nex[i-1]-i+1);
            else
                sum[i]=sum[i-1]-(n-i+2);
            ans+=sum[i];
        }
//        for(int i=1;i<=n;i++)
//            cout<<sum[i]<<' ';
//        cout<<'\n';
        cout<<ans<<'\n';
    }
    return 0;
}
/*
8
1 2 4 1 3 4 3 5
*/

C-CDMA

 

题目描述

Gromah and LZR have entered the third level. There is a blank grid of size m×mm\times mm×m, and above the grid is a word "CDMA".

 

In CDMA Technology, a Technology about computer network, every network node should be appointed a unique binary sequence with a fixed and the same length. Moreover, if we regard 00_{}0​ in the sequence as −1-1_{}−1​, and regard 11_{}1​ as +1+1_{}+1​, then these sequences should satisfy that for any two different sequences s,ts,t_{}s,t​, the inner product of s,ts,t_{}s,t​ should be exactly 00_{}0​.

 

The inner product of two sequences s,ts,t_{}s,t​ with the same length nn_{}n​ equals to ∑i=1nsiti\sum_{i=1}^{n} s_it_i∑i=1n​si​ti​.

 

So, the key to the next level is to construct a grid of size m×mm\times mm×m, whose elements are all −1-1_{}−1​ or 11_{}1​, and for any two different rows, the inner product of them should be exactly 00_{}0​.

 

In this problem, it is guaranteed that mm_{}m​ is a positive integer power of 22_{}2​ and there exists some solutions for input mm_{}m​. If there are multiple solutions, you may print any of them.

输入描述:

 

Only one positive integer mm_{}m​ in one line.

 

m∈{2k  ∣  k=1,2,⋯ ,10}m \in \{2^k \; | \;k = 1, 2, \cdots, 10\}m∈{2k∣k=1,2,⋯,10}

输出描述:

Print mm_{}m​ lines, each contains a sequence with length mm_{}m​, whose elements should be all −1-1_{}−1​ or 11_{}1​ satisfying that for any two different rows, the inner product of them equals 00_{}0​.

You may print multiple blank spaces between two numbers or at the end of each line, and you may print any number of blank characters at the end of whole output file.

示例1

输入

复制

2

输出

复制

1 1
1 -1

说明

The inner product of the two rows is 1×(−1)+1×1=01\times(-1) + 1\times 1 = 01×(−1)+1×1=0.

题意:任意输出一个m*m的矩阵,满足任意两行相同元素乘积之和为0

wotaicailebaqwqwq

找到一个满足题意的2阶矩阵,之后的多阶肯定与2阶的有关

1 1

1 -1

四阶的话把二阶套到里面的1里,-1的地方取反qwq  以此类推。

#include<bits/stdc++.h>
using namespace std;
int mp[2050][2050];
int main()
{
    int n;
    mp[1][1]=1;
    mp[1][2]=1;
    mp[2][1]=1;
    mp[2][2]=-1;
    for(int k=2; k<=1024; k*=2)
    {
//        cout<<k<<endl;
        for(int i=1; i<=k; i++)
        {
            for(int j=1; j<=k; j++)
            {
                mp[i+k][j]=mp[i][j];
            }
            for(int j=1; j<=k; j++)
            {
                mp[i][j+k]=mp[i][j];
            }
            for(int j=1; j<=k; j++)
            {
                mp[i+k][j+k]=-mp[i][j];
            }
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                cout<<mp[i][j];
                if(j!=n)
                {
                    cout<<' ';
                }
            }
            cout<<'\n';
        }
//        cout<<"YES"<<endl;
    }
}

G-Gemstones 

 

题目描述

Gromah and LZR have entered the seventh level. There are a sequence of gemstones on the wall.

 

After some tries, Gromah discovers that one can take exactly three successive gemstones with the same types away from the gemstone sequence each time, after taking away three gemstones, the left two parts of origin sequence will be merged to one sequence in origin order automatically.

 

For example, as for "ATCCCTTG", we can take three 'C's away with two parts "AT", "TTG" left, then the two parts will be merged to "ATTTG", and we can take three 'T's next time.

 

The password of this level is the maximum possible times to take gemstones from origin sequence.

 

Please help them to determine the maximum times.

输入描述:

 

Only one line containing a string ss_{}s​, denoting the gemstone sequence, where the same letters are regarded as the same types.

 

 

1≤∣s∣≤1051 \le |s| \le 10^51≤∣s∣≤105

 

ss_{}s​ only contains uppercase letters.

输出描述:

Print a non-negative integer in a single line, denoting the maximum times.

示例1

输入

复制

ATCCCTTG

输出

复制

2

说明

One possible way is that ‘‘ATCCCTTG "  →  ‘‘ATTTG "  →‘‘AG "``ATCCCTTG\,"  \; \rightarrow \; ``ATTTG\," \; \rightarrow ``AG\,"‘‘ATCCCTTG"→‘‘ATTTG"→‘‘AG".

题意:3个相同的字母可以消掉,问可以消多少次

温暖的单调栈裸题

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int len=s.size();
        if(len<3)
            cout<<0<<'\n';
        else
        {
            stack<char>st;
            int ans=0;
            st.push(s[0]);
            st.push(s[1]);
            for(int i=2; i<len; i++)
            {
                if(st.size()>=2)
                {
                    char c=st.top();
                    if(c==s[i])
                    {
                        st.pop();
                        if(st.top()==s[i])
                        {
                            st.pop();
                            ans++;
                        }
                        else
                        {
                            st.push(c);
                            st.push(s[i]);
                        }
                    }
                    else
                        st.push(s[i]);
                }
                else
                    st.push(s[i]);
            }
            cout<<ans<<'\n';
        }
    }
    return 0;
}
 
///ABBBAATCHCCCHHCCTT

 

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 12:19
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务