SRM 578 DIV 2

250:

简单题目:

500:

题意:

给定一个矩形,里面要么是"v"表示,要么是".",v表示可能是g,也可能是d,如果是g的话,那么它的哈弗曼距离dis之内如果是v的话,一定是g。求有多少种满足条件的可能数。

思路:

将每一个块分出来,自这一联通块里面,所有的v要么是g,要么d,bfs把所有的快求出来,假设为n,则最后的总数为2^n - 1

View Code
#line 5 "GooseInZooDivTwo.cpp"

#include <iostream>

#include <cstdio>

#include <cmath>

#include <vector>

#include <cstring>

#include <algorithm>

#include <string>

#include <set>

#include <functional>

#include <numeric>

#include <sstream>

#include <stack>

#include <map>

#include <queue>



#define CL(arr, val)    memset(arr, val, sizeof(arr))



#define lc l,m,rt<<1

#define rc m + 1,r,rt<<1|1

#define pi acos(-1.0)

#define ll long long

#define L(x)    (x) << 1

#define R(x)    (x) << 1 | 1

#define MID(l, r)   (l + r) >> 1

#define Min(x, y)   (x) < (y) ? (x) : (y)

#define Max(x, y)   (x) < (y) ? (y) : (x)

#define E(x)        (1 << (x))

#define iabs(x)     (x) < 0 ? -(x) : (x)

#define OUT(x)  printf("%I64d\n", x)

#define lowbit(x)   (x)&(-x)

#define Read()  freopen("din.txt", "r", stdin)

#define Write() freopen("dout.txt", "w", stdout);

#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)



#define M 107

#define N 107

using namespace std;



const ll mod = 1000000007;



struct node

{

    int x,y;

}nd;

int n,m;



vector<string> tmp;



int Abs(int x)

{

    if (x >= 0) return x;

    else return (-x);

}

void bfs(int x,int y,int k)

{

    queue<node>q;

    nd.x = x; nd.y = y;

    q.push(nd);

    int i,j;

    while (!q.empty())

    {

        node u = q.front(); q.pop();



        for (i = max(0,u.x - k); i <= min(n - 1, u.x + k); ++i)

        {

            for (j = max(0,u.y - k); j <= min(m - 1,u.y + k); ++j)

            {

                if (Abs(u.x - i) + Abs(u.y - j) <= k && tmp[i][j] == 'v')

                {

                    tmp[i][j] = 'g';

                    nd.x = i,nd.y = j;

                    q.push(nd);

                }

            }

        }

    }

}



class GooseInZooDivTwo

{

        public:



        int count(vector <string> fd, int dis)

        {

            int i,j;

            n = fd.size(); m = fd[0].size();

            tmp.clear();

            for (i = 0; i < n; ++i)

            {

                tmp.push_back(fd[i]);

            }

            n = fd.size(); m = fd[0].size();

            ll cnt = 0;

            for (i = 0;  i < n; ++i)

            {

                for (j = 0; j < m; ++j)

                {

                    if (tmp[i][j] == 'v')

                    {

                        cnt++;

                        tmp[i][j] = 'g';

                        bfs(i,j,dis);

                    }

                }

            }

//            cout<<cnt<<endl;

            ll ans = 1;

            for (i = 0; i < cnt; ++i)

            {

                ans = ans*2%mod;

            }

            return ans - 1;

        }





};





// Powered by FileEdit

// Powered by TZTester 1.01 [25-Feb-2003]

// Powered by CodeProcessor





// Powered by FileEdit

// Powered by TZTester 1.01 [25-Feb-2003]

// Powered by CodeProcessor





// Powered by FileEdit

 

1000:

题意:
个顶n个段,每个段里面要么有狼,要么没狼,然后给出m个区域间,[l[i],r[i]]表示在区间内至少有一只狼,问满足条件的一共有多少种可能数?

思路:

记忆化搜索DP , dp[i][j]表示到i个段,有j个区间还没有处理, 对于第i个段我们有两种选择要么方狼,要么不放狼。 如果方狼的话,dp[i][j] = dp[i - 1][k] k表示必须多少个区间被处理了。

如果不放的话,要必须满足区间条件。 dp[i][j] += dp[i - 1][j] 

View Code
#line 5 "WolfInZooDivTwo.cpp"

#include <iostream>

#include <cstdio>

#include <cmath>

#include <vector>

#include <cstring>

#include <algorithm>

#include <string>

#include <set>

#include <functional>

#include <numeric>

#include <sstream>

#include <stack>

#include <map>

#include <queue>



#define CL(arr, val)    memset(arr, val, sizeof(arr))



#define lc l,m,rt<<1

#define rc m + 1,r,rt<<1|1

#define pi acos(-1.0)

#define ll long long

#define L(x)    (x) << 1

#define R(x)    (x) << 1 | 1

#define MID(l, r)   (l + r) >> 1

#define Min(x, y)   (x) < (y) ? (x) : (y)

#define Max(x, y)   (x) < (y) ? (y) : (x)

#define E(x)        (1 << (x))

#define iabs(x)     (x) < 0 ? -(x) : (x)

#define OUT(x)  printf("%I64d\n", x)

#define lowbit(x)   (x)&(-x)

#define Read()  freopen("din.txt", "r", stdin)

#define Write() freopen("dout.txt", "w", stdout);

#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)



#define M 107

#define N 307

using namespace std;



const ll mod = 1000000007;



struct node

{

    int l,r;

}nd[N];



ll pow2[N];

ll dp[N][N];

int off[N],cnt;



ll DP(int x,int y)

{

     int i;

     if (x < 0) return 1;

     if (dp[x][y] != -1) return dp[x][y];

     if (y == 0) return (dp[x][y] = pow2[x + 1]);



     dp[x][y] = 0;

     for (i = y; i >= 1; --i)

     {

         if (x > nd[i].r) break;

     }

     dp[x][y] += DP(x - 1,i);

     dp[x][y] %= mod;



     for (i = y; i >= 1; --i)

     {

         if (x == nd[i].l) break;

     }

     if (i == 0) dp[x][y] += DP(x - 1, y);



     dp[x][y] %= mod;

     return dp[x][y];

}

vector<int> getR(string s)

{

    istringstream is(s);

    int no;

    vector<int> rs;

    while (is >> no)

    {

        rs.push_back(no);

    }

    return rs;

}

class WolfInZooDivTwo

{

        public:

        void init()

        {

            pow2[0] = 1;

            for (int i = 1; i <= 300; ++i)

            {

                pow2[i] = pow2[i - 1]*2%mod;

            }

        }

        int count(int n, vector <string> L, vector <string> R)

        {

            int i;

            init();

            string lstr = "",rstr = "";

            for (size_t i = 0; i < L.size(); ++i) lstr += L[i];

            for (size_t i = 0; i < R.size(); ++i) rstr += R[i];



            vector<int> Ls = getR(lstr);

            vector<int> Rs = getR(rstr);

            if (Ls.size() != Rs.size()) puts("BUBIUBYUGB");

            set<int> sr(Rs.begin(),Rs.end());



            CL(off,-1);

            int m = Ls.size();

            for (i = 0; i < m; ++i) off[Rs[i]] = max(off[Rs[i]],Ls[i]);

            cnt = 0;

            set<int>::iterator it = sr.begin();

            for (; it != sr.end(); ++it)

            {

                nd[++cnt].l = off[*it];

                nd[cnt].r = *it;

            }

            CL(dp,-1);

            return DP(n - 1,cnt);

        }

};

 

你可能感兴趣的:(div)