A:挑战密室

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define N 600
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
 
char s[N];
 
int Find(int x, int len)
{
    int i;
 
    for(i=x; i<=len; i++)
        if(s[i]=='=')
        return i+1;
}
int Find2(int x, int len)
{
    int i;
 
    for(i=x; i<=len; i++)
        if(s[i]=='+')
        return i-1;
    return len;
}
 
 
int Find1(int x)
{
    int i, len = strlen(s)-1;
 
    for(i=x; i<len; i++)
        if(s[i]==')')
        return i-1;
 
    return len;
}
 
int Slove(int start, int e)
{
    int k=0, sum=0, pre, i;
 
    for(i=start; i<=e; i++)
    {
        if(s[i]>='0' && s[i]<='9')
            k = k*10 + s[i]-'0';
        else break;
    }
 
    for( ;i<=e; i++)
    {
        if(s[i]=='N')
        {
            if(s[i+1]=='a')
            {
                sum += 23;
                pre = 23;
                i++;
            }
            else
            {
                sum += 14;
                pre = 14;
            }
        }
        else if(s[i]=='C')
        {
            if(s[i+1]=='l')
            {
                sum += 35;
                pre = 35;
                i++;
            }
            else if(s[i+1]=='a')
            {
                sum += 40;
                pre = 40;
                i++;
            }
            else
            {
                sum += 12;
                pre = 12;
            }
        }
        else if(s[i]=='O')
        {
            sum += 16;
            pre = 16;
        }
        else if(s[i]=='S')
        {
            sum += 32;
            pre = 32;
        }
        else if(s[i]=='H')
        {
            sum += 2;
            pre = 2;
        }
        else if(s[i]=='A')
        {
            sum += 27;
            pre = 27;
            i++;
        }
        else if(s[i]=='Z')
        {
            sum += 65;
            pre = 65;
            i++;
        }
        else if(s[i]=='(')
        {
            int Index = Find1(i);
            int w = Slove(i+1, Index);
            sum += w;
            pre =  w;
            i = Index+1;
        }
        else
        {
            int z=0;
            while(s[i]>='0' && s[i]<='9')
            {
                z = z*10 + s[i]-'0';
                i++;
            }
            i--;
            sum += (z-1)*pre;
        }
    }
 
    if(k==0) return sum;
    else     return k*sum;
}
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int Index1, Index2, sum, len;
 
 
        scanf("%s", s);
 
        len = strlen(s)-1;
 
        Index1 = Find(0, len);
        Index2 = Find2(Index1, len);
        sum = Slove(Index1, Index2);
 
        printf("%04d\n", sum);
    }
    return 0;
}
View Code

 

B:最大岛屿

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define N 550
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
 
int s[N][N];
int n, m, Max, dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
 
int DFS(int x, int y)
{
    int i, nx, ny, sum=0;
 
    s[x][y] = 0;
    for(i=0; i<8; i++)
    {
       nx = x + dir[i][0];
       ny = y + dir[i][1];
       if(nx>=0 && nx<n && ny>=0 && ny<m && s[nx][ny])
       {
          sum += DFS(nx, ny) + 1;
       }
    }
 
    return sum;
}
 
int main()
{
    int T;
 
    while(scanf("%d%d%d", &n, &m, &T)!=EOF)
    {
        int i, j, ans, sum=0, Max=0;
 
        met(s, 0);
        for(i=0; i<n; i++)
        for(j=0; j<m; j++)
            scanf("%1d", &s[i][j]);
 
        for(i=0; i<n; i++)
        for(j=0; j<m; j++)
        {
            if(s[i][j]==1)
            {
                ans = DFS(i, j) + 1;
                ///printf("%d\n", ans);
                if(ans) sum++;
                Max = max(Max, ans);
            }
        }
 
        printf("%d %d\n", sum, Max*T);
    }
    return 0;
}
View Code

 

C:最小换乘

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
using namespace std;
#define N 1305
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))

int vis[N], G[N][N], n, dist[N];

int Dij(int s)
{
    met(vis, 0);
    dist[s] = 0;
    ///vis[s] = 1;
    for(int i=1; i<=n; i++)
    {
        int Min = INF, Index = -1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && Min>dist[j])
            {
                Min = dist[j];
                Index = j;
            }
        }
        if(Index == -1)break;
        vis[Index] = 1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j] > dist[Index]+G[Index][j])
            {
                dist[j] = dist[Index] + G[Index][j];
            }
        }
    }
    return dist[n];
}

int main()
{
    int m, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &m, &n);

        for(int i=1; i<=n; i++)
        {
            dist[i] = INF;
            for(int j=1; j<=n; j++)
                G[i][j] = INF;
            G[i][i] = 0;
        }

        for(int i=1; i<=m; i++)
        {
            char ch; int k = 0, a[N];

            while(scanf("%d%c", &a[k], &ch),ch==' ')
                k++;

            for(int j=0; j<=k; j++)
            {
                for(int ii=j+1; ii<=k; ii++)
                    G[a[j]][a[ii]] = 1;
            }
        }
        int ans = Dij(1);
        if(ans == INF)
            puts("NO");
        else
            printf("%d\n", ans-1);
    }
    return 0;
}


/**

2
3 7
6 7
4 7 3 6
2 1 3 5
2 6
1 3 5
2 6 4 3

*/
View Code

 

D:引水工程

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
using namespace std;
#define N 305
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
 
int vis[N], G[N][N], n, dist[N];
 
int Prim(int s)
{
    for(int i=1; i<=n; i++)
        dist[i] = G[s][i];
    met(vis, 0);
    vis[s] = 1;
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
        int Min = INF, Index = -1;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<Min)
            {
                Min = dist[j];
                Index = j;
            }
        }
        if(Index == -1)break;
        vis[Index] = 1;
        ans += Min;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>G[Index][j])
            {
                dist[j] = G[Index][j];
            }
        }
    }
    return ans;
}
 
int main()
{
    int w, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
 
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
            {
                G[i][j] = G[j][i] = INF;
            }
        }
 
        for(int i=1; i<=n; i++)
        {
            scanf("%d", &w);
            G[0][i] = G[i][0] = w;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d", &w);
                G[i][j] = w;
            }
        }
        int ans = Prim(0);
        printf("%d\n", ans);
    }
    return 0;
}
View Code

 

 

 

F:Distribution

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<map>
using namespace std;
#define N 10100
#define met(a, b) memset(a, b, sizeof(a))
 
int main()
{
    int n, m, x[N], y[N], a, b;
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d %d", &x[i], &y[i]);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d", &a, &b);
            int cnt1 = 0, cnt2 = 0;
            for(int j=0; j<n; j++)
            {
                if((x[j]>a && y[j]>b) || (x[j]<a && y[j]<b))
                    cnt1++;
                else if((x[j]<a && y[j]>b) || (x[j]>a && y[j]<b))
                    cnt2++;
            }
            printf("%d\n", cnt1-cnt2);
        }
    }
    return 0;
}
View Code

 

G:Interference Signal

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long LL;
#define N 1100
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f

int a[N];

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n, k, i, j;
        double Max=0, ans;

        scanf("%d%d", &n, &k);

        met(a, 0);
        for(i=0; i<n; i++)
            scanf("%d", &a[i]);

        for(i=0; i<n; i++)
        {
            int sum = 0;
            for(j=i; j<n; j++)
            {
                sum += a[j];
                if((j-i+1)>=k)
                {
                     ans = (sum*1.0)/(j-i+1);
                     Max = max(Max, ans);
                }
            }
        }

        printf("%d\n", (int)(Max*1000));
    }
    return 0;
}
View Code

 

posted on 2016-05-24 16:12  栀蓝  阅读(668)  评论(0编辑  收藏  举报

levels of contents