JNday6-am

T1 book
模拟。。。。。
10块的只能用5块的找, 20块的能用10块用十块。

T2
当时做的时候貌似读错题目了,当时好像很多人都
读错了,然后就GG了,这道题的本质是括号匹配
我们可以贪心的进行从后向前如果这个括号没有
指定成右括号而且他是左括号合法,则标记为左
括号 否则为右括号。跑完之后看看是否合法

T3
这道题的话,当时并没有足够的时间写,这道题的本质
有一棵树,每个节点有一个括号,问从起点到终点
是否存在一个合法的括号匹配
Floyed
F[i][j][k]表示从i到j能否以k的方式到达。
K = 括号完全匹配 或者 缺少一个右括号

T1 买书

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

int m5, m10, m20, n, money;

inline int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x;
}

inline void print()
{
    printf("NO\n");
    exit(0);
}

int main()
{
    freopen("book.in", "r", stdin);
    freopen("book.out", "w", stdout);
    n = read();
    for(int i = 1; i <= n; i ++)
    {
        money = read();
        if(money == 5) m5 ++;
        if(money == 10)
        {
            if(m5 >= 1) m5 --, m10 ++;
            else print();
        }
        if(money == 20)
        {
            if(m5 >= 1 && m10 >= 1) m5 --, m10--;
            else 
                if(m5 >= 3) m5 -= 3;
                else print();
        }
    }
    printf("YES\n");
    return 0;
}
/*
4
5 5 10 20
*/

T2写代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int maxn = 1000007;
int p = 0; 
int z[maxn],a[maxn],n,m;
int main()
{
  freopen("program.in","r",stdin);
  freopen("program.out","w",stdout);
  scanf("%d",&n);
  for (int i = 1; i<=n;i++)
    scanf("%d",&a[i]);
  scanf("%d",&m);
  int x;
  for (int i = 1; i<=m;i++)
  {
    scanf("%d",&x);
    if (a[x]>0) a[x] = -a[x];
  }
  for (int x = n; x>=1;x--)
  {
    if (a[x]>0) {
      if (z[p] == -a[x]) p--; 
        else { a[x] = -a[x]; p++; z[p]= a[x];}
    } else
    {
      p++;
      z[p]=a[x];
    }   
  }
  if (p==0) 
  {
    for (int i = 1; i<=n;i++)
    {
       if (a[i]>0) printf("+%d ",a[i]);
         else printf("%d ",a[i]);
        }
    }
    else printf("NO\n");
  return 0;
}

T3迷宫

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
using namespace std;
int n,m;
int g[105][105][40],q[105*105*40][3],t,x,y,z;
int main()
{
  freopen("maze.in","r",stdin);
  freopen("maze.out","w",stdout);
  scanf("%d%d",&n,&m);
  memset(g,0,sizeof(g));
  t = 0;
  for (int i = 1; i<=m;i++)
  {
    scanf("%d%d%d",&x,&y,&z);
    if (z!=0) { if (z<0) z = abs(z); else z+=10; g[x][y][z] = 1; g[y][x][z] = 1;}
      else { g[x][y][z] = 1; g[y][x][z] = 1; t++; q[t][0] = x; q[t][1] = y; q[t][2] = 0; t++; q[t][0] = y; q[t][1] = x; q[t][2] = 0;}
  }
  for (int i =1; i<=n;i++) {g[i][i][0] = 1; t++; q[t][0] = i; q[t][1] = i; q[t][2] = 0;}
  for (int s = 1; s<=t;s++)
  {
     int x = q[s][0]; 
     int y = q[s][1];
     int status = q[s][2];
     if (status!=0)
     {
       for (int i = 1; i<=n;i++)
         if (g[i][x][status-10] == 1)
         {
            if (g[i][y][0] ==0)
            {
                g[i][y][0] = 1;
                t++;
                q[t][0] = i; q[t][1] = y; q[t][2] = 0;
                        }
         }
         }
         else
         {
           for (int i =1; i<=n;i++)
           {
             if (g[i][x][0] ==1 && g[i][y][0] == 0)
             {
                g[i][y][0] =1;
                t++;
                q[t][0]= i;
                q[t][1] = y;
                q[t][2]= 0;
             }
             if (g[y][i][0] == 1 && g[x][i][0] ==0)
             {
               g[x][i][0] = 1;
               t++;
               q[t][0] = x;
               q[t][1] = i;
               q[t][2]= 0;
             }
             for (int j = 1; j<=10; j ++)
             {
               if (g[y][i][j] == 1 && g[x][i][j+20] == 0)
               {
                 g[x][i][j+20] = 1;
                 t++;
                 q[t][0] = x;
                 q[t][1] = i;
                 q[t][2] = j+20;
                     }
                 }
           }
         } 
  }
  int tt = 0; 
  scanf("%d",&tt);
  for (;tt;tt--)
  {
    scanf("%d%d",&x,&y);
    if (g[x][y][0] ==1) printf("YES\n"); else printf("NO\n");
  } 
}

 

posted @ 2017-11-02 21:04  ioioioioioio  阅读(166)  评论(0编辑  收藏  举报