国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

算法提高 盾神與條狀項鏈 雙向鏈表

2019-11-06 06:31:31
字體:
來源:轉載
供稿:網友

       思路:用雙向鏈表,next[x] = y表示顏色為x的珠子的下一個珠子的顏色是y,PRe[x] = y表示表示顏色為x的珠子的上一個珠子的顏色是y。時間復雜度是O(n + m)

#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10#define inf 0x3f3f3f3f#define PI pair<int, int> typedef long long LL;const int maxn = 1e5 + 5;int next[maxn], pre[maxn];int n, m, head, len;//雙向鏈表 void Insert(int key, int pos1, int pos2) {	++len;	next[key] = pos2;	next[pos1] = key;	pre[key] = pos1;	pre[pos2] = key;}void init(int n) {	head = 0; next[head] = 0; pre[head] = 0;	int x, prev = 0;	for(int i = 0; i < n; ++i) {		scanf("%d", &x);		Insert(x, prev, next[prev]);		prev = x;	}}void Delete(int key, int pos1, int pos2) {	--len;	next[pos1] = pos2;	pre[pos2] = pos1;}void print() {	printf("%d/n", len); 	int nex = next[head];	while(nex) {		printf("%d ", nex);		nex = next[nex];	}	printf("/n");}int main() {	while(scanf("%d%d", &n, &m) == 2) {		len = 0;		init(n);		char op[5];		int x, y;		for(int i = 0; i < m; ++i) {			scanf("%s", op);			if(op[0] == 'A') {				scanf("%d%d", &x, &y);				Insert(y, pre[x], x);			}			else {				scanf("%d", &x);				Delete(x, pre[x], next[x]);			}		}		print();	}	return 0;}如有不當之處歡迎指出!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潢川县| 杭州市| 东宁县| 武城县| 花莲市| 临夏县| 库车县| 竹溪县| 叙永县| 红河县| 盈江县| 府谷县| 长阳| 永州市| 鲁山县| 伊金霍洛旗| 马鞍山市| 海宁市| 南和县| 乌鲁木齐市| 古蔺县| 旅游| 安国市| 淮滨县| 睢宁县| 合川市| 巴彦淖尔市| 磴口县| 英德市| 大足县| 吴堡县| 工布江达县| 鄂伦春自治旗| 西峡县| 南昌市| 盱眙县| 黑河市| 临武县| 莫力| 德清县| 泸溪县|