NOIP模拟赛11

T1 [HAOI2016]放棋子

https://daniu.luogu.org/problem/show?pid=3182

障碍交换行不影响

所以第i列有障碍的行换到第i行

然后错排公式

本校自测要写压位高精,不写了。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int len;
    int num[401];
    void clear()
    {
        len=0;
        memset(num,0,sizeof(num));
    }
    void operator = (int a)
    {
        len=0;
        while(a) len++,num[len]=a%10,a/=10;
    }
    void operator = (node a)
    {
        len=a.len;
        for(int i=1;i<=len;i++) num[i]=a.num[i];
    }
    node operator + (node a)
    {
        node c; c.clear();
        memset(c.num,0,sizeof(c.num));
        int L=max(a.len,len);
        c.len=L;
        for(int i=1;i<=L;i++) c.num[i]=num[i]+a.num[i];
        for(int i=1;i<=L;i++)
            if(c.num[i]>9) c.num[i+1]+=c.num[i]/10,c.num[i]%=10;
        if(c.num[L+1]) c.len++;
        while(c.num[c.len]>9) c.num[c.len+1]=c.num[c.len]/10,c.num[c.len]%=10,len++;
        if(c.num[c.len+1]) c.len++;
        return c;
    }
    node operator * (int a)
    {
        node c; c.clear();
        int L;
        c.len=L=len;
        for(int i=1;i<=L;i++) c.num[i]=num[i]*a;
        for(int i=1;i<=L;i++)
            if(c.num[i]>9) c.num[i+1]+=c.num[i]/10,c.num[i]%=10;
        if(c.num[L+1]) c.len++;
        while(c.num[c.len]>9) c.num[c.len+1]=c.num[c.len]/10,c.num[c.len]%=10,len++;
        if(c.num[c.len+1]) c.len++;
        return c;
    }
    void print()
    {
        for(int i=len;i;i--) printf("%d",num[i]);
    }
};
node f[201];
int main()
{
//    freopen("firstmeet.in","r",stdin);
//    freopen("firstmeet.out","w",stdout);
    int n;
    scanf("%d",&n);
    f[1]=0,f[2]=1,f[3]=2;
    for(int i=4;i<=n;i++)     
    {
        f[i].clear();
        f[i]=(f[i-1]+f[i-2])*(i-1);
    }
    f[n].print();
}
View Code

T2  [Usaco2010 Dec]Exercise 奶牛健美操

http://www.lydsy.com/JudgeOnline/problem.php?id=2097

树形DP

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100001
using namespace std;
int front[N],to[N<<1],nxt[N<<1],tot;
int dp[N],tmp[N];
int cnt,p;
void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}
void add(int u,int v)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
}
bool dfs(int x,int y,int z)
{
    int sum=0;
    for(int i=front[x];i;i=nxt[i])
        if(to[i]!=y) { if(!dfs(to[i],x,z)) return false; }
    for(int i=front[x];i;i=nxt[i]) 
        if(to[i]!=y) tmp[++sum]=dp[to[i]]+1;
    sort(tmp+1,tmp+sum+1);
    for(sum;tmp[sum]+tmp[sum-1]>z;sum--,cnt++);
    dp[x]=tmp[sum];
    if(cnt>p) return false; 
    return true;
}
bool check(int mid)
{
    memset(dp,0,sizeof(dp));
    cnt=0;
    return dfs(1,0,mid);
}
int main()
{
    /*  int size = 256 << 15;
    char *pp = (char*) malloc(size) + size;  
    __asm__ ("movl %0, %%esp
" :: "r"(pp));
    freopen("longnosee.in","r",stdin);
    freopen("longnosee.out","w",stdout); */
    int n;
    read(n), read(p);
    int u,v;
    for(int i=1;i<n;i++)
    {
        read(u),read(v);
        add(u,v);
    }
    int l=1,r=n,mid,ans;
    while(l<=r)
    {
        mid=l+r>>1;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d",ans);
}
View Code

T3

大爆搜,粘std啦

#include <cstdio>
#include <iostream>
#define rg register
#define Max 22
inline void read (int &now)
{
    rg char c = getchar ();
    for (now = 0; !isdigit (c); c = getchar ());
    for (; isdigit (c); now = now * 10 + c - '0', c = getchar ());
}
int _x[] = { 1, -1, 0, 0, 0, 0 }, _y[] = { 0, 0, 1, -1, 0, 0 }, _z[] = { 0, 0, 0, 0, 1, -1 };
int N, M, Z, P, Sx, Sy, Sz, St, Tt,Tx, Ty, Tz, Answer; bool is[Max][Max][Max], dot[Max][Max][Max];
inline bool Can (int x, int y, int z) { return x > 0 && x <= N && y > 0 && y <= M && z > 0 && z <= Z && !is[x][y][z] && !dot[x][y][z]; }
inline int abs (int a) { return a < 0 ? -a : a; }
void Dfs (int x, int y, int z, int t, int s)
{
    int r = abs (x - Tx) + abs (y - Ty) + abs (z - Tz);
    if (s + (r >> 2) >= Answer)    return ;
    if (x == Tx && y == Ty && z == Tz) { if (t == Tt) Answer = s; return ; }
    rg int i, a, b, c;
    if (Can (x + _x[t], y + _y[t], z + _z[t]) && Can (x + _x[t] * 2, y + _y[t] * 2, z + _z[t] * 2))
    {
        is[x + _x[t]][y + _y[t]][z + _z[t]] = true;
        is[a = (x + _x[t] * 2)][b = (y + _y[t] * 2)][c = (z + _z[t] * 2)] = true;
        for (i = 0; i < 6; ++ i)
            if ((i >> 1) != (t >> 1) && Can (a + _x[i], b + _y[i], c + _z[i]) && Can (a + _x[i] * 2, b + _y[i] * 2, c + _z[i] * 2))
            {
                is[a + _x[i]][b + _y[i]][c + _z[i]] = true;
                is[a + _x[i] * 2][b + _y[i] * 2][c + _z[i] * 2] = true;
                Dfs (a + _x[i] * 2, b + _y[i] * 2, c + _z[i] * 2, i, s + 1);
                is[a + _x[i]][b + _y[i]][c + _z[i]] = false;
                is[a + _x[i] * 2][b + _y[i] * 2][c + _z[i] * 2] = false;
            }
        if (Can (x + _x[t] * 3, y + _y[t] * 3, z + _z[t] * 3))
        {
            is[a = (x + _x[t] * 3)][b = (y + _y[t] * 3)][c = (z + _z[t] * 3)] = true;
            for (i = 0; i < 6; ++ i)
                if ((i >> 1) != (t >> 1) && Can (a + _x[i], b + _y[i], c + _z[i]))
                {
                    is[a + _x[i]][b + _y[i]][c + _z[i]] = true;
                    Dfs (a + _x[i], b + _y[i], c + _z[i], i, s + 1);
                    is[a + _x[i]][b + _y[i]][c + _z[i]] = false;
                }
             is[a = (x + _x[t] * 3)][b = (y + _y[t] * 3)][c = (z + _z[t] * 3)] = false;
        }
        is[x + _x[t]][y + _y[t]][z + _z[t]] = false;
        is[x + _x[t] * 2][y + _y[t] * 2][z + _z[t] * 2] = false;
    }
}
int Main ()
{
    freopen ("blessyou.in", "r", stdin); freopen ("blessyou.out", "w", stdout);
    read (N), read (M), read (Z), read (P); rg int i, j; int x, y, z; rg char c;
    if (N <= 2 && M <= 2 && Z <= 2) return printf ("Dream Battle"), 0; 
    read (Sx), read (Sy), read (Sz);
    for (c = getchar (); c != 'x' && c != 'y' && c != 'z'; c = getchar ());
    St = (c - 'x') << 1;
    if (c == 'x') St += (Sx == N); else if (c == 'y') St += (Sy == M);
    else if (c == 'z') St += (Sz == Z); 
    Sx -= _x[St], Sy -= _y[St], Sz -= _z[St];
    read (Tx), read (Ty), read (Tz);
    for (c = getchar (); c != 'x' && c != 'y' && c != 'z'; c = getchar ());
    Tt = (c - 'x') << 1;
    if (c == 'x') Tt += (Tx == 1); else if (c == 'y') Tt += (Ty == 1);
    else if (c == 'z') Tt += (Tz == 1);
    for (i = 1; i <= P; ++ i) read (x), read (y), read (z), dot[x][y][z] = true;
    Answer = 13; Dfs (Sx, Sy, Sz, St, 0);
    if (Answer == 13) printf ("Dream Battle"); else printf ("%d", Answer);
    return 0;
}
int ZlycerQan = Main (); int main (int argc, char *argv[]) { return 0; }
View Code
原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/7562042.html