求助,60,4TLE
查看原帖
求助,60,4TLE
676611
SIRLRFanco楼主2022/7/26 16:36

本蒟蒻今天做题,开门“红”,成功60,代码如下不喜勿喷

#include <iostream>
#include <algorithm>
using namespace std;

int n, k, l[200005], r[200005], a[200005];
void del(int x) {//删除x号元素 
	r[l[x]] = r[x];
	l[r[x]] = l[x];
}
void insertR(int pos, int x) {//在pos号右侧空隙插入x号元素 
	l[x] = pos, r[x] = r[pos];
	r[l[x]] = x, l[r[x]] = x;
}
void insertL(int pos, int x) {//在pos号左侧空隙插入x号元素 
	l[x] = l[pos], r[x] = pos;
	r[l[x]] = x, l[r[x]] = x;
}


bool vis[200005] = {0};//vis[i]=true表示i号拿走了 
int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1; i <= n; i++) 
		cin >> a[i];
	//初始化链表
	for (int i = 0; i <= n + 1; i++)
		l[i] = i - 1, r[i] = i + 1; 
	  
	while (r[0] != n + 1) {//头位置的后边不是尾,说明还有剩 
		//剩余第一个或和剩余的上一个不同类,则是一块的第一个 
    	for (int i = r[0]; i != n + 1; i = r[i])
			if (l[i] == 0 || a[i] != a[l[i]]) 
				(cout << i << " "), vis[i] = true;//标记,输出,先不删除
		cout << endl;
		//把标记的删除
		for (int i = r[0]; i != n + 1; i = r[i])
			if (vis[i]) 
				del(i);
	} 

	return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;

int n, k, l[200005], r[200005], a[200005];
void del(int x) {//删除x号元素 
	r[l[x]] = r[x];
	l[r[x]] = l[x];
}
void insertR(int pos, int x) {//在pos号右侧空隙插入x号元素 
	l[x] = pos, r[x] = r[pos];
	r[l[x]] = x, l[r[x]] = x;
}
void insertL(int pos, int x) {//在pos号左侧空隙插入x号元素 
	l[x] = l[pos], r[x] = pos;
	r[l[x]] = x, l[r[x]] = x;
}


bool vis[200005] = {0};//vis[i]=true表示i号拿走了 
int main()
{
	ios::sync_with_stdio(0);
	cin >> n;
	for (int i = 1; i <= n; i++) 
		cin >> a[i];
	//初始化链表
	for (int i = 0; i <= n + 1; i++)
		l[i] = i - 1, r[i] = i + 1; 
	  
	while (r[0] != n + 1) {//头位置的后边不是尾,说明还有剩 
		//剩余第一个或和剩余的上一个不同类,则是一块的第一个 
    	for (int i = r[0]; i != n + 1; i = r[i])
			if (l[i] == 0 || a[i] != a[l[i]]) 
				(cout << i << " "), vis[i] = true;//标记,输出,先不删除
		cout << endl;
		//把标记的删除
		for (int i = r[0]; i != n + 1; i = r[i])
			if (vis[i]) 
				del(i);
	} 

	return 0;
}

2022/7/26 16:36
加载中...