全TLE,还有什么优化空间吗?
查看原帖
全TLE,还有什么优化空间吗?
301765
ElfOfEra楼主2022/12/24 10:33

这是我几分钟前写的链表解法:

#include<iostream>
#include<cstdlib>
using namespace std;
int main(){
	int n,m;
	cin>>n>>m;
	struct stu{
		int n;
		struct stu *next;
	}*a,*b;
	a=b=(struct stu*)malloc(sizeof(struct stu));
	for(int i=1;i<=n;i++){
		cin>>b->n;
		struct stu *c=(struct stu*)malloc(sizeof(struct stu));
		b->next=c;
		b=b->next;
	}
	for(int i=1;i<=m;i++){
		struct stu *tmp=a;
		int t;
		cin>>t;
		for(int o=1;o<t;o++){
			tmp=tmp->next;
		}
		printf("%d\n",tmp->n);
	}
	return 0;
}

这是我一年半以前硬写出来的粗暴解法:

#include<iostream>
using namespace std;
long long a[2000001];
int main(){
    int n,m,i;
    cin>>n>>m;
    for(int o=1;o<=n;o++){
        cin>>a[o];
    }
    for(int o=1;o<=m;o++){
        int s;
        cin>>s;
        if(o==m){
            cout<<a[s];
        }else cout<<a[s]<<endl;
    }
    return 0;
}

求问前者还有什么优化方案吗?大一新生刚学链表想试试手

2022/12/24 10:33
加载中...