UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=648&page=show_problem&problem=5151
You have a rectangle of size N × M, N rows from top to bottom and M columns from left to right,
that is divided into a grid of unit squares. The corners and sides of those squares will be called grid
points and grid lines, respectively.
You are given a path along some grid lines. The path satisfies the following properties:
• Both start and end of the path are at the top left grid point.
• Each step is to go along the grid line (i.e., move up, down, left, or right).
You need to calculate the square sum of all the rotation values in each all. The definition of the
notation value in each cell is below.
Suppose there is a moving car at the path and a person stands at the center of the cell. The person
is facing the car all the time. After the path is finished, the rotation value of the grid equals to the
net number of clockwise turns the person would make if he stood in that square. (In other words, if
the person standing in that square rotate by the same total amount clockwise and counterclockwise,
the rotation value is 0. If the person’s total clockwise rotation is 360x degrees more than the person’s
total counterclockwise rotation, the rotation value of the cell is x. If the person’s total couuterclockwise
rotation is 360x degrees more than the person’s total clockwise rotation, the rotation value of the cell
is −x)
Input
The first line of the input gives the number of test cases, T. T cases follow. For each test case, the first
line contains three numbers, N, M and K. The next K line describes the steps of the path. Each line
containing ‘d s’, where d is one of the four characters (‘U’ for up, ‘D’ for down, ‘L’ for left, ‘R’ for right)
means the direction of the step and s is the length of the step.
It is guaranteed that the path is inside the grid.
Output
For each test case, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is square sum of all the rotation values of each cell.
题目大意:给一个n*m的矩阵,每个方块上有一个人。现在有一辆车在左上角的格点处,矩阵里的人都会一直面向那辆车。现在给出车的移动路线,问每个人总旋转角度的平方和是多少。若一个人顺时针旋转10个圈,逆时针旋转15个圈,最终算旋转角度为5个圈。
思路:根据题意,车一定会回到原点,那么每个人的初始面向方向与最终面向方向相同,每个人旋转的圈数都必将是整数。
若车在人的正左方下降了X次,上升了Y次,那么那个人的旋转圈数便是abs(X-Y)。(不要问我为什么,多想想多想想)
然后暴力模拟车的移动:
在车向下移动的时候,我们就要给车影响右方的一个矩阵加上1。
在车向上移动的时候,我们就要给车影响右方的一个矩阵减去1。
为了给一个矩阵加上一个值,因为本题只需要最终结果,可以使用静态的矩阵前缀和的方式处理。
最后求出矩阵,直接累加结果即可。
代码(0.249S):
#ifdef OYK_JUDGE
#define longformat "%I64d"
#else
#define longformat "%lld"
#endif // OYK_JUDGE #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL; vector<vector<int> > mat;
int T, n, m, k; char str[] = "RDLU";
int fr[] = {, , , -};
int fc[] = {, , -, }; void modify(int c, int r1, int r2, int val) {
mat[r1][c] += val;
mat[r2][c] -= val;
} LL solve() {
int step, r = , c = ;
char op;
while(k--) {
scanf(" %c%d", &op, &step);
if(op == 'D') modify(c, r, r + step, );
if(op == 'U') modify(c, r - step, r, -); int f = strchr(str, op) - str;
r += step * fr[f];
c += step * fc[f];
} LL res = ;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) {
mat[i][j] = mat[i][j] + mat[i - ][j] + mat[i][j - ] - mat[i - ][j - ];
res += mat[i][j] * mat[i][j];
}
return res;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d%d", &n, &m, &k);
mat = vector<vector<int> >(n + , vector<int>(m + , ));
LL res = solve();
printf("Case #%d: " longformat "\n", t, res);
}
}
UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)的更多相关文章
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7141 BombX(离散化+线段树)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...
- UVALive 7148 LRIP(树的分治+STL)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- hdu5071 2014 Asia AnShan Regional Contest B Chat
模拟题: add的时候出现过的则不再添加 close的时候会影响到top rotate(Prior.Choose)的时候会影响到top /*============================== ...
- 2014 Asia AnShan Regional Contest --- HDU 5073 Galaxy
Galaxy Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5073 Mean: 在一条数轴上,有n颗卫星,现在你可以改变k颗 ...
- dp --- 2014 Asia AnShan Regional Contest --- HDU 5074 Hatsune Miku
Hatsune Miku Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5074 Mean: 有m种音符(note),现在要从 ...
随机推荐
- canvas画布属性globalAlpha 和 createRadialGradient函数出现的设置问题
今天用canvas做了一个页面特效,呼呼,在做的过程中发现createRadialGradient 和 globalAlpha这2个属性一起使用导入不能实现透明度问题,首先把createRadialG ...
- Only Link: What's the difference between dynamic dispatch and dynamic binding
http://stackoverflow.com/questions/20187587/what-is-the-difference-between-dynamic-dispatch-and-late ...
- js闭包Demo
我们先看一个关于Javascript利用循环绑定事件的例子: 例如:一个不确定长度的列表,在鼠标经过某一条的时候改变背景. ﹤!DOCTYPE html PUBLIC "-//W3C// ...
- PK淘宝BUY+,京东推出AR购物应用JD Dream
今年双十一淘宝推出了虚拟现实VR购物"BUY+",用户可以在虚拟环境中选购商品.那作为竞争对手的京东将使出什么绝招呢?在近日上海举办的谷歌开发者大会上得到了答案.会上京东推 ...
- 洛谷 P1373 小a和uim之大逃离 Label:dp 不会
题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北部天边急涌过来,还伴着一道道闪电,一阵阵雷声.刹那间,狂风大作,乌云布满了天空,紧接着豆大的雨点从天空中打落下来,只见前方出现了一个 ...
- SQL获取当前时间(日期)
--获取当前日期(如:yyyymmdd) select CONVERT (nvarchar(12),GETDATE(),112) --获取当前日期(如:yyyymmdd hh:MM:ss) selec ...
- [LintCode] Majority Number 求众数
Given an array of integers, the majority number is the number that occurs more than half of the size ...
- Hive_数据倾斜处理
Hive中三种join map join (小表join大表,将小表加入到内存) 设置map join: hive.auto.convert.join=true hive.mapjoin.smallt ...
- Unity运行时检测Altas使用情况
UI贴图在游戏中内存大小中占的分量非常非常大,尤其对于前期对UI没有规划的项目,无论是包量还是内存大小都是需要花费很多时间去优化.如果涉及到战斗场景和逻辑场景的情况下,常用的做法就是把两个场景使用的a ...
- RDLC 设置标题每页显示