Bayan 2015 Contest Warm Up(C,D)

来源:互联网 发布:unity3d中文版本下载 编辑:程序博客网 时间:2024/05/17 12:50

B. Strongly Connected City

Imagine a city with n horizontal streets crossingm vertical streets, forming an(n - 1) × (m - 1) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection.

The mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern.

Input

The first line of input contains two integers n andm, (2 ≤ n, m ≤ 20), denoting the number of horizontal streets and the number of vertical streets.

The second line contains a string of length n, made of characters '<' and '>', denoting direction of each horizontal street. If thei-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south.

The third line contains a string of length m, made of characters '^' and 'v', denoting direction of each vertical street. If thei-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east.

Output

If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO".

Sample test(s)
Input
3 3><>v^v
Output
NO
Input
4 6<><>v^v^v^
Output
YES
Note

The figure above shows street directions in the second sample test case.

思路:只要最外面的边是逆时针或者顺时针就可以。

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;  int n,m;  char a[2][30];    int check(char c)  {      int k;      switch(c)      {          case 'v': return 1;          case '>': return 2;          case '^': return 3;          case '<': return 4;      }  }  bool solve()  {      int  b[4];      b[0] = check(a[0][0]);      b[1] = check(a[0][n - 1]);      b[2] = check(a[1][0]);      b[3] = check(a[1][m - 1]);      if(b[0] == 4 && b[1] == 2 && b[2] == 1 && b[3] == 3) return true;      if(b[0] == 2 && b[1] == 4 && b[2] == 3 && b[3] == 1) return true;      return false;  }  int main()  {      scanf("%d %d",&n,&m);      scanf("%s %s",a[0],a[1]);      puts(solve() ? "YES" : "NO");      return 0;  }  


C. Kamal-ol-molk's Painting

Rumors say that one of Kamal-ol-molk's paintings has been altered. A rectangular brush has been moved right and down on the painting.

Consider the painting as a n × m rectangular grid. At the beginning anx × y rectangular brush is placed somewhere in the frame, with edges parallel to the frame, (1 ≤ x ≤ n, 1 ≤ y ≤ m). Then the brush is moved several times. Each time the brush is moved one unit right or down. The brush has been strictly inside the frame during the painting. The brush alters every cell it has covered at some moment.

You have found one of the old Kamal-ol-molk's paintings. You want to know if it's possible that it has been altered in described manner. If yes, you also want to know minimum possible area of the brush.

Input

The first line of input contains two integers n andm, (1 ≤ n, m ≤ 1000), denoting the height and width of the painting.

The next n lines contain the painting. Each line hasm characters. Character 'X' denotes an altered cell, otherwise it's showed by '.'. There will be at least one altered cell in the painting.

Output

Print the minimum area of the brush in a line, if the painting is possibly altered, otherwise print - 1.

Sample test(s)
Input
4 4XX..XX..XXXXXXXX
Output
4
Input
4 4.....XXX.XXX....
Output
2
Input
4 5XXXX.XXXX..XX...XX..
Output
-1


首先可以纯暴力,找到符合条件的最小的长和宽,然后在根据这个长宽找到最符合条件的最小面积

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;#define N 1010int top;char s[N][N];int n, m, stx, sty;bool ok(int x, int y){    int nowx = stx, nowy = sty;    if(nowx + x-1 > n || nowy + y-1>m)return false;    for(int i = 0; i < x; i++)        for(int j = 0; j < y; j++)            if(s[i+nowx][j+nowy]!='X')return false;    int cnt = x*y;    while(1)    {        if(nowx + x <= n && s[nowx+x][nowy] =='X')        {            nowx ++;            for(int i = 0; i < y; i++)                if(s[nowx+x-1][i+nowy]!='X')                    return false;            cnt += y;        }        else if(nowy + y <= m && s[nowx][nowy+y] == 'X')        {            nowy++;            for(int i = 0; i < x; i++)                if(s[i+nowx][nowy+y-1]!='X')                    return false;            cnt += x;        }        else break;    }    return cnt == top;}int hehe;int feifei;int x, y;void find_xy(){    x = N, y = N;    for(int i = 1; i <= m; i++)    {        int cnt = 0;        for(int j = 1; j <= n; j++)        {            if(s[j][i]=='.')            {                if(cnt) x = min(x, cnt);                cnt = 0;            }            else                cnt++;        }        if(cnt) x = min(x, cnt);    }    for(int i = 1; i <= n; i++)    {        int cnt = 0;        for(int j = 1; j <= m; j++)        {            if(s[i][j]=='.')            {                if(cnt) y = min(y, cnt);                cnt = 0;            }            else cnt++;        }        if(cnt) y = min(y, cnt);    }}int solve(){    int ans = N*N;    for(int i = 1; i <= x; i++)        if(ok(i,y))        {            ans = i*y;            break;        }    for(int i = 1; i <= y && x*i<ans; i++)        if(ok(x,i))        {            ans = x*i;            break;        }    if(ans > n*m) return -1;    return ans;}void input(){    stx = -1;    top = 0;    for(int i = 1; i <= n; i++)    {        scanf("%s", s[i]+1);        for(int j = 1; j <= m; j++)        {            if(stx==-1 && s[i][j]=='X')            {                stx = i, sty = j;            }            top += s[i][j]=='X';        }    }}int main(){    while(cin>>n>>m)    {        input();        find_xy();        printf("%d\n", solve());    }    return 0;}

也可以先预处理一下,这样会快很多

#include <cstdio>  #include <algorithm>  #include <cstring>  #include <queue>  using namespace std;    #define sum(x1,y1,x2,y2) (g[x2][y2] - g[x1 - 1][y2] - g[x2][y1 - 1] + g[x1 - 1][y1 - 1])    const int INF = 0x3f3f3f3f;    const int N = 1005;  int n, m, g[N][N], ans = INF;  char str[N][N];    int dfs(int x,int y,int wx,int wy) {      if(sum(x, y + 1, x + wx - 1, y + wy) == wx * wy) return wx + dfs(x, y + 1, wx, wy);      if(sum(x + 1, y, x + wx, y + wy - 1) == wx * wy) return wy + dfs(x + 1, y, wx, wy);      return 0;  }    int main() {      scanf("%d%d",&n,&m);      int flag = 0,px,py;      for(int i = 1; i <= n; i++) scanf("%s",str[i] + 1);      for(int i = 1; i <= n; i++)          for(int j = 1; j <= m; j++) {              if(str[i][j] == 'X') {                  if(!flag) {flag = 1; px = i; py = j;}                  g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1] + 1;              }              else g[i][j] = g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1];          }      int tmp,wx,wy;      for(tmp = px; str[tmp][py] == 'X'; tmp++);      wx = tmp - px;      for(int i = py; str[px][i] == 'X'; i++)          if(dfs(px, py, wx, i - py + 1) + wx * (i - py + 1) == g[n][m])              ans = min(ans, wx * (i - py + 1));      for(tmp = py; str[px][tmp] == 'X'; tmp++);      wy = tmp - py;      for(int i = px; str[i][py] == 'X'; i++)          if(dfs(px, py, i - px + 1, wy) + (i - px + 1) * wy == g[n][m])               ans = min(ans, (i - px + 1) * wy);      if (ans == INF) ans = -1;      printf("%d\n", ans);      return 0;  }  


D. CGCDSSQ

Given a sequence of integers a1, ..., an andq queriesx1, ..., xq on it. For each queryxi you have to count the number of pairs(l, r) such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.

is a greatest common divisor ofv1, v2, ..., vn, that is equal to a largest positive integer that divides allvi.

Input

The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line containsn space separated integers a1, ..., an, (1 ≤ ai ≤ 109).

The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then followsq lines, each contain an integer xi, (1 ≤ xi ≤ 109).

Output

For each query print the result in a separate line.

Sample test(s)
Input
32 6 3512346
Output
12201
Input
710 20 3 15 1000 60 16101234561020601000
Output
14022202211

思路:刚开始想了半天能不能用线段树做,最后没能想出来。。。其实这个可以直接先把所有的可能的gcd对应的区间数求出来,那么应该怎么求?根据gcd具有递减性,每个数最多循环log(a[i])次,所以总的复杂度为nloga[i]。枚举右端点,然后pre保存前面与当前位置i的gcd不同的最近的数,比如1 1 1 2 2 2,pre[6]=3;

#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;typedef long long LL;const int maxn=100010;int N,a[maxn],pre[maxn],M;map<int,LL> ans;int gcd(int x,int y){    if(!y)return x;    return gcd(y,x%y);}void solve(){    for(int i=1;i<=N;i++)    {        int pos=i,x=a[i];        pre[i]=i-1;        for(int j=i;j>0;j=pre[j])        {            a[j]=gcd(a[j],x);            ans[a[j]]+=j-pre[j];            if(a[j]<x)            {                pre[pos]=j;                pos=j;                x=a[j];            }        }        pre[pos]=0;    }}int main(){    scanf("%d",&N);    for(int i=1;i<=N;i++)scanf("%d",&a[i]);    solve();    scanf("%d",&M);    while(M--)    {        int x;        scanf("%d",&x);        cout<<ans[x]<<endl;    }    return 0;}


0 0
原创粉丝点击