zoukankan      html  css  js  c++  java
  • CCCC L2-022. 重排链表

    题解:直接list模拟,头尾两个指针,分别将头尾元素push到另一个list里面,输处输入方式同上一篇

    坑:第一发卡了第二个样例,第二发卡了第4个,莫名其妙,所以把两个代码合起来,然后强行ac了。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <map>
    #include<stack>
    #include<set>
    #include<string.h>
    #include<list>
    #define pb push_back
    #define mp make_pair
    #define _for(i, a, b) for (int i = (a); i<(b); ++i)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    
    using namespace std;
    const  int N = 100000 + 5;
    //double num[N], price[N], ave[N];
    int nxt[N], val[N];
    int nxt1[N];
    map<int, int> p;
    list<pair<int, int> >l,ll;
    int main() {
        int head, n;
        cin >> head >> n;
        _for(i, 0, n) {
            int x;
            cin >> x;
            cin >> val[x] >> nxt[x];
    
        }
    
        for (int p = head; p != -1; p = nxt[p]) {
            l.push_back(mp(p, val[p]));
        }
        
        list<pair<int, int> >::iterator it1,it2;
        it2 = l.end(); it2--;
        if (n % 2 == 1) {
            for (it1 = l.begin();;) {//
                ll.push_back(*it2); n--; if (n == 0)break;
                it2--; //if (it1 == it2)break;
                ll.push_back(*it1); n--; if (n == 0)break;
                it1++;// if (it1 == it2)break;
    
            }
        }
        else {
            for (it1 = l.begin(); it1 != it2;) {
                ll.push_back(*it2);
                ll.push_back(*it1);
    
                it1++; if (it1 == it2)break;
                it2--;
            }
        }
        for (it1 = ll.begin(); it1 != ll.end(); ) {
            printf("%05d %d ", it1->first, it1->second);
            //cout << it->first << ' ' << it->second << ' ';
            if (++it1 != ll.end())printf("%05d
    ", it1->first);//cout<< it->first << endl;
                else cout << -1 << endl;
        }
    
        system("pause");
    
    
    }
    /*
      00100 3
    
    23854 2 1
    1 3 -1
    00100 1 23854
    */
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    联表查询更新
    SQLServer 中多行数据合并成一行数据(一个字段)
    换行和回车的区别
    SQL语句中使用回车换行符
    g2o使用总结
    求导总结
    ubuntu14.04 升级gcc
    如何入门SLAM
    imu和canmera标定
    使用velodyne16线激光雷达跑loam-velodyne------包括激光雷达和imu的标定
  • 原文地址:https://www.cnblogs.com/SuuT/p/8668748.html
Copyright © 2011-2022 走看看