[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;
}