当前位置:网站首页>PAT1165 Block Reversing(25)
PAT1165 Block Reversing(25)
2022-08-01 12:51:00 【此杭非彼航】
题目



代码
#include<bits/stdc++.h>
using namespace std;
struct Node{
int addr,val,next,index,init_pos;
};
unordered_map<int,Node> li;
vector<Node> vec;
bool cmp(Node x,Node y){
if(x.index!=y.index) return x.index>y.index;
else return x.init_pos<y.init_pos;
}
int main()
{
int root,n,k;
cin>>root>>n>>k;
for(int i=0;i<n;++i){
int x,y,z;
cin>>x>>y>>z;
li[x]={
x,y,z};
}
int pos=1,cnt=0;
while(root!=-1){
cnt++;
li[root].index=pos;
li[root].init_pos=cnt;
if(cnt==k){
cnt=0;
pos++;
}
vec.push_back(li[root]);
root=li[root].next;
}
sort(vec.begin(),vec.end(),cmp);
// printf("------\n");
for(int i=0;i<vec.size();++i){
printf("%05d %d ",vec[i].addr,vec[i].val);
if(i==vec.size()-1) printf("-1");
else printf("%05d\n",vec[i+1].addr);
}
}
思路
给block标序号(index),同一个block内的结点按原始顺序标号(init_pos),然后按照index从大到小排序,同一个block按init_pos从小到大排序
注意
虽然例题中所有的结点都是可用的,但测试点所给的数据是有可能存在脏数据的,因为在输出时不能下意识地按n输出,而是要按vec内实际的数量输出(vec.size())。
但谁能想到,我一开始改了for循环的n,却忘了改if判断里的n,一直是if(i==n-1),直接导致我最后一个测试点一直无法通过,好笨
边栏推荐
- Multithreading Case - Timer
- 快速幂---学习笔记
- Batch replace tables in Word with pictures and save
- 找出相同属性值的对象 累加数量 汇总
- 易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
- Simulation implementation of new of Js handwritten function
- 英特尔全方位打造算力基础,助推“算”赋百业
- 达梦更换正式授权dm.key
- 28uA待机8米距离低压保护单片机探头太阳能灯人体PIR定制方案
- NFV迈向云原生时代:Network Service Mesh项目介绍
猜你喜欢

leetcode:1201. 丑数 III【二分 + 数学 + 容斥原理】

Find objects with the same property value Cumulative number Summarize

Multi-threaded cases - blocking queue

What is consistent hashing?In what scenarios can it be applied?

HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界

Towhee 每周模型

Programmer's self-cultivation

8. How does the SAP ABAP OData service support the Create operation

The basic knowledge of scripting language Lua summary

脚本语言Lua的基础知识总结
随机推荐
Istio投入生产的障碍以及如何解决这些问题
如何降低Istio服务网格中Envoy的内存开销
MySQL调优
PanGu-Coder:函数级的代码生成模型
阿里云官方 Redis 开发规范
Fault 007: The dexp derivative is inexplicably interrupted
如何设计一个分布式 ID 发号器?
CloudCompare & PCL ICP registration (point to face)
程序员的自我修养
How does the SAP ABAP OData service support the Create operation trial version
kubernetes之DaemonSet以及滚动更新
ECCV22|只能11%的参数就能优于Swin,微软提出快速预训练蒸馏方法TinyViT
观察者模式
SQL函数 SQUARE
初级必备:单例模式的7个问题
CloudCompare&PCL ICP配准(点到面)
SQL functions STR
Programmer's self-cultivation
JMP Pro 16.0 software installation package download and installation tutorial
力扣160题,相交链表