Works Application 2017 笔试题

本文介绍了一个问题场景:如何在一个拥有N个房间的大房子里放置M个无线路由器以达到最高的满意度。每个房间的满意度不同,且路由器只能覆盖相邻的房间。文章提供了一段C++代码示例,展示了如何通过算法找到最佳的路由器放置方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Exam1] Magic Cube

Description

Magic cube is a cube (M*M*M) which contains a integer number (0 <= number < P) in each cell (So you know there are M ^ 3 numbers).

Alice have one big cube and N smaller cubes (each small cube is unique).

The cube is magic since when you put a small cube in bigger one, the number in the same location will add up and mod P.

Your job is to determine the location of each small cube, so that we can make every number in big cube equal 0 after putting all the small cubes.

Notice

We guarantee that there is only one solution.
You cannot rotate cube.
We give the numbers in cubes in order of position [0,0,0], [0,0,1], …, [0,1,0], …,[1,0,0], …

Limits

Memory limit per test: 256 megabytes
Tiem limit per test: The faster the better

Compile & Environment

C++
g++ Main.cc -o Main -fno-asm -Wall -lm –static -std=C++0x -DONLINE_JUDGE

Java J2SE 8
Maximum stack size is 50m

Input

The first line is three integer M (2 <= M <= 7), N (1<= N <= 12), P ( 3 <= P <=5 )
Second line is M*M*M integers, showing the numbers in big cube.

Then is N lines, in which first is a integer L[i] ( 1 <= L[i] < M) shows the length of the small cube, then follows L[i] ^ 3 numbers (0 <= number < P).

Output

Output N lines of triad of integers, shows the location of this small cube.

Sample Test

Input

2 1 3
1 0 0 0 0 0 0 0
1 2

Output
0 0 0



第二题

[Exam2] Wireless Routers

Description

Alice just bought a very big house with N rooms and N-1 doors, which each door connects two rooms. Besides, each room have at least one door and at most 3 doors (of course Alice can Go to every room in this house).

However, a modern person cannot live without Wifi, so Alice wants to put M wireless routers in several rooms. As the routers are cheap, only adjacent rooms (which have a door connect to this room) can receive it, and each router works independently.

Since M routers may cannot cover every room, Alice tells you the satisfaction point S[i] she could have if room i have Wifi.

Please help Alice to calculate the maximum satisfaction point she can get in total.

Input

The first line is two integers N (2 <= N <= 1000), M (1 <= M <= N, M <= 100) Then are N lines, each shows the satisfaction S[i] (1 <= S[i] <= 10) point of room i. Then are N-1 lines, each contains two integers x,y, which represents a door is between room x and y.

output

Just output the maximum point of satisfaction.

Limits

Memory limit per test: 256 megabytes
Time limit per test: The faster the better

Compile & Environment

C++
g++ Main.cc -o Main -fno-asm -Wall -lm –static -std=C++0x -DONLINE_JUDGE
Java
J2SE 8
Maximum stack size is 50m

Sample Test

Input

5 1
1 2 3 4 5
2 1
3 2
4 2
5 3

Output

10


///////////////////////////////////////////////////

#include<iostream>
#include<string>


class WRouter
{
private:
 int N, M;
 int(*door)[2];
 double *s;
   
 int(*doorNext)[3];
 //int maxS;
 ///int maxRoom;
public:
 WRouter(int N_, int M_, int(*door_)[2], double *s_)
 {
  N = N_;
  M = M_;
  door = door_;
  s = s_;
  

  doorNext = new int[N][3];
  for (int i = 0; i < N - 1; i++)
  for (int j = 0; j < 3; j++)
   doorNext[i][j] = 0;


  for (int i = 0; i < N; i++)
  {
   int k = 0;
   for (int j = 0; j < N - 1; j++)
   {
    if (door[j][0] == i + 1)
    {
     doorNext[i][k] = door[j][1];
     k++;
    }
    else if (door[j][1] == i + 1)
    {
     doorNext[i][k] = door[j][0];
     k++;
    }
    if (k == 3)
    {
     break;
    }
   }

  }
 }

 double findMaxRoom()
 {

  double maxS1 = 0;
  int maxRoom = 0;

  for (int i = 0; i < N; i++)
  {

   double sum = 0;
   sum = s[i - 1];

   for (int t = 0; t < 3; t++)
   {
    if (doorNext[i][t] != 0)
    {
     sum += s[doorNext[i][t] - 1];
    }
   }
   if (sum>maxS1)
   {
    maxS1 = sum;
    maxRoom = i + 1;
   }

  }


  for (int t = 0; t < 3; t++)
  {
   if (doorNext[maxRoom-1][t] != 0)
   {
    s[doorNext[maxRoom - 1][t] - 1]=0;
   }
  }

  return maxS1;
 }
};


int main()
{
 int N, M;
 while (std::cin >> N >> M)
 {
  double *s = new double(N);
  for (int i = 0; i < N; i++)
  {
   std::cin >> s[i];
  }

  int(*door)[2] = new int[N - 1][2];
  for (int i = 0; i < N - 1; i++)
  {
   std::cin >> door[i][0] >> door[i][1];
  }

  for (int i = 0; i < N - 1; i++)
  {
   std::cout << door[i][0] << "  " << door[i][1] << std::endl;
  }
 

 double maxS = 0;
 WRouter WR(N, M, door, s);
 for (int i = 0; i < M; i++)
 {
  maxS += WR.findMaxRoom();
 }
 std::cout << "\n"<< maxS << std::endl;

 delete s;
 delete[] door;
}
 return 0;

}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值