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;
    }
    
  • 相关阅读:
    IDEA创建test测试类
    SpringBoot Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration
    Mysql在线加索引锁表归纳
    工作感悟--对上一份工作总结
    ESP8266获取网络NTP时间(转)
    Python中的CGI编程 config配置(windows、Apache) 以及后期的编写(转)
    CGI与FastCGI(转)
    JSON-RPC轻量级远程调用协议介绍及使用
    java插件化编程(动态加载)
    PF4J入门指南
  • 原文地址:https://www.cnblogs.com/qdscwyy/p/9892636.html
Copyright © 2011-2022 走看看