周润达 NOIP 模拟题 周任飞 题解

  • T1 :

暴力深搜。不解释。
代码:

#include
#include
#include
#define f(x,y,z) for (int (x) = (y) ; x <= (z) ; x ++ )
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll size = 100;
ll ans = 0;
ll a[size],b[size],c[size],d[size];
ll n;

ll dfs(ll nn,ll oi,ll whk)
{
    if (nn == n+1)
    {
        if (ans < oi*whk) ans = oi * whk;
        return 0;
    }
    if (oi - b[nn] > 0 ) dfs(nn+1 , oi-b[nn] , whk+a[nn]);
    else dfs(nn+1, 0 ,whk+a[nn]);//学文化课
    if (whk - d[nn] > 0 ) dfs(nn+1 , oi+c[nn] , whk-d[nn]); 
    else dfs(nn+1 , oi+c[nn]  ,0 );//颓oi 
}

int main(void)
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    scanf("%lld",&n);
    for (ll i=1;i<=n;i++)
    scanf("%lld%lld%lld%lld",&a[i],&b[i],&c[i],&d[i]);
    dfs (1,0,0);
    printf("%lld",ans); 
    return 0;
}
  • T2:

这里有一个很好的结论:因为是树形的路径,所以有一个区域内的联通块个数等于块内总的黑格子数减去相邻黑色块的对数。原因是一开始有黑块个数 K 个联通块,每有一对就相当于连了一条边,而连一条边就少一个联通块(因为是树形结构),所以可证其联通块个数为 Kn 。然后前缀和统计,注意统计边的时候可以把值给其上面的或左边的,注意查询的范围。预处理复杂度 O(N2) ,查询复杂度 O(1)
代码:

#include
#include
#include
#define f(x,y,z) for (int (x) = (y) ; x <= (z) ; x ++ )
using namespace std;
typedef int ll;
const ll MOD = 1000000007;
const ll size = 2010;
ll dop[size][size],l[size][size],up[size][size];
ll mat[size][size],tmp[size][size];
ll n,m,q;
char smat[size];

int main(void)
{
    scanf("%d%d%d",&n,&m,&q);
    for (ll i=1;i<=n;i++)
    {
        memset(smat,NULL,sizeof smat);
        scanf("%s",smat);   
        for (ll j=1;j<=m;j++)
        mat[i][j] = (int)(smat[j-1] - '0');
    }
    for (ll i=1;i<=n;i++)
    for (ll j=1;j<=m;j++)
    dop[i][j] = dop[i-1][j] + dop[i][j-1] - dop[i-1][j-1] + mat[i][j]; 

    for (ll i=1;i<=n;i++)
    for (ll j=1;j<=m;j++)
    {
        up[i][j] = up[i-1][j] + up[i][j-1] - up[i-1][j-1] ;
        if (mat[i+1][j]==1 && mat[i][j]==1) ++up[i][j];
    }

    for (ll i=1;i<=n;i++)
    for (ll j=1;j<=m;j++)
    if (mat[i][j] == 1 && mat[i][j+1]==1) l[i][j] = l[i-1][j] + l[i][j-1] -l[i-1][j-1] + 1; else
     l[i][j] = l[i-1][j] + l[i][j-1] - l[i-1][j-1]; 
    while (q--)
    {
        ll x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        ll all = dop[x2][y2] + dop[x1-1][y1-1] - dop[x1-1][y2] - dop[x2][y1-1];
        ll updot,ldot;
        updot = up[x2-1][y2] + up[x1-1][y1-1] - up[x1-1][y2] - up[x2-1][y1-1];
        ldot = l[x2][y2-1] + l[x1-1][y1-1] - l[x1-1][y2-1] - l[x2][y1-1];
        ll ans = all-updot-ldot;
        printf("%d\n",ans);
    }
    return 0;
}
  • T3


如果一个点被 n 条线段所过,那么其鬼畜值为 n(n1)/2 。所以实际上就是统计逆序对,因为如果有n条线段经过,就有 n(n1)/2 个逆序对,所以与原来的公式一样。但是这题又卡空间,所以我们把一条一条的维护,也就是从一开始到其最接近 mod 为一条,然后下一个为另一条的开始。计算时只有条之间有逆序对,然后推一推公式就完了。复杂度 O(N2)=O()
代码:

#include
#include
#include
#include
#define f(x,y,z) for (int (x) = (y) ; x <= (z) ; x ++ 
#define ni line[i].num
#define nj line[j].num
using namespace std;
typedef long long ll;
ll mod,n,begin,a,ans=0;
const ll MOD = 1000000007;
const ll size=100005;
struct WTF {
    ll str, num;
    inline ll operator * (const WTF &nima) {
         ll ste = (str -nima.str + a) / a;
         ll end = nima.num - ste + 1;
        if (end >= num) return num * ( 2 * ste + num-1)/2;
        return  end * (ste + nima.num )/2  + (ll)nima.num * (num - end);
    }
} line[size];ll cnt=0;
int main(void)
{
    scanf("%lld%lld%lld%lld",&n,&begin,&a,&mod);
    ll now = begin;ll y=0;
    while (y1 - now) /a + 1;
        if (y+num<=n)
        {
        line[cnt].num = num;
        (now += num*a)%=mod;
        y+=num;
        }
        else
        {
            line[cnt].num = n-y;
            y = n;
        }
    }
    for (ll i = 0;ifor (ll j = i+1 ; j<=cnt ; j++)
         ans+=line[i] * line [j];
    printf("%lld",ans);
    return 0;
}
  • 结语:

这套题大多数为思路题,所以还是有意义的。

你可能感兴趣的:(考试题解)