zoukankan      html  css  js  c++  java
  • 牛客红包OI赛 B 小可爱序列

    Description

    链接:https://ac.nowcoder.com/acm/contest/224/B
    来源:牛客网

    ”我愿意舍弃一切,以想念你,终此一生。“
    ”到后来,只能将记忆拼凑。“ ——QAQ
    小可爱刚刚把KR的序列切开了,但是她还没有玩够,于是就又双叒叕打乱了佳佳刚刚买回来的序列。
    但是还好,佳佳通过监控记录下来了小可爱的打乱方式,于是把小可爱送回家之后,现在佳佳要还原这个序列。
    佳佳需要维护一个长度为n的序列,小可爱只用了以下两种操作:
    a.将最后一个数挪到第一位
    b.将序列第3位挪到第一位
    你需要给出最后的序列

    Solution

    某人跟我说是链表
    然后就没读题无脑做
    然后TLE之后发现不能无脑做

    然后有脑一下

    Code

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    struct Node {
    	int v; Node *nxt, *pre;
    	Node(int _v, Node *_n = nullptr, Node *_p = nullptr) :
    		v(_v), nxt(_n), pre(_p) { }
    } *head, *tail;
    
    void Delete(const Node* b, const Node* e = nullptr) {
    	if (not e) e = b;
    	b->pre->nxt = e->nxt;
    	e->nxt->pre = b->pre;
    }
    void InsertNxt(Node* p, Node* b, Node* e = nullptr) {
    	if (not e) e = b;
    	Node* Pre = p;
    	Node* Nxt = p->nxt;
    	Pre->nxt = b;
    	Nxt->pre = e;
    	b->pre = Pre;
    	e->nxt = Nxt;
    }
    void InsertPre(Node* p, Node* b, Node* e = nullptr) {
    	if (not e) e = b;
    	Node* Pre = p->pre;
    	Node* Nxt = p;
    	Pre->nxt = b;
    	Nxt->pre = e;
    	b->pre = Pre;
    	e->nxt = Nxt;
    }
    
    void Sol1(int n) {
    	Node* Beg = tail, *End = tail->pre;
    	while (n--) { Beg = Beg->pre; }
    	Delete(Beg, End);
    	InsertNxt(head, Beg, End);
    }
    void sol2() {
    	Node *now = head->nxt->nxt->nxt;
    	Delete(now);
    	InsertNxt(head, now);
    }
    void Show() {
    	Node* now = head->nxt;
    	while (now != tail) {
    		printf("%d ", now->v);
    		now = now->nxt;
    	}
    	puts("");
    }
    
    int main () {
    	int n, m;
    	scanf("%d%d", &n, &m);
    	int u;
    	tail = new Node(0); head = new Node(0);
    	tail->pre = head, head->nxt = tail;
    	for (int i = 1; i <= n; i += 1) {
    		scanf("%d", &u);
    		InsertPre(tail, new Node(u));
    	}
    	for (int i = 1; i <= m; i += 1) {
    		char ch; int u;
    		scanf("%d%c", &u, &ch);
    		if (ch == 'a') {
    			u %= n;
    			if (u) Sol1(u);
    		}
    		else {
    			u %= 3;
    			while(u--) sol2();
    		}
    	}
    	Show();
    	return 0;
    }
    
  • 相关阅读:
    Python(21):numpy数组模块
    ASP.NET(99):ASP.NET 内容管理系统CMS
    Python(20):Python常用模块sys、random、math
    Python(19):Python标准库: 日期、时间和日历模块
    Python(18):Python模块基础
    微信官方文档
    Python(17):Python面向对象高级
    Python(16):Python面向对象进阶
    Python(15):Python面向对象基础
    Python(14):python异常处理
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9892636.html
Copyright © 2011-2022 走看看