80分超时暴力代码:读入数据,对于每个待操作坐标都重新对于所给区间进行平移,旋转的坐标变换
#include <iostream>
#include <cmath>
using namespace std;
struct mani
{
int type;
double num;
};
mani all[100001];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++)
{
mani temp;
scanf("%d %lf", &temp.type, &temp.num);
all[i] = temp;
}
for(int i = 0; i < m; i++)
{
int st, ed;
double x, y;
scanf("%d %d %lf %lf", &st, &ed, &x, &y);
for(int i = st-1; i < ed; i++)
{
if(all[i].type == 1)
{
x *= all[i].num;
y *= all[i].num;
}
else
{
double x_t = x;
double y_t = y;
x = x_t * cos(all[i].num) - y_t * sin(all[i].num);
y = x_t * sin(all[i].num) + y_t * cos(all[i].num);
}
}
printf("%f %f\n", x, y);
}
return 0;
}
满分代码:观察到在连续的区间内进行坐标的平移变换具有无序性,也就是说,多次旋转和平移操作可以通过改变操作顺序以及同类合并合成两个平移、旋转大操作,平移大操作的拉伸系数k相当于区间内所有拉伸系数ki的积,旋转大操作的旋转角θ相当于区间内所有旋转角θi之和。
结合连续区间内的可加性,可以通过求前缀和数组简化连续区间内的坐标变换操作。
求前缀和方法如下:
info s[100002];
info temp_s;
temp_s.angle = 0; //初始化旋转角大小为0
temp_s.times = 1; //初始化伸缩系数为1
s[0] = temp_s;
for(int i = 0; i < n; i++) //共n个操作
{
int type;
double value;
scanf("%d %lf", &type, &value); //操作的类型,取值
if(type == 1)
temp_s.times *= value;
else
temp_s.angle += value;
s[i+1] = temp_s;
}
在连续区间内只需要对前缀和数组进行还原操作即可
int st, ed; //起始操作,结束操作
scanf("%d %d", &st, &ed);
double times_t, angle_t;
times_t = s[ed].times / s[st-1].times; //区间内伸缩系数
angle_t = s[ed].angle - s[st-1].angle; //区间内旋转角之和
总代码如下
#include <iostream>
#include <cmath>
using namespace std;
struct info
{
double times;
double angle;
};
info s[100002];
int main()
{
int n, m;
scanf("%d %d", &n, &m);
info temp_s;
temp_s.angle = 0;
temp_s.times = 1;
s[0] = temp_s;
for(int i = 0; i < n; i++)
{
int type;
double value;
scanf("%d %lf", &type, &value);
if(type == 1)
temp_s.times *= value;
else
temp_s.angle += value;
s[i+1] = temp_s;
}
for(int i = 0; i < m; i++)
{
int st, ed;
double x, y;
scanf("%d %d %lf %lf", &st, &ed, &x, &y);
double times_t, angle_t;
times_t = s[ed].times / s[st-1].times;
angle_t = s[ed].angle - s[st-1].angle;
x *= times_t;
y *= times_t;
double x_t = x;
double y_t = y;
x = x_t * cos(angle_t) - y_t * sin(angle_t);
y = x_t * sin(angle_t) + y_t * cos(angle_t);
printf("%f %f\n", x, y);
}
return 0;
}