码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #293 Div2 F(Pasha and Pipe)

时间:2015-03-05 23:47:26      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:模拟   构造   贪心   

Problem

技术分享
技术分享
技术分享

Limits

TimeLimit(ms):4000

MemoryLimit(MB):512

n,m[2,2000]

Look up Original Problem From here

Solution

一共就14种情况。有些情况可以合并一起算,算出来就好了。

Complexity

TimeComplexity:O(n×m)

MemoryComplexity:O(n×m)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uin;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN
#define N 2015
#define M 4
char grid[N][N];
bool a[N][N][M];
int n,m;
ll ans=0;
int main(){
    scanf("%d %d",&n,&m);
    repin(i,1,n){
        scanf("%s",grid[i]+1);
    }
    repin(i,1,n){
        bool all=true;
        int p=m;
        repin(j,1,m){
            if(grid[i][j]==‘#‘){
                all=false;
                p=j-1;
                break;
            }
        }
        depin(j,p,1){
            a[i][j][1]=1;
        }
        all=true;
        p=1;
        depin(j,m,1){
            if(grid[i][j]==‘#‘){
                all=false;
                p=j+1;
                break;
            }
        }
        repin(j,p,m){
            a[i][j][3]=1;
        }
        if(all&&i!=1&&i!=n) ans+=1;
    }
    repin(j,1,m){
        bool all=true;
        int p=n;
        repin(i,1,n){
            if(grid[i][j]==‘#‘){
                all=false;
                p=i-1;
                break;
            }
        }
        depin(i,p,1){
            a[i][j][0]=1;
        }
        all=true;
        p=1;
        depin(i,n,1){
            if(grid[i][j]==‘#‘){
                all=false;
                p=i+1;
                break;
            }
        }
        repin(i,p,n){
            a[i][j][2]=1;
        }
        if(all&&j!=1&&j!=m) ans+=1;
    }
    rep(i,2,n){
        rep(j,2,m){
            if(a[i][j][0]&&a[i][j][1]) ans+=1;
            if(a[i][j][0]&&a[i][j][3]) ans+=1;
            if(a[i][j][1]&&a[i][j][2]) ans+=1;
            if(a[i][j][2]&&a[i][j][3]) ans+=1;
        }
    }
    ll last2=0,last1=0;
    rep(i,2,n){
        last2=last1=0;
        rep(j,2,m){
            if(a[i][j][2]) ans+=last1;
            if(a[i][j][0]) ans+=last2;
            last2=last1;
            last1+=a[i][j][0];
            if(grid[i][j]==‘#‘) last2=last1=0;
        }
    }
    last2=last1=0;
    rep(j,2,m){
        last2=last1=0;
        dep(i,n-1,1){
            if(a[i][j][3]) ans+=last2;
            if(a[i][j][1]) ans+=last1;
            last2=last1;
            last1+=a[i][j][3];
            if(grid[i][j]==‘#‘) last2=last1=0;
        }
    }
    last2=last1=0;
    rep(j,2,m){
        last2=last1=0;
        dep(i,n-1,1){
            if(a[i][j][1]) ans+=last2;
            if(a[i][j][3]) ans+=last1;
            last2=last1;
            last1+=a[i][j][1];
            if(grid[i][j]==‘#‘) last2=last1=0;
        }
    }
    last2=last1=0;
    rep(i,2,n){
        last2=last1=0;
        rep(j,2,m){
            if(a[i][j][2]) ans+=last2;
            if(a[i][j][0]) ans+=last1;
            last2=last1;
            last1+=a[i][j][2];
            if(grid[i][j]==‘#‘) last2=last1=0;
        }
    }
    printf("%lld\n",ans);
}

Codeforces Round #293 Div2 F(Pasha and Pipe)

标签:模拟   构造   贪心   

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/44088337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!