洛谷 P5614題解

吐槽:數據好像有點水,直接枚舉到200能夠得80 points。c++

另:我仍是太弱了,比賽的時候只有90 points,#7死卡不過去,最後發現是沒有判斷 \(z_1\)\(z_2\) 的範圍……優化


Subtask 1:

Method:spa

直接輸出4,完。code


Subtask 2:

Method:get

直接暴力枚舉 \(x\) , \(y\) , \(z\) ,判斷是否知足一下關係便可:
\[ \forall i\in\left[1,n\right],s.t.\left |a_i-x\right|\bigoplus \left|b_i-y\right| \bigoplus \left|c_i -z\right|=9 \]
時間複雜度:\(O(nM^3)\)\(n\leq 5\) ,能夠忽略 )it

能夠經過 \(60\%\) 的數據。io

Codeclass

#include<bits/stdc++.h>
#define int long long 
#define Maxn 10
using namespace std;
inline void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
int n,M;
int ans=0;
int a[Maxn],b[Maxn],c[Maxn];
void solve1()
{
    int flag=1;
    for(int x=1;x<=M;x++)
    {
        for(int y=1;y<=M;y++)
        {
            for(int z=1;z<=M;z++)
            {
                for(int i=1;i<=n;i++)
                {
                    if((((abs(a[i]-x))^(abs(b[i]-y)))^(abs(c[i]-z)))!=9)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    ans++;
                }else
                {
                    flag=1;
                }
            } 
        }
    }
    printf("%lld\n",ans);
    return ;    
} 
signed main()
{
    read(n),read(M);
    for(int i=1;i<=n;i++)
    {
        read(a[i]);
        read(b[i]);
        read(c[i]);
    }
    ans=0;
    if(M<=200)
    {
        solve1();
        return 0;
    }
}

Subtask 3:

Method :map

考慮優化枚舉。若知足一下的性質:
\[ A\bigoplus B\bigoplus C=D \]
則顯然能夠獲得:
\[ \begin{equation} \begin{aligned} & A\bigoplus B \bigoplus D\\ & =A \bigoplus B \bigoplus \left(A\bigoplus B\bigoplus C\right) \\ & = \left[\left(A \bigoplus B\right) \bigoplus \left(A\bigoplus B\right)\right]\bigoplus C\\ & \because x \bigoplus x=0,0\bigoplus x=x\\ & \therefore A \bigoplus B \bigoplus D =C \end{aligned} \end{equation} \]
故只須要枚舉 \(x\)\(y\) ,則:
\[ \begin{equation} \begin{aligned} & \left|c_1-z\right|=\left|a_1-x\right|\bigoplus \left|b_1-y\right| \bigoplus 9 \end{aligned} \end{equation} \]
咱們設
\[ C=\left|a_1-x\right|\bigoplus \left|b_1-y\right| \bigoplus 9 \]
則:
\[ z_1=c_1-C,z_2=c_1+C \]
最後只須要判斷一下 \(z_1\)\(z_2\) 是否知足如下性質便可:
\[ \forall i\in \left[2,n\right],s.t.\left |a_i-x\right|\bigoplus \left|b_i-y\right| \bigoplus \left|c_i -z_j\right|=9\text{且}z_j \in [1,M]\\ (j\in \{1,2\}) \]
時間複雜度:\(O(nM^2)\)\(n\leq 5\) ,能夠忽略 )數據

Code:

#include<bits/stdc++.h>
#define int long long 
#define Maxn 10
using namespace std;
inline void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
int n,M;
int ans=0;
int a[Maxn],b[Maxn],c[Maxn];
map<int,int>mp;
void solve1()
{
    int flag=1;
    for(int x=1;x<=M;x++)
    {
        for(int y=1;y<=M;y++)
        {
            for(int z=1;z<=M;z++)
            {
                for(int i=1;i<=n;i++)
                {
                    if((((abs(a[i]-x))^(abs(b[i]-y)))^(abs(c[i]-z)))!=9)
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==1)
                {
                    ans++;
                }else
                {
                    flag=1;
                }
            } 
        }
    }
    printf("%lld\n",ans);
    return ;    
} 
void solve2()
{
    int flag1=1,flag2=1;
    for(int x=1;x<=M;x++)
    {
        for(int y=1;y<=M;y++)
        {
            mp.clear();
            int tmp=(((abs(a[1]-x))^(abs(b[1]-y)))^9);
            int zkkk=c[1]-tmp;
            int zwww=c[1]+tmp;
            if(zkkk>=1&&zkkk<=M)
            {
                for(int i=2;i<=n;i++)
                {
                    if((((abs(a[i]-x))^(abs(b[i]-y)))^(abs(c[i]-zkkk)))!=9)
                    {
                        flag1=0;
                        break;
                    }
                }
                if(flag1==1&&mp.find(zkkk)==mp.end())
                {
                    ans++;
                    mp[zkkk]=1;
                }else
                {
                    flag1=1;
                }
            }else
            {
                flag1=1;
            }
            if(zwww>=1&&zwww<=M)
            {
                for(int i=2;i<=n;i++)
                {
                    if((((abs(a[i]-x))^(abs(b[i]-y)))^(abs(c[i]-zwww)))!=9)
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1&&mp.find(zwww)==mp.end())
                {
                    ans++;
                }else
                {
                    flag2=1;
                }
            }else
            {
                flag2=1;
            }
        }   
    }
    printf("%lld\n",ans);
    return ;
}
signed main()
{
    read(n),read(M);
    for(int i=1;i<=n;i++)
    {
        read(a[i]);
        read(b[i]);
        read(c[i]);
    }
    ans=0;
    if(M<=200)
    {
        solve1();
        return 0;
    }
    else 
    {
        solve2();
        return 0;
    }
}
相關文章
相關標籤/搜索