SPOJ - AMR11
A
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did we not tell you that it was just an online game ? uhhh! now here is the real onsite task for Harry. You are given a magrid S ( a magic grid ) having R rows and C columns. Each cell in this magrid has either a Hungarian horntail dragon that our intrepid hero has to defeat, or a flask of magic potion that his teacher Snape has left for him. A dragon at a cell (i,j) takes away |S[i][j]| strength points from him, and a potion at a cell (i,j) increases Harry's strength by S[i][j]. If his strength drops to 0 or less at any point during his journey, Harry dies, and no magical stone can revive him.
Harry starts from the top-left corner cell (1,1) and the Sorcerer's Stone is in the bottom-right corner cell (R,C). From a cell (i,j), Harry can only move either one cell down or right i.e., to cell (i+1,j) or cell (i,j+1) and he can not move outside the magrid. Harry has used magic before starting his journey to determine which cell contains what, but lacks the basic simple mathematical skill to determine what minimum strength he needs to start with to collect the Sorcerer's Stone. Please help him once again.
Input (STDIN):
The first line contains the number of test cases T. T cases follow. Each test case consists of R C in the first line followed by the description of the grid in R lines, each containing C integers. Rows are numbered 1 to R from top to bottom and columns are numbered 1 to C from left to right. Cells with S[i][j] < 0 contain dragons, others contain magic potions.
Output (STDOUT):
Output T lines, one for each case containing the minimum strength Harry should start with from the cell (1,1) to have a positive strength through out his journey to the cell (R,C).
Constraints:
1 ≤ T ≤ 5
2 ≤ R, C ≤ 500
-10^3 ≤ S[i][j] ≤ 10^3
S[1][1] = S[R][C] = 0
Sample Input:
3
2 3
0 1 -3
1 -2 0
2 2
0 1
2 0
3 4
0 -2 -3 1
-1 4 0 -2
1 -2 -3 0
Sample Output:
2
1
2
Explanation:
Case 1 : If Harry starts with strength = 1 at cell (1,1), he cannot maintain a positive strength in any possible path. He needs at least strength = 2 initially.
Case 2 : Note that to start from (1,1) he needs at least strength = 1.
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9+; #define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) //dp,从 i,j点到终点的最小需求分数
int a[][], dp[][];
int main()
{
int T;
cin >> T;
while(T--)
{
Mem0(a);
Mem0(dp);
int n, m;
cin >> n >> m;
int i, j;
for(i = ;i < n;++i)
for(j = ;j < m;++j)
cin >> a[i][j]; dp[n - ][m - ] = ;
for(i = n - ;i >= ;--i)
dp[i][m - ] = max(dp[i + ][m - ] - a[i][m - ], );
for(i = m - ;i >= ;--i)
dp[n - ][i] = max(dp[n - ][i + ] - a[n - ][i], );
for(i = n - ;i >= ;--i)
for(j = m - ;j >= ;--j)
dp[i][j] = max(min(dp[i + ][j], dp[i][j + ]) - a[i][j], );
cout << dp[][] << endl;
}
return ;
}
---------------------------------------------------------------------------------------------------------------------------
B题是个坑!!!!
先把图画出来再遍历有多少个点就是过不了!!!
下面题目加AC码
Hogwarts is under attack by the Dark Lord, He-Who-Must-Not-Be-Named. To protect the students, Harry Potter must cast protective spells so that those who are protected by the spells cannot be attacked by the Dark Lord.
Harry has asked all the students to gather on the vast quidditch sports field so that he can cast his spells. The students are standing in a 2D plane at all grid points - these are the points (x,y) such that both x and y are integers (positive, negative or 0). Harry's spell can take the shapes of triangle, circle or square, and all who fall within that shape (including its boundaries) are protected.
Given the types of spells and the details regarding where Harry casts the spell, output the number of people saved by Harry's spells.
Input (STDIN):
The first line contains the number of test cases T. T test cases follow.
Each case contains an integer N on the first line, denoting the number of spells Harry casts. N lines follow, each containing the description of a spell.
If the ith spell is a triangle, then the line will be of the form "T x1 y1 x2 y2 x3 y3". Here, (x1,y1), (x2,y2) and (x3,y3) are the coordinates of the vertices of the triangle.
If the ith spell is a circle, then the line will be of the form "C x y r". Here, (x,y) is the center and r is the radius of the circle.
If the ith spell is a square, then the line will be of the form "S x y l". Here, (x,y) denotes the coordinates of the bottom-left corner of the square (the corner having the lowest x and y values) and l is the length of each side.
Output (STDOUT):
Output T lines, one for each test case, denoting the number of people Harry can save.
Constraints:
All numbers in the input are integers between 1 and 50, inclusive.
The areas of all geometric figures will be > 0.
Sample Input:
4
1
C 5 5 2
1
S 3 3 4
1
T 1 1 1 3 3 1
3
C 10 10 3
S 9 8 4
T 7 9 10 8 8 10
Sample Output:
13
25
6
34
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <queue>
#include <map> using namespace std; typedef long long LL;
const int MAXN = ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f; #define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) int ans;
bool vis[][];
int dis_squ(int x1, int y1, int x2, int y2)
{
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
} void C_judge(int x, int y, int r)
{
int i, j;
for(i = x - r;i <= x + r;++i)
for(j = y - r;j <= y + r;++j)
if(!vis[i][j] && dis_squ(i, j, x, y) <= r * r)
{
ans++;
vis[i][j] = ;
}
} void S_judge(int x, int y, int l)
{
for(int i = x;i <= x + l;++i)
for(int j = y;j <= y + l;++j)
{
if(!vis[i][j])
{
ans++;
vis[i][j] = ;
}
}
} //* 叉乘
struct Point
{
int x, y;
}; //BC * BA
int cal(Point a, Point b, Point c)
{
return (c.x - b.x) * (a.y - b.y) - (a.x - b.x) * (c.y - b.y);
} void T_judge(int x1, int y1, int x2, int y2, int x3, int y3)
{
Point a, b, c;
a.x = x1, a.y = y1;
b.x = x2, b.y = y2;
c.x = x3, c.y = y3;
int xmi = min(x1, min(x2, x3)), xmx = max(x1, max(x2, x3));
int ymi = min(y1, min(y2, y3)), ymx = max(y1, max(y2, y3));
for(int i = xmi;i <= xmx;++i)
for(int j = ymi;j <= ymx;++j)
{
Point t;
t.x = i, t.y = j;
if(!vis[i][j] && cal(t, a, b) * cal(t, b, c) >= && cal(t, b, c) * cal(t, c, a) >= )
{
ans++;
vis[i][j] = ;
}
}
} int main()
{
int T;
cin >> T;
while(T--)
{
Mem0(vis);
int n, x1, y1;
ans = ;
string s;
cin >> n;
while(n--)
{
cin >> s >> x1 >> y1;
if(s[] == 'C')
{
int r;
cin >> r;
C_judge(x1 + , y1 + , r);
}
else if(s[] == 'S')
{
int l;
cin >> l;
S_judge(x1 + , y1 + , l);
}
else
{
int x2, y2, x3, y3;
cin >> x2 >> y2 >> x3 >> y3;
T_judge(x1 + , y1 + , x2 + , y2 + , x3 + , y3 + );
}
}
// int ans = 0;
// for(int i = 0;i < 200;++i)
// for(int j = 0;j < 200;++j)
// if(vis[i][j])
// ans++;
// for(int i = 100;i < 120;++i)
// {
// for(int j = 100;j < 120;++j)
// cout << vis[i][j] << " ";
// cout << endl;
// }
cout << ans << endl;
}
return ;
}
--------------------------------------------------------------------------------------------
D
You are the organizer of a Wizarding Duel tournament at Hogwarts. N players participated in the tournament and each player played with every other player exactly once, and each such game resulted in a winner and a loser (no drawn games). It is now time to declare the results. Each player's score is the number of games the player won. However, you realize that something possibly went wrong with the scoring system, and the scores that you have noted down might be wrong. In fact, the scores you have noted down could simply not be possible. For example, suppose there are 3 players and the scores that you noted are 0,0 and 2. Clearly this is not possible as the game between the first two players must have had a winner and thus both the players cannot have score 0.
While you have no way to figure out the correct scores of each player, you've decided to set the scores right by adjusting the scores of some players. The new set of scores should be possible to have arisen in the tournament, otherwise there would be ample proof that the scoring is wrong. However, making drastic changes might cause suspicion. So you've decided to adjust the scores so that the sum of the absolute differences between the old and the new scores of each player is minimized. In other words, if the original scores which you noted are a1,..,aN, you must change them to the series of possible scores b1,...bN such that the sum |ai - bi| is minimized.
Input (STDIN):
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N on the first line, followed by the set of scores which you have noted down: a1..aN.
Output (STDOUT):
Output T lines, one for each test case, containing the minimum sum of absolute values in order to make the scorecard a valid one.
Constraints:
1 <= T <= 20
2 <= N <= 50
0 <= ai <= 100
Sample Input:
2
3
0 0 2
5
5 3 2 1 4
Sample Output:
1
5
这题要注意一个点:前 i 项的和一定要 >= i * (i - 1) / 2,因为总的赢的场数不变,就是说后面的人能赢的最多场数是固定的
举个例子,0 0 2 4 4,这里第五个人赢了 4 场,那第四个人不可能赢 4 场
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9 + ; #define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) int main()
{
int T;
cin >> T;
int p[];
while(T--)
{
Mem0(p);
int n, i, j;
cin >> n;
int sum = , d = , ans = ;
for(i = ;i < n;++i)
cin >> p[i];
sort(p, p + n);
//这里保证了每个人赢的场次在合理的范围内
for(i = ;i < n;++i)
{
d += i, sum += p[i];
if(sum < d)
{
ans += d - sum;
sum = d;
}
}
//不过如样例 2 ,去掉这一行会输出 0。这里保证了总场次合理
ans += sum - d;
cout << ans << endl;
}
return ;
}
----------------------------------------------------------------------------------------------
H
Enough with this Harry Potter, please! What are we, twelve-year olds? Let's get our teeth into some real pumpkin pasties -- oops, programming problems!
Here we go!
Let's define the diversity of a list of numbers to be the difference between the largest and smallest number in the list.
For example, the diversity of the list (1, -1, 2, 7) = 7 - (-1) = 8.
A substring of a list is considered a non-empty sequence of contiguous numbers from the list. For example, for the list (1,3,7), the substrings are (1), (3), (7), (1,3), (3,7), (1,3,7). A subsequence of a list is defined to be a non-empty sequence of numbers obtained by deleting some elements from the list. For example, for the list (1,3,7), the subsequences are (1), (3), (7), (1,3), (3,7), (1,7), (1,3,7).
Given a list of length N find the number of substrings and subsequences in this list with the maximum diversity. If a substring/subsequence having maximum diversity occurs multiple times in the list, each of its occurences adds towards the answer. And tell Harry Potter your answer
Input (STDIN):
The first line contains T, the number of test cases. Then follow T test case blocks.
Each blocks starts with the first line containing the number N.
The second line contains a list of numbers in this list.
Output (STDOUT):
For each test case, output the number of substrings and the number of subsequences in this list with the maximum diversity.
Since the answers maybe very large, output them modulo 1000000007.
Constraints:
T <= 10
N <= 100,000
Each number in the list is between 1 and 100,000 inclusive.
Sample Input:
3
3
1 2 3
4
1 4 3 4
3
3 2 1
Sample Output:
1 2
3 6
1 2
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9 + ; #define MemN(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) //题目要算的是连续子串和子串
int main()
{
LL a[MAXN];
a[] = ;
for(int i = ;i < MAXN;++i)
a[i] = (a[i - ] * ) % MOD; int T;
cin >> T;
while(T--)
{
int p[MAXN];
int i, n, mi = INF, mx = ;
cin >> n;
for(i = ;i < n;++i)
{
cin >> p[i];
mi = min(mi, p[i]);
mx = max(mx, p[i]);
}
if(mi == mx)
cout << ((n + ) * n / ) << " " << a[n] - << endl;
else
{
int num_mi = , num_mx = ;
int x = -, y = -;
LL ans1 = , ans2 = ;
for(i = ;i < n;++i)
{
if(p[i] == mi)
{
x = i;
num_mi++;
}
if(p[i] == mx)
{
y = i;
num_mx++;
}
//当最大值和最小值同时存在时计算
ans1 = (ans1 + min(x + , y + )) % MOD;
}
//容斥定理
//ans2 = 总子集 - 不包含最小值的子集 - 不包含最大值的子集 + 既没有最小值也没有最大值子集
//减去两个 a[i], 最好加上 2 * MOD
ans2 = (a[n] - a[n - num_mi] - a[n - num_mx] + a[n - num_mi - num_mx] + * MOD) % MOD;
cout << ans1 << " " << ans2 << endl;
}
}
return ;
}
-----------------------------------------------------------------------------------------------
J
这题主要难点是判断一个点是之前就变化的还是现在才变化的
这里用 int vis[i][j] 标记计算变化的步数
The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson to be no less boring than you found your own history classes. Recently Binns has been droning on about Goblin wars, and which goblin civilization fought which group of centaurs where etc etc. The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams. Can you help them?
The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a '#' in the grid. All the other cells - to which the civilizations can move - are represented by a '.' in the grid.
A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid. Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a '*'. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.
Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.
Input (STDIN):
The first line contains T, the number of cases. This is followed by T test case blocks.
Each test case contains two integers, R, C.
This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.
Each cell is either a
1. '.' which represents an unoccupied cell
2. '#' which represents a cell that cannot be occupied
3. A civilization represented by a lowercase letter ('a' - 'z')
Output (STDOUT):
For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use '*' to denote that a battle is being waged in that particular cell.
Print a blank line at the end of each case.
Constraints:
1 <= R, C <= 500
1 <= T <= 5
Sample Input:
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
Sample Output:
#####
aa*bb
#####
####
aabb
####
#c#
a*b
#d#
#c#
acb
a*b
aa*bb
a#.#b
aa*bb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <map>
#include <set> using namespace std; typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MOD = 1e9 + ; #define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x)) int vis[][], dis[][] = {, , , , , -, -, };
char p[][];
struct Point
{
int x, y;
char c;
};
queue<Point> m; void solve(int a, int b)
{
Point t;
int x, y, k, i, j;
while(!m.empty())
{
x = m.front().x, y = m.front().y;
m.pop();
if(p[x][y] == '*')
continue; for(k = ;k < ;++k)
{
i = x + dis[k][], j = y + dis[k][];
if(i < || i > a || j < || j > b)
continue;
if(p[i][j] == '#')
continue;
//这里可能此时i, j指向的值在后面变成 *
else if(p[i][j] == '.')
{
p[i][j] = p[x][y];
t.x = i, t.y = j, t.c = p[i][j];
m.push(t);
vis[i][j] = vis[x][y] + ;
}
else if(vis[i][j] == vis[x][y] + && p[i][j] != p[x][y])
p[i][j] = '*';
} // cout << endl;
// for(int u = 0;u < a;++u)
// {
// for(int v = 0;v < b; ++v)
// cout << p[u][v];
// cout << endl;
// }
}
} int main()
{
int T;
cin >> T;
int a, b;
while(T--)
{
Mem0(vis);
Mem0(p);
while(!m.empty())
m.pop();
Point t;
cin >> a >> b;
for(int i = ;i < a;++i)
{
getchar();
for(int j = ;j < b;++j)
{
cin >> p[i][j];
if(p[i][j] >= 'a' && p[i][j] <= 'z')
{
vis[i][j] = ;
t.x = i, t.y = j, t.c = p[i][j];
m.push(t);
}
}
}
solve(a, b);
for(int i = ;i < a;++i)
{
for(int j = ;j < b;++j)
cout << p[i][j];
cout << endl;
}
}
return ;
}
SPOJ - AMR11的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
- SPOJ GSS2 Can you answer these queries II
Time Limit: 1000MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Description Being a ...
随机推荐
- 【linux命令】setterm控制终端属性命令(中英文)
[linux命令]setterm控制终端属性命令(中英文) 2018年03月23日 17:13:44 阅读数:489 标签: linux 更多 个人分类: linux 摘自:https://blog. ...
- 命令之 ulimit
help ulimit help ulimit ulimit: ulimit [-SHacdefilmnpqrstuvx] [limit] Modify shell resource limits. ...
- 希尔伯特空间(Hilbert Space)是什么?
希尔伯特空间是老希在解决无穷维线性方程组时提出的概念, 原来的线性代数理论都是基于有限维欧几里得空间的, 无法适用, 这迫使老希去思考无穷维欧几里得空间, 也就是无穷序列空间的性质. 大家知道, 在一 ...
- 编写高质量代码改善C#程序的157个建议——建议17:多数情况下使用foreach进行循环遍历
建议17:多数情况下使用foreach进行循环遍历 由于本建议涉及集合的遍历,所以在开始讲解本建议之前,我们不妨来设想一下如何对结合进行遍历.假设存在一个数组,其遍历模式可以采用依据索引来进行遍历的方 ...
- Floyd-Warshall求图中任意两点的最短路径
原创 除了DFS和BFS求图中最短路径的方法,算法Floyd-Warshall也可以求图中任意两点的最短路径. 从图中任取两点A.B,A到B的最短路径无非只有两种情况: 1:A直接到B这条路径即是最短 ...
- JAVA的IO处理【转】
I/O简介 IO是输入和输出的简称,在实际的使用时,输入和输出是有方向的.就像现实中两个人之间借钱一样,例如A借钱给B,相对于A来说是借出,而相对于B来说则是借入.所以在程序中提到输入和输出时,也需要 ...
- mysql5.6数据库双机热备、主从备份
主题:mysql5.6数据库双机热备.主从备份 缘由: 在Web应用系统中,数据库性能是导致系统性能瓶颈最主要的原因之一.尤其是在大规模系统中,数据库集群已经成为必备的配置之一.集群的好处主要有:查询 ...
- Redis 占用Windows系统盘空间23G
Redis常出现问题总结: 1.当出现修改--maxheap and --heapdir 在启动这两个版本时都会创建一个 RedisQFork.dat文件,我不确定 RedisQFork 文件是否变小 ...
- MS SQL Server计算间隔时间
开始需要,写了一个计算时间间隔的函数,可以参考: 得到的结果: 再列举一个例子:
- OC自定义文档头部注释
1.创建文件 IDETemplateMacros.plist 2.向文件里添加内容 具体内容 // 文 件 名:___FILENAME___ // // 版权所有:___COPYRIGHT___ // ...