1074 reverse list

最后一个测试用例很坑,注意给出的n个节点不一定都在以h为头的链表上,这种测试用例在PAT链表相关的问题中多次出现,要予以注意。

AC代码

#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
class node{
public:
    int addr;
    int v;
    int next;
};
int main(){
    int h,n,k;
    scanf("%d %d %d",&h,&n,&k);
    vector<node> list;
    map<int,node> rec;
    for(int i = 0;i < n;i++){
        node tmp;
        scanf("%d %d %d",&tmp.addr,&tmp.v,&tmp.next);
        rec[tmp.addr] = tmp;
    }
    int addr(h);
    int count(0);
    while(addr != -1){
        list.push_back(rec[addr]);
        addr = rec[addr].next;
    }
    n = list.size();
    if(n < k){
        for(int i = 0;i < list.size();i++){
            if(list[i].next != -1)
                printf("%05d %d %05d\n",list[i].addr,list[i].v,list[i].next);
            else
                printf("%05d %d -1\n",list[i].addr,list[i].v);
        }
        return 0;
    }
    for(int i = 0;i < n / k;i++){
        reverse(list.begin() + i * k,list.begin() + (i+1) * k);
        for(int j = i * k;j < (i+1) * k;j++){
            if(j != (i+1) * k - 1)
                list[j].next = list[j+1].addr;
        }
    }
    for(int i = 0;i < n / k;i++){
        if((i+1) * k < n)
            list[i * k + k - 1].next = list[(i+1) * k].addr;
        else
            list[i * k + k - 1].next = -1;
    }
    list.back().next = -1;
    for(int i = 0;i < list.size();i++){
        if(list[i].next != -1)
            printf("%05d %d %05d\n",list[i].addr,list[i].v,list[i].next);
        else
            printf("%05d %d -1\n",list[i].addr,list[i].v);
    }
    return 0;
}
时间: 2024-08-12 04:39:56

1074 reverse list的相关文章

pat解题报告【1074】

1074. Reversing Linked List (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→

PAT 1074. Reversing Linked List (25)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6. Input Specifica

1074. Reversing Linked List (25)

reverse 方法很好用 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then yo

PAT 1074 Reversing Linked List[链表][一般]

1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you mus

Django url 标签和reverse()函数的使用(转)

原文:http://www.yihaomen.com/article/python/355.htm 使用url标签和reverse()函数,可以避免在模板和view中对url进行硬编码,这样即使url改变了,对模板和view也没有影响 起初用django 开发应用的时候,完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRedirect()也是硬编码转向地址,当然在template 中也是一样了,这样带来一个问题,如果在urls.py 中修改了某个页面的地址,

186. Reverse Words in a String II

https://leetcode.com/problems/reverse-words-in-a-string-ii/#/description Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters. The input string does not contain leading or trailing spaces and

codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)

题目链接: C. Mashmokh and Reverse Operation time limit per test 4 seconds memory limit per test 512 megabytes input standard input output standard output Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university

Forward Proxy &amp; Reverse Proxy | 正向代理 和 反向代理

对请求和响应内容不做修改的转发的服务器,被称为代理服务器.代理服务器分为两种类型:正向代理 和 反向代理. 正向代理:面向互联网,从更广范围获取信息的代理. 反向代理:面向内部,一般用于某企业的网站的前端的代理.反向代理能承担负载均衡,身份认证,内容缓存的任务.这些功能在反向代理上面实现会显得很自然. 正向代理: 如果使用过 vpn 或者 shadowsocks 等FQ工具访问 Google,那么就是在使用正向代理服务器. 下面的图例解释了正向代理的使用.正向代理服务器在互联网中扮演用户的角色,

Reverse Linked List

题目: Reverse a singly linked list. cpp: class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *p3 = head->next->next; p1->next = nullpt