【NY8】一种排序sort+结构体

本文介绍了一种针对多个长方形的排序算法,通过比较长方形的编号、长度和宽度来实现有序排列。首先按照编号从小到大排序,对于编号相同的长方形则进一步按长度和宽度进行排序,最终去除重复项并输出所有长方形的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一种排序

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述
现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复;还知道这个长方形的宽和长,编号、长、宽都是整数;现在要求按照一下方式排序(默认排序规则都是从小到大);

1.按照编号从小到大排序

2.对于编号相等的长方形,按照长方形的长排序;

3.如果编号和长都相同,按照长方形的宽排序;

4.如果编号、长、宽都相同,就只保留一个长方形用于排序,删除多余的长方形;最后排好序按照指定格式显示所有的长方形;
输入
第一行有一个整数 0<n<10000,表示接下来有n组测试数据;
每一组第一行有一个整数 0<m<1000,表示有m个长方形;
接下来的m行,每一行有三个数 ,第一个数表示长方形的编号,

第二个和第三个数值大的表示长,数值小的表示宽,相等
说明这是一个正方形(数据约定长宽与编号都小于10000);
输出
顺序输出每组数据的所有符合条件的长方形的 编号 长 宽
样例输入
1
8
1 1 1
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
样例输出
1 1 1
1 2 1
1 2 2
2 1 1

2 2 1

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct rec{
	int num,a,b;
}g[1000];
bool cmp(rec A,rec B){
	if(A.num==B.num){
		if(A.a==B.a)
		return A.b<B.b;
		else return A.a<B.a;
	}
	else return A.num<B.num;
}
int main(){
	int n,m;
	scanf("%d",&n);
	while(n--){
	scanf("%d",&m);	
	for(int i=0;i<m;i++){
		scanf("%d%d%d",&g[i].num,&g[i].a,&g[i].b);
		if(g[i].a<g[i].b)
		swap(g[i].a,g[i].b);
	}
	sort(g,g+m,cmp);
	printf("%d %d %d\n",g[0].num,g[0].a,g[0].b);
	for(int i=1;i<m;i++){
		if(g[i].num==g[i-1].num&&g[i].a==g[i-1].a&&g[i].b==g[i-1].b)
		continue;
		 printf("%d %d %d\n",g[i].num,g[i].a,g[i].b);
	}
	}
	return 0;
}


#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x7ffffff #define rep(i,s,t) for(register ll i = s;i <= t;++i) #define per(i,t,s) for(register ll i = t;i >= s;--i) const ll N = 25; const ll M = 1e5 + 5; struct node { ll x; ll y; ll rnk; bool operator < (const node& u) const { return rnk < u.rnk; } }; ll n; ll m; ll len; ll msk; ll ans = INF; ll dx[4] = {-1,0,1,0}; ll dy[4] = {0,1,0,-1}; vector <node> s; char a[N][N] = {}; bool vis[N][N][M] = {}; inline ll read() { ll x = 0; ll y = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') y = -y; c = getchar(); } while(c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ '0'); c = getchar(); } return x * y; } inline void write(ll x) { if(x < 0) { putchar('-'); write(-x); return; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void bfs() { queue <tuple <ll,ll,ll,ll> > q; q.push({s[0].x,s[0].y,msk,0}); vis[s[0].x][s[0].y][msk] = true; while(!q.empty()) { auto [x,y,cur,stp] = q.front(); q.pop(); if(a[x][y] == '@') { ans = stp; break; } rep(i,0,3) { ll nx = x + dx[i]; ll ny = y + dy[i]; if(nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == '#') continue; ll cx = x; ll cy = y; bool flag = false; rep(i,0,len - 3) { cx += dx[(cur >> (i << 1)) & 3]; cy += dy[(cur >> (i << 1)) & 3]; if(nx == cx && ny == cy) { flag = true; break; } } if(flag) continue; ll pos = (i + 2) % 4; if(len > 2) { ll tmp = (1 << ((len - 2) << 1)) - 1; pos |= (cur & tmp) << 2; } if(!vis[nx][ny][pos]) { vis[nx][ny][pos] = true; q.push({nx,ny,pos,stp + 1}); } } } } int main() { cin >> n >> m; rep(i,1,n) { rep(j,1,m) { cin >> a[i][j]; if(a[i][j] >= '1' && a[i][j] <= '9') s.push_back({i,j,(ll)(a[i][j] - '0')}); } } sort(s.begin(),s.end()); len = s.size(); rep(i,1,len - 1) { ll nx = s[i].x - s[i - 1].x; ll ny = s[i].y - s[i - 1].y; rep(j,0,3) { if(nx == dx[j] && ny == dy[j]) { msk |= (j << ((i - 1) << 1)); break; } } } bfs(); if(ans == INF) cout << -1; else cout << ans; return 0; }请详细解释上述代码每一行每一个操作的直接作用和整体功能,特别是位运算的原理
最新发布
07-18
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x7ffffff #define rep(i,s,t) for(register ll i = s;i <= t;++i) #define per(i,t,s) for(register ll i = t;i >= s;--i) const ll N = 25; struct node { ll x; ll y; ll rnk; bool operator < (const node& u) const { return rnk < u.rnk; } bool operator == (const node& u) const { return x == u.x && y == u.y && rnk == u.rnk; } }; ll n; ll m; ll ans = INF; ll dx[5] = {0,0,1,0,-1}; ll dy[5] = {0,1,0,-1,0}; char a[N][N] = {}; set <node> s; inline ll read() { ll x = 0; ll y = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') y = -y; c = getchar(); } while(c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + (c ^ '0'); c = getchar(); } return x * y; } inline void write(ll x) { if(x < 0) { putchar('-'); write(-x); return; } if(x > 9) write(x / 10); putchar(x % 10 + '0'); } inline bool posfind(node tmp,set <node> cur) { for(register auto i : cur) if(i.x == tmp.x && i.y == tmp.y) return true; return false; } inline void bfs() { queue <pair<set <node>,ll> > q; q.push({s,0}); while(!q.empty()) { set <node> cur = q.front().first; ll stp = q.front().second; q.pop(); node head = *cur.begin(); if(a[head.x][head.y] == '@') { ans = stp; break; } ll cnt = 0; set <node> tmp; for(register auto i : cur) { cnt++; if(cnt == 1 || cnt == cur.size()) continue; tmp.insert({i.x,i.y,i.rnk + 1}); } rep(i,1,4) { ll nx = head.x + dx[i]; ll ny = head.y + dy[i]; node tmp1 = {nx,ny,1}; if(posfind(tmp1,tmp) || a[nx][ny] == '#' || nx < 1 || nx > n || ny < 1 || ny > m) continue; set <node> tmp2 = tmp; tmp2.insert(tmp1); q.push({tmp2,stp + 1}); } } } int main() { cin >> n >> m; rep(i,1,n) { rep(j,1,m) { cin >> a[i][j]; if(a[i][j] >= '0' && a[i][j] <= '9') s.insert({i,j,(ll)(a[i][j] - '0')}); } } bfs(); if(ans == INF) cout << -1; else cout << ans; return 0; }请针对以下问题优化上述代码的时间空间复杂度,极度精确保留源代码风格# CF225D Snake ## 题目描述 Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm"). The game field is represented by an $ n×m $ rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable. You control a snake, the snake consists of segments. Each segment takes up exactly one passable square of the field, but any passable square contains at most one segment. All segments are indexed by integers from $ 1 $ to $ k $ , where $ k $ is the snake's length. The $ 1 $ -th segment is the head and the $ k $ -th segment is the tail. For any $ i $ ( $ 1<=i<k $ ), segments with indexes $ i $ and $ i+1 $ are located in the adjacent squares of the field, that is, these squares share a common side. One of the passable field squares contains an apple. The snake's aim is to reach the apple and eat it (that is, to position its head in the square with the apple). The snake moves throughout the game. During one move the snake can move its head to an adjacent field square. All other segments follow the head. That is, each segment number $ i $ $ (1<i<=k) $ moves to the square that has just had segment number $ i-1 $ . Consider that all segments including the head move simultaneously (see the second test sample). If the snake's head moves to an unpassable square or to the square, occupied by its other segment, the snake dies. That's why we will consider such moves unvalid. Your task is to determine the minimum number of valid moves that the snake needs to reach the apple. ## 输入格式 The first line contains two space-separated integers $ n $ and $ m $ ( $ 1<=n,m<=15 $ ) — the number of rows and columns of the game field. Next $ n $ lines describe the game field. Each of these lines contains $ m $ characters. Character "\#" represents a wall, "." is a passable square, "@" is an apple. The snake's first segment is represented by character "1", the second one segment — by character "2" and so on. The game field description doesn't contain any characters besides "\#', ".", "@" and digits (except 0). It is guaranteed that the described field is correct. It is guaranteed that the described field contains exactly one apple and exactly one snake, the snake's length is at least 3 and at most 9. ## 输出格式 Print a single integer to the output — the minimum number of moves needed to reach the apple. If the snake can't reach the apple, print -1. ## 输入输出样例 #1 ### 输入 #1 ``` 4 5 ##... ..1#@ 432#. ...#. ``` ### 输出 #1 ``` 4 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 4 4 #78# .612 .543 ..@. ``` ### 输出 #2 ``` 6 ``` ## 输入输出样例 #3 ### 输入 #3 ``` 3 2 3@ 2# 1# ``` ### 输出 #3 ``` -1 ```
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值