Developing school contest 2

Problem H

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 100   Accepted Submission(s) : 63
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

Now that spring is here and the sun is shining bright, people are starting to lower their blinds. Stefica is an elderly woman who likes to keep track of what other people in the neighbourhood are doing and then talk about it behind their backs. This year, she is particularly interested in who is lowering blinds in the building across the street, and how low are they lowering them. We will represent each window with a 4 x 4 grid, with asteriskes representing lowered blinds. Stefica can see a window in one of the following 5 states: 

The building across the street has N windows at each of the M floors. Given the current building state, find out how many windows are in each of the 5 states shown above.

Input

The input consists of multiple test cases.The first line of input contains space separated integers M and N (1 ≤ M, N ≤ 100). The following lines describe the current building state. Each window is represented with one of the 4 x 4 grids shown above, and windows are separated using character '#'. See the example input for clarification. Building description will have exactly 5M + 1 lines each having 5N + 1 characters.

Output

Output should contain 5 space separated integers, number of windows for each type in order shown above. Sum of these numbers is M*N.

Sample Input

1 2
###########
#....#****#
#....#****#
#....#....#
#....#....#
###########
2 3
################
#****#****#****#
#****#****#****#
#****#....#****#
#....#....#****#
################
#....#****#****#
#....#****#....#
#....#....#....#
#....#....#....#
################

Sample Output

1 0 1 0 0
1 1 2 1 1

Statistic |  Submit |  Back
代码:
View Code
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
using namespace std;

#define MAXN 105

char mp[MAXN * 10][MAXN * 9];

int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };

int N,M,ans;
int a,b,c,d,e;
int visit[620][610];
int hash[100];
int vhash[1200];

int jugde( int x, int y )
{
     if( x < 0 || y < 0 || x >= 6 * N - N + 1 || y >= 5 * M + 1)
     return 0;
     else
     return 1;
}

struct node
{
  int x, y;
}info;


int bfs( int p)
{
   queue<node>q;
   q.push(info);
   int maxn = p,size = 0;
   memset(vhash,0,sizeof(vhash));
   visit[info.x][info.y] = ++size;
   vhash[size] = 1;
   while( !q.empty())
   {
      info = q.front();
      q.pop();
      int x = info.x;
      int y = info.y;
      for( int i = 0; i < 4; i++)
      {
         int xx = x + dx[i];
         int yy = y + dy[i];
         if( jugde(xx, yy) && mp[xx][yy] == '*' && !vhash[visit[xx][yy]] )
         {
             info.x = xx;
             info.y = yy;
             q.push( info );
             ++size;
             visit[xx][yy] = size;
             vhash[size] = 1;
             maxn++;
         }        
              
      }          
   }
   return maxn;      
}


int main()
{
    
    while( scanf( "%d%d",&N,&M ) == 2 )
    {
        ans = 0;
        memset(hash,0,sizeof(hash));
        for( int i = 0; i < 6*N - N + 1; i++ )
        {
           scanf( "%s",mp[i] );    
           //printf("\n\n%s\n",mp[i]);        
        }
        memset(visit,0,sizeof(visit));
        for( int i = 0; i < N; i++ )
        {
           int x = i * 5 + 1;
           int y = 0;
           for( int j = 0; j < M; j++)
           {
              y = 1 + 5 * j;
              info.x = x;
              info.y = y;
              int ans;
             // printf("abc:%c\n",mp[x][y]);
              if( mp[x][y] == '*' )
                 ans = bfs(1);
              else
                 ans = bfs(0);
           //   printf("x = %d\n", ans);
              hash[ans]++;
           }
  
        }
        printf("%d %d %d %d %d\n",hash[0],hash[4],hash[8],hash[12],hash[16]);
    }
    return 0;
}

Problem F

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 340   Accepted Submission(s) : 45
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

自从周正虎被告知山上并没有老虎后就成为了当地一名伐木工人。这一次他需要砍下M米的木材,当然,自从有了新的伐木工具之后,这对他来说简直就是一件轻而易举的事情。
周正虎的伐木工具的使用说明书上这样写道:机器需要设定一个高度参数H(米),然后机器就会升高到H(米),砍下所有高于H(米)的树。对于高度没有达到H(米)的树,我们认为它是幸运的,在这次砍伐中它将免受威胁。例如:现有树的高度为 20, 15, 10, 17 将机器设置为15,那么我们将砍伐到第一和第四棵树,得到的木材长度为(20-15) + (17-15) = 7。
周正虎是一个爱好环境的人,所以他想知道他最高能将机器的高度设置为多少去满足至少砍下M米的木材。

Input

输入有多组数据。第一行将输入两个数据,1 <= N <= 1 000 000,1<= M <= 2 000 000 000分别代表树的棵树以及要得到的木材的长度,
第二行将包含以空格隔开的N个数,分别表示第1到第N棵树的高度。每棵树的高度小于 1 000 000 000,树的高度总和将超过M,周正虎将始终得到他想要的木材长度。

Output

输出有且仅有一行,表示要求输出的高度。

Sample Input

4 7
20 15 10 17
5 20
4 42 40 26 46

Sample Output

15
36

Statistic |  Submit |  Back
View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>

using namespace std;
__int64 N,M;
__int64 dt[1000010], Total;
__int64 d[1000100];

__int64 lowbit( __int64 x )
{
 return x&-x;
}

void add( __int64 n, __int64 val)
{
    while( n <= N )
    {
               
       dt[n] += val;
       n += lowbit(n);
    }      
}


__int64 sum( __int64 n)
{
   __int64 ans = 0;
   while( n >= 1 )
   {
     ans += dt[n];
     n -= lowbit(n);
   }    
   return ans;
}

__int64 jugde( __int64 x )
{
   __int64 id = lower_bound(d + 1, d + N + 1, x) - d;
   Total = sum(N) - sum(id-1);
   Total -= (N - id + 1) * x;
   return Total;
}


__int64 solve( __int64 l, __int64 r)
{  
   __int64 v = 0;
   while( l <= r )
   {
         __int64 mid = (l + r) / 2;
         __int64 ans = jugde(mid);
      if( ans >= M )
      {  
         v = mid;
         l = mid + 1;            
      }     
      else
         r = mid - 1;
             
   }
   return v;     
}

int main( )
{
   while( scanf("%I64d%I64d",&N,&M) != EOF)
   {
     __int64 maxn = 0;
     memset(dt, 0, sizeof(dt));
     memset(d, 0, sizeof(d));
     for( __int64 i = 1; i <= N; i++)
     {
        scanf("%I64d",&d[i]);
     }
     sort(d + 1, d + N + 1);
     for( __int64 i = 1;i <= N; i++)
     { 
        add(i,d[i]);
        if( d[i] > maxn )
           maxn = d[i];
     }          
     __int64 ans = solve(0,maxn);
     printf("%I64d\n",ans);
    }
   return 0;
     
}

Problem A

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 268   Accepted Submission(s) : 104
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

In “Blackjack”, a popular card game, the goal is to have cards which sum up to largest number not exceeding 21. Mirko came up with his own version of this game. In Mirko's game, cards have positive integers written on them. The player is given a set of cards and an integer M. He must choose three cards from this set so that their sum comes as close as possible to M without exceeding it. This is not always easy since there can be a hundred of cards in the given set. Help Mirko by writing a program that finds the best possible outcome of given game.

Input

The input consists of multiple test cases.The first line of input contains an integer N (3 ≤ N ≤ 100), the number of cards, and M (10 ≤ M ≤ 300 000), the number that we must not exceed. 
The following line contains numbers written on Mirko‘s cards: N distinct space-separated positive integers less than 100 000. There will always exist some three cards whose sum is not greater than M.

Output

The first and only line of output should contain the largest possible sum we can obtain.

Sample Input

5 21
5 6 7 8 9
10 500
93 181 245 214 315 36 185 138 216 295

Sample Output

21
497
View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>

using namespace std;
int N,M;
int a[110]; 
int hash[300010];   
int main()
{
     while( scanf("%d%d",&N, &M) != EOF)
     {
        for( int i = 1; i <= N; i++)
              scanf("%d",&a[i]);
        memset( hash, 0, sizeof(hash));
        for( int i = 1; i <= N; i++)
           for( int j = i + 1; j <= N;j++)
              for( int k = j + 1; k <= N; k++)
              {
                  int t = a[i] + a[j] + a[k];
                  if( t <= M )
                  hash[t] = 1;
               }
        for( int i = M; i >= 0; i--)
           if( hash[i] )
           {
              printf("%d\n",i);
              break;       
           }
              
     }
     return 0;     
}

 

转载于:https://www.cnblogs.com/tangcong/archive/2012/07/22/2604022.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值