Good Bye 2020

Supplementing

A. Bovine Dilemma

Question portal:

A. Bovine Dilemma

Ideas:

As long as violence finds how many triangle bases of different lengths are there

AC Code

#include<bits/stdc++.h>
using namespace std;
int x[100];
int ans[100];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&x[i]);
        int num=0;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<n;i++)
        {
    
    
            for(int j=i+1;j<=n;j++)
            {
    
    
                if(ans[x[j]-x[i]]==0)
                {
    
    
                    ans[x[j]-x[i]]=1;
                    num++;
                }
            }
        }
        printf("%d\n",num);
    }
    //system("pause");
    return 0;
}

B. Last minute enhancements

Question portal:

B. Last minute enhancements

Ideas:

Simple simulation is enough. If the current number has already appeared, add one and record each number that has appeared.

AC Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int a[N];
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        int num=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
    
    
            if(a[i]>ans)
            {
    
    
                ans=a[i];
                num++;
            }
            else if(a[i]==ans)
            {
    
    
                ans++;
                num++;
            }
        }
        printf("%d\n",num);
    }
    //system("pause");
    return 0;
}

C. Canine poetry

Question portal:

C. Canine poetry

Ideas:

The title does not want any palindrome substrings. Then we think that a large palindrome must be composed of small palindrome. Then we can process the second character of each aa, the third character of aba, and the second and third characters of aaa.

AC Code

#include<iostream>
#include<string>
using namespace std;
int main()
{
    
    
	int t;
    cin >> t;
	while (t--)
	{
    
    
		string str;
        cin >> str;
		int n = str.size();
		int ans = 0;
		for (int i = 0;i < n;i++)
		{
    
    
			if (str[i] == '#')continue;
			if ( i + 2 <= n - 1&&str[i] == str[i + 2] )
			{
    
    
				ans++;
				str[i+2] = '#';
			}
			if ( i + 1 <= n - 1&&str[i] == str[i + 1])
			{
    
    
				ans++;
				str[i + 1] = '#';
			}
		}
		cout << ans << endl;
	}
}

D. 13th Labour of Heracles

Question portal:

D. 13th Labour of Heracles

Ideas:

Greedy thinking that if the degree of a point is n (greater than 1), it means that this point can be painted (n-1) times more colors, then every time the number of colors +1, we find the largest weight and can paint The point can be.

AC Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
int ans[N],f[N];
int num;
LL w[N];
bool cmp(int a,int b)
{
    
    
    return a>b;
}
int main()
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int n;
        scanf("%d",&n);
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
    
    
            scanf("%lld",&w[i]);
            sum=sum+w[i];
        }
        num=0;
        memset(ans,0,sizeof(ans));
        for(int i=1;i<n;i++)
        {
    
    
            int u,v;
            scanf("%d%d",&u,&v);
            ans[u]++;
            ans[v]++;
            if(ans[u]>=2) f[++num]=w[u];
            if(ans[v]>=2) f[++num]=w[v];
        }
        sort(f+1,f+1+num,cmp);
        for(int i=1;i<n;i++)
        {
    
    
            if(i==1) printf("%lld ",sum);
            else
            {
    
    
                sum=sum+f[i-1];
                printf("%lld ",sum);
            }
        }
        printf("\n");
    }
    //system("pause");
    return 0;
}

E. Apollo versus Pan (good question)

(Mathematics, thinking, bitwise operations)

Question portal:

E. Apollo versus Pan

Ideas:

The most direct idea is of course violence, so the complexity of n 3 will obviously explode, let alone n 3 , even n 2 will explode. Then we thought that there should be a preprocessing operation for each bit of each number with high probability. Let's change the formula first:
Insert picture description here
to
Insert picture description here
then obviously the problem is transformed into how to find the number in the two square brackets. We must enumerate every j, then for the i-th bit of the j-th number, if the current bit is 1, then only the number with the i-th bit as 1 in the AND operation can contribute 2 i , in the OR In the operation, only the i-th number is 0, I can contribute 2 i more , we only need to preprocess the number of 1 in each digit, then the answer is ready to come out.

AC Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5e5+10;
const LL mod=1e9+7;
LL num[75];
LL a[N],cnt[75];
LL quick_pow(LL a,LL b)
{
    
    
    LL res=1;
    while(b)
    {
    
    
        if(b%2) res=res*a%mod;
        a=a*a%mod;
        b=b/2;
    }
    return res%mod;
}
int main()
{
    
    
    int t;
    scanf("%d",&t);
    for(int i=0;i<=63;i++)
        cnt[i]=quick_pow(2,i);
    while(t--)
    {
    
    
        memset(num,0,sizeof(num));
        int n;
        scanf("%d",&n);
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
    
    
            LL x;
            scanf("%lld",&x);
            ans=(ans+x)%mod;
            a[i]=x;
            int k=0;
            while(x)
            {
    
    
                if(x%2) num[k]++;
                k++;
                x=x/2;
            }
        }
        LL sum=0;
        for(int i=1;i<=n;i++)
        {
    
    
            LL f1=0,f2=ans;
            LL x=a[i];
            int k=0;
            while(x)
            {
    
    
                if(x%2==1)
                {
    
    
                    f1=(f1+cnt[k]*num[k]%mod)%mod;
                    f2=(f2+cnt[k]*(n-num[k])%mod)%mod;
                }
                k++;
                x=x/2;
            }
            sum=(sum+f1*f2%mod)%mod;
        }
        printf("%lld\n",sum);
    }
    //system("pause");
    return 0;
}

F. Euclid's nightmare (good question)

(Thinking and checking)

Title:

The title of this question is more difficult to read. I also read other people's solutions to know what this question means (I am old-fashioned b). The definition of Z2 is a modulo-2 addition. Give you some vectors, and ask you to find out which vectors are the least necessary and can combine the most different vectors.

Ideas:

A vector has two dimensions at most 1, so we can consider and search the set. For a vector, if the two dimensions of 1 in this vector are not in a connected component, it means that this vector is the vector in the minimum cover, and the two dimensions of 1 in this vector are connected together. At the same time, if the two dimensions in this vector are already in a connected component, it means that this vector can be added by the previous different vectors. Some vectors have only one dimension as 1. We add one dimension to this vector and expand it so that the m+1 dimension of this vector is 1, and then we can use the union search set. I think this model is very similar to the extremely irrelevant group of vectors in linear algebra. Maybe friends who have studied linear algebra will feel the same way.

AC Code

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5e5+10;
const LL mod=1e9+7;
int fa[N];
int find(int x)
{
    
    
    if(x==fa[x]) return x;
    return fa[x]=find(fa[x]);
}
queue<int>que;
bool Union(int x,int y)
{
    
    
    int dx=find(x);
    int dy=find(y);
    if(dx==dy) return false;
    else
    {
    
    
        fa[dx]=dy;
        return true;
    }
}
LL quick_pow(LL x,LL y)
{
    
    
    LL res=1;
    while(y)
    {
    
    
        if(y%2) res=res*x%mod;
        x=x*x%mod;
        y=y/2;
    }
    return res;
}
int num=0;
int main()
{
    
    
    int n,m;    //向量的个数和向量的维数
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m+1;i++)
        fa[i]=i;
    for(int i=1;i<=n;i++)
    {
    
    
        int k;
        scanf("%d",&k);
        int a,b;
        if(k==1)
        {
    
    
            scanf("%d",&a);
            if(Union(a,m+1))
            {
    
    
                que.push(i);
                num++;
            }
        }
        else
        {
    
    
            scanf("%d%d",&a,&b);   
            if(Union(a,b))
            {
    
    
                que.push(i);
                num++;
            }
        }
    }
    printf("%lld %d\n",quick_pow(2,num),num);
    while(!que.empty())
    {
    
    
        printf("%d ",que.front());
        que.pop();
    }
    printf("\n");
    //system("pause");
    return 0;
}

Guess you like

Origin blog.csdn.net/Stevenwuxu/article/details/112758499