题目来源:CodeForce #15 A

现在有 n 间正方形的房子,其中心点分布在 X轴 上,现在我需要新建一间边长为 t 的房子,要求新房子至少和一间房子相邻,但是不能和其他房子重合。请输出我有多少个位置可以选。

先分析一下:

  因为现在要建一间边长为 t 的房子,而且要有一间房子与之相邻。所以,只有两种可能:第一种,在两端头。第二种,两间房子之间的间隔>= t。

分析完之后,做法已经是显而易见的了。首先,最少能建2间房子(在两端头)。然后就是遍历所有中心点,来计算房子间的距离,来判断能不能建房子。特别的,如果距离=t,只有一种方法。如果>t那么有两种。

时间效率:O( n * logn),主要用在排序上。

附AC代码:

#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
namespace std;
  10:  
int t, n, pri[29], res[29], tmp[29], sum = 0;
  12:     
int id)
  14: {
if (id > n)
  16:     {
int m = 0;
int i = 1; i <= n; i++)
if (tmp[i])
  20:                 m += pri[i];
if (m > sum && m <= t)
  22:         {
  23:             sum = m;
int i = 1; i <= n; i++)
  25:                 res[i] = tmp[i];
  26:         }
return;
  28:     }
else
  30:     {
  31:         tmp[id] = 1;
  32:         dp(id+1);
  33:         tmp[id] = 0;
  34:         dp(id+1);
return;
  36:     }
  37: }
  38:  
int main()
  40: {
, &t, &n))
  42:     {
sizeof(res));
sizeof(tmp));
sizeof(pri));
  46:         sum = 0;
int i = 1; i <= n; i++)
, &pri[i]);
  49:         dfs(1);
int i = 1; i <= n; i++)
if (res[i])
, pri[i]);
, sum);
  54:     }
  55: }

相关文章:

  • 2022-02-18
  • 2021-07-17
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
相关资源
相似解决方案