【CCF CSP】202309-2 坐标变换(其二)

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

  • 26
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andy_Xie007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值