2021桂林 D. Assumption is All You Need(铜牌题)

原题链接:https://codeforces.com/gym/103409/problem/D

数组开小了,导致我多试了好多次,还去看了别人的题解。最后发现我原来的做法是可以AC的,别人的基本是如果是从小的开始遍历,就每一步替换最小的可以替换的,如果是从大的开始,每步替换最大可以替换的,和我不太一样。

我是从大到小开始遍历,并不要求做完一轮swap操作后归为目标位置,而是要让操作完的位置小于等于目标位置,并且该轮swap操作中不能让更大数的位置大于其目标位置,否则之前的做法就变得无意义了。因为后面要遍历的数都是更小的,如果该轮swap操作完成,每次都满足当前位置小于等于目标位置,则最后有所有数的当前位置都小于等于目标位置。那么只能是全等于的情况才可能,这样就达到了题目要求。

#include <bits/stdc++.h>
using namespace std;

const int N = 3e3 + 5;

int t, n, a[N], b[N], aim[N], now[N], ans[N * N][2]; 

int main(void) {
//	freopen("in.txt", "r", stdin);
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			now[a[i]] = i;
		}
		for (int i = 1; i <= n; i++) {
			scanf("%d", &b[i]);
			aim[b[i]] = i;
		}
		bool ok = true;
		int k = 0;
		for (int i = n; i >= 1; i--) {
			while (now[i] > aim[i]) {
				int pos = now[i] - 1;
				while (pos >= 1 && (a[pos] < i || aim[a[pos]] < now[i])) pos--;
				if (pos >= 1) {
					k++;
					ans[k][0] = pos;
					ans[k][1] = now[i];
					a[now[i]] = a[pos];
					now[a[pos]] = now[i];
					now[i] = pos;
					a[pos] = i;
 				}
 				else break;
			}
			if (now[i] > aim[i]) {
				ok = false;
				break;
			}
		}
		for (int i = 1; i <= n; i++) 
				if (a[i] != b[i]) {
					ok = false;
					break;
				}
		if (!ok) puts("-1");
		else {
			printf("%d\n", k);
			for (int i = 1; i <= k; i++)
				printf("%d %d\n", ans[i][0], ans[i][1]);
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Write a program to 1.Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2.Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3.Read logical addresses one by one from la.txt. 4.For each logical address, a)if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b)if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Assumption: 1.Assume the file la.txt includes the sequence of generated addresses from CPU. 2.Use a part of memory as backing store that store data for a process. 3.The backing store size is 128 bytes 4.The size of process p is 128 bytes. 5.The contents of p is included in a file pdata.bin which is a binary file. 6.Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7.The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. At beginning, no page table is available for process p.
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JILIN.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值