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

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

一种排序

时间限制: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 <iostream> #include <vector> #include <queue> #include <algorithm> #include <climits> using namespace std; // 定义迷宫中的单元格 struct Cell { int x, y, steps, value; Cell(int x, int y, int steps, int value) : x(x), y(y), steps(steps), value(value) {} }; // 用于查询的结构体 struct QueryResult { int steps; int maxValue; QueryResult(int steps, int maxValue) : steps(steps), maxValue(maxValue) {} }; // 比较函数,用于排序 bool compareBySteps(const QueryResult& a, const QueryResult& b) { return a.steps < b.steps; } // BFS计算最少步数 vector<vector<int>> bfs(const vector<vector<int>>& maze, int startX, int startY) { int rows = maze.size(); int cols = maze[0].size(); vector<vector<int>> steps(rows, vector<int>(cols, -1)); queue<pair<int, int>> q; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; steps[startX][startY] = 0; q.push({startX, startY}); while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && maze[nx][ny] != 0 && steps[nx][ny] == -1) { steps[nx][ny] = steps[x][y] + 1; q.push({nx, ny}); } } } return steps; } // 构建查询用的数据结构 vector<QueryResult> buildQueryStructure(const vector<vector<int>>& maze, const vector<vector<int>>& steps) { int rows = maze.size(); int cols = maze[0].size(); vector<QueryResult> queryData; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (steps[i][j] != -1) { queryData.push_back(QueryResult(steps[i][j], maze[i][j])); } } } sort(queryData.begin(), queryData.end(), compareBySteps); return queryData; } // 处理单个查询 int queryMaxValue(const vector<QueryResult>& queryData, int maxSteps) { int left = 0; int right = queryData.size() - 1; int result = INT_MIN; while (left <= right) { int mid = left + (right - left) / 2; if (queryData[mid].steps <= maxSteps) { result = max(result, queryData[mid].maxValue); left = mid + 1; } else { right = mid - 1; } } return result; } int main() { // 示例迷宫 vector<vector<int>> maze = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int startX = 0; int startY = 0; // 计算最少步数 vector<vector<int>> steps = bfs(maze, startX, startY); // 构建查询数据 vector<QueryResult> queryData = buildQueryStructure(maze, steps); // 处理多个查询 vector<int> queries = {1, 2, 3}; for (int query : queries) { int maxValue = queryMaxValue(queryData, query); cout << "Max value within " << query << " steps: " << maxValue << endl; } return 0; } 请化简一下上面代码码风,把类似变量名改为a,b,c,sum,ant
最新发布
08-17
#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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值