题目A:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5319
Alice and Bob is playing a game, and this time the game is all about the absolute value!
Alice has N different positive integers, and each number is not greater than N. Bob has a lot of blank paper, and he is responsible for the calculation things. The rule of game is pretty simple. First, Alice chooses a number a1 from the N integers, and Bob will write it down on the first paper, that's b1. Then in the following kth rounds, Alice will choose a number ak (2 ≤ k ≤ N), then Bob will write the number bk=|ak-bk-1| on the kth paper. |x| means the absolute value of x.
Now Alice and Bob want to kown, what is the maximum and minimum value of bN. And you should tell them how to achieve that!
The input consists of multiple test cases;
For each test case, the first line consists one integer N, the number of integers Alice have. (1 ≤ N ≤ 50000)
For each test case, firstly print one line containing two numbers, the first one is the minimum value, and the second is the maximum value.
Then print one line containing N numbers, the order of integers that Alice should choose to achieve the minimum value. Then print one line containing N numbers, the order of integers that Alice should choose to achieve the maximum value.
Attention: Alice won't choose a integer more than twice.
2
1 1 1 2 2 1
分析:
4个数为4组,最小值单独考虑最前面的几个,最大值单独考虑最后面的几个。具体见代码。
代码:
#include
int minNum[50001];
int maxNum[50001];
int main()
{
int N;
int max, min;
int num;
while(std::cin >> N)
{
if(N % 4 == 0)
{
min = 0;
num = N;
for(int i = N / 4; i > 0; --i)
{
minNum[i * 4 - 1] = num - 1;
minNum[i * 4 - 2] = num;
minNum[i * 4 - 3] = num - 2;
minNum[i * 4 - 4] = num - 3;
num -= 4;
}
max = N;
num = 1;
for(int i = 0; i < N / 4; ++i)
{
maxNum[i * 4] = num;
maxNum[i * 4 + 1] = num + 2;
maxNum[i * 4 + 2] = num + 1;
maxNum[i * 4 + 3] = num + 3;
num += 4;
}
}
else if(N % 4 == 1)
{
min = 1;
num = N;
for(int i = N / 4; i > 0; --i)
{
minNum[i * 4] = num - 1;
minNum[i * 4 - 1] = num;
minNum[i * 4 - 2] = num - 2;
minNum[i * 4 - 3] = num - 3;
num -= 4;
}
minNum[0] = 1;
max = N;
num = 1;
for(int i = 0; i < N / 4; ++i)
{
maxNum[i * 4] = num;
maxNum[i * 4 + 1] = num + 2;
maxNum[i * 4 + 2] = num + 3;
maxNum[i * 4 + 3] = num + 1;
num += 4;
}
maxNum[N - 1] = N;
}
else if(N % 4 == 2)
{
min = 1;
num = N;
for(int i = N / 4; i > 0; --i)
{
minNum[i * 4 + 1] = num - 1;
minNum[i * 4] = num;
minNum[i * 4 - 1] = num - 2;
minNum[i * 4 - 2] = num - 3;
num -= 4;
}
minNum[1] = 2;
minNum[0] = 1;
max = N - 1;
num = 1;
for(int i = 0; i < N / 4; ++i)
{
maxNum[i * 4] = num;
maxNum[i * 4 + 1] = num + 2;
maxNum[i * 4 + 2] = num + 1;
maxNum[i * 4 + 3] = num + 3;
num += 4;
}
maxNum[N - 2] = N - 1;
maxNum[N - 1] = N;
}
else
{
min = 0;
num = N;
for(int i = N / 4; i > 0; --i)
{
minNum[i * 4 + 2] = num - 1;
minNum[i * 4 + 1] = num;
minNum[i * 4] = num - 2;
minNum[i * 4 - 1] = num - 3;
num -= 4;
}
minNum[2] = 2;
minNum[1] = 3;
minNum[0] = 1;
max = N - 1;
num = 1;
for(int i = 0; i < N / 4; ++i)
{
maxNum[i * 4] = num;
maxNum[i * 4 + 1] = num + 2;
maxNum[i * 4 + 2] = num + 3;
maxNum[i * 4 + 3] = num + 1;
num += 4;
}
maxNum[N - 3] = N - 1;
maxNum[N - 2] = N - 2;
maxNum[N - 1] = N;
}
std::cout << min << " " << max << std::endl;
for(int i = 0; i < N; ++i)
{
std::cout << minNum[i] << " ";
}
std::cout << std::endl;
for(int i = 0; i < N; ++i)
{
std::cout << maxNum[i] << " ";
}
std::cout << std::endl;
}
return 0;
}
Despite YY's so much homework, she would like to take some time to play with her minions first.
YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. We use 0(the digit) to represent that it is asleep, and 1 for awake. Also, we define the minions who are around a minion closest in one of the eight directions its neighbors. And every minute every minion will change its status by the following specific rules:
If this minion is awake, and the number of its neighbors who are awake is less than 2, this minion will feel lonely and turn to asleep.If this minion is awake, and the number of its neighbors who are awake is more than 3, this minion will turn to asleep for it will feel too crowded.If this minion is awake, and the number of its neighbors who are awake is exactly 2 or 3, this minion will keep being awake and feel very happy.If this minion is asleep, and the number of its neighbors who are awake is exactly 3, this minion will wake up because of the noise. Note that all changes take place at the same time at the beginning of a specific minute.Also, some minions will get bored and leave this silly game. We use 'X's to describe them. We suppose that a minion would leave after T minutes. It will leave at the end of the Tth minute. Its status is considered during the change at the beginning of the Tth minute, and should be ignored after that. Of course, one minion will not leave twice!
YY is a girl full of curiosity and wants to know every minion's status after F minutes. But you know she is weak and lazy! Please help this cute girl to solve this problem :)
There are multiple test cases.
The first line contains the number of test cases Q. 1<=Q<=100. For each case, there are several lines: The first line contains four integers N, M, F, K. K means the number of leaving messages. 1<=N, M<=50, 1<=F<=1000, 1<=K<=N*M. Next N lines are the matrix which shows the initial status of each minion. Each line contains M chars. We guarantee that 'X' wouldn't appear in initial status matrix. And next K lines are the leaving messages. Each line contains three integers Ti, Xi, Yi, They mean the minion who is located in (Xi, Yi) will leave the game at the end of the Tith minutes. 1<=Ti<= F, 1<=Xi<=N, 1<=Yi<=M.
For each case, output N lines as a matrix which shows the status of each minion after F minutes.
2 3 3 2 1 101 110 001 1 2 2 5 5 6 3 10111 01000 00000 01100 10000 2 3 3 2 4 1 5 1 5
010 1X0 010 0000X 11000 00X00 X0000 00000
For case 1: T=0, the game starts 101 110 001 --------------- at the beginning of T=1, a change took place 100 101 010 --------------- at the end of T=1 (the minion in (2,2) left) 100 1X1 010 --------------- at the beginning of T=2, a change took place 010 1X0 010 --------------- at the end of T=2 (nothing changed for no minion left at T=2) 010 1X0 010
统计每个点的邻居信息,然后按规则统一更新。离开信息的时间放在优先级队列中,同时时间和坐标信息放在map里,如果队列顶端元素和当前正在处理的时间相同,则pop该时间,并把相应坐标改成'X'。详见代码。
代码:
#include
#include
#include
#include
#include // std::greater
#include // std::pair
char matrix[52][52];
char newState[52][52];
int N, M, F, K;
int xx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int yy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int GetAwakeNeighbors(int x, int y)
{
int count = 0;
int newX, newY;
for(int k = 0; k < 8; ++k)
{
newX = x + xx[k];
newY = y + yy[k];
if(newX >= 0 && newX < N && newY >= 0 && newY < M)
{
if(matrix[newX][newY] == '1')
++count;
}
}
return count;
}
int main()
{
int Q;
std::cin >> Q;
int t, x, y;
std::priority_queue, std::greater > pq;
std::unordered_map > > m;
for(int caseNo = 0; caseNo < Q; ++caseNo)
{
std::cin >> N >> M >> F >> K;
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
{
std::cin >> matrix[i][j];
}
for(int i = 0; i < K; ++i)
{
std::cin >> t >> x >> y;
pq.push(t);
if(m[t].empty())
{
std::vector > tmp;
tmp.push_back(std::make_pair(x, y));
m[t] = tmp;
}
else
{
m[t].push_back(std::make_pair(x, y));
}
}
for(int f = 1; f <= F; ++f)
{
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < M; ++j)
{
newState[i][j] = matrix[i][j];
int count = GetAwakeNeighbors(i, j);
if(matrix[i][j] == '1' && (count < 2 || count > 3))
{
newState[i][j] = '0';
}
if(matrix[i][j] == '0' && (count == 3))
{
newState[i][j] = '1';
}
}
}
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < M; ++j)
{
matrix[i][j] = newState[i][j];
}
}
if(!pq.empty() && f == pq.top())
{
pq.pop();
for(int k = 0; k < m[f].size(); ++k)
{
int mX = m[f][k].first - 1;
int mY = m[f][k].second - 1;
matrix[mX][mY] = 'X';
}
m.erase(f);
while (!pq.empty() && f == pq.top()) // remove same minutes
{
pq.pop();
}
}
}
for(int i = 0; i < N; ++i)
{
for(int j = 0; j < M; ++j)
{
std::cout << matrix[i][j];
}
std::cout << std::endl;
}
}
return 0;
}