第31次csp2023-9

第一题坐标变换(其一)

无脑做

#include<iostream>
using namespace std;
const int N=110,M=110;
int n,m;
int x[M],y[M];
int dx[N],dy[N]; 
void trans(int k){
    for(int i=0;i<n;i++){
        x[k]+=dx[i];
        y[k]+=dy[i];
    }
    return ;
}
int main(){
    //读入数据 
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d%d",&dx[i],&dy[i]);
    for(int i=0;i<m;i++) scanf("%d%d",&x[i],&y[i]);
    //执行操作
    for(int i=0;i<n;i++){
        trans(i);
    } 
    for(int i=0;i<m;i++) printf("%d %d\n",x[i],y[i]);
    
    return 0;
}

第二题坐标变换(其二)

暴力做法o(n^2) 会超时

暴力做法如下:

#include<iostream>
#include<cmath> 
using namespace std;
const int N=1e5+10;
int n,m;
double x,y;
int trans_type[N];
double trans_value[N];
void trans(int l,int r){
	for(int i=l;i<=r;i++){
		if(trans_type[i]==1){
			x=trans_value[i]*x;
			y=trans_value[i]*y;
		}
		else {
			double sita=trans_value[i];
			double temp1 =x*cos(sita)-y*sin(sita);
			double temp2 =x*sin(sita)+y*cos(sita);
			x=temp1;
			y=temp2;
		} 
	}
	return ; 
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d%lf",&trans_type[i],&trans_value[i]);
	while(m--){
		int l,r;
		scanf("%d%d%lf%lf",&l,&r,&x,&y);
		trans(l,r);
		printf("%.3f %.3f\n",x,y);
	} 
	return 0;
}

尝试结合前缀和思想进行优化,将每次操作看作对x乘上了某个系数,第l次操作到第r次操作就相当于Xr/Xl-1,但是跑样例不对,可能是坐标的伸长和旋转的混合运算在直角坐标系中不适用12212112

#include<iostream>
#include<cmath>
using namespace std;
const int N=1e6+10;
int n,m;
double changex[N],changey[N];
int main(){
	scanf("%d%d",&n,&m);
	changex[0]=1.0; changey[0]=1.0;
	for(int i=1;i<=n;i++){
		int type;double value;
		scanf("%d%lf",&type,&value);
        if(type==1) {
            changex[i]=changex[i-1]*value;
            changey[i]=changey[i-1]*value;
        }
        else{
            double temp1,temp2;
            temp1=changex[i-1]*cos(value)-changey[i-1]*sin(value);
            temp2=changex[i-1]*sin(value)+changey[i-1]*cos(value);
            changex[i]=temp1;
            changey[i]=temp2;
        }
	} 
	while(m--){ 
		int l,r;
		double x,y;
		scanf("%d%d%lf%lf",&l,&r,&x,&y);
		x*=changex[r]/changex[l-1];
		y*=changey[r]/changey[l-1];
		printf("%.3lf %.3lf\n",x,y);
	}
	
	return 0;
}

 改用极坐标系进行坐标变换即可,可以看到对坐标的伸长运算前缀和计算方法为len[r]/len[l-1],而对坐标的旋转运算计算方法为theta[r]-theta[l-1]。

#include<iostream>
#include<cmath>
using namespace std;
const int N=1e5+10;
int n,m;
double len[N],theta[N];
int main(){
	scanf("%d%d",&n,&m);
	len[0]=1.0;theta[0]=1.0;
	for(int i=1;i<=n;i++){
		int type;double value;
		scanf("%d%lf",&type,&value);
		if(type==1){
			len[i]=len[i-1]*value;
			theta[i]=theta[i-1];
		}
		else {
			len[i]=len[i-1];
			theta[i]=theta[i-1]+value;
		}
	}
	while(m--){
		int l,r;double x,y;
		scanf("%d%d%lf%lf",&l,&r,&x,&y);
		double tmplen=len[r]/len[l-1];
		double tmptheta=theta[r]-theta[l-1];
		x*=tmplen;
		y*=tmplen;
		double tmpx,tmpy; 	
		tmpx=x,tmpy=y;
		x=tmpx*cos(tmptheta)-tmpy*sin(tmptheta);
		y=tmpx*sin(tmptheta)+tmpy*cos(tmptheta);
		printf("%.3lf %.3lf\n",x,y);
	}
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值