北林oj-算法设计与分析-Don‘t touch my cake(题意+代码)

描述

A boy bought N different cakes someday. They were arranged randomly in a line given by permutation P=[p1, p1, ..., pn], where pi denotes the unique label of a cake. To play a game, his mother sorted some subsegment of permutation P from position L to position R, that is, [L, R].

After every such sorting, the boy would be wondering if the X-th cake is still in its original position. In other words, was px changed?

After every such sorting, cakes were returned to their initial positions, so you can assume that all the sortings are independent of each other.

输入

The first line contains two space-separated integers N, M (1 ≤ N, M ≤ 1000) — the length of permutation and the number of sortings.

The second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that all elements in the permutation are different.

Each of the next M lines contains three space-separated integers Li, Ri, Xi (1 ≤ Li ≤ Xi ≤ Ri ≤ n) — the left and right borders of the subsegment and the position which is interesting to the boy in the i-th sorting.

The input is terminated at the end of the file (EOF).

输出

For each sorting, print "Yes" if the cake at position X was not changed, or "No" otherwise.

输入样例 1 

5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3

输出样例 1

Yes
No
Yes
Yes
No

输入样例 2 

6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3

输出样例 2

Yes
No
Yes
No
Yes

提示

Explanation of first test case:

  1. [1, 2, 3, 4, 5] — permutation after sorting, the 3-rd element is not changed, so answer is "Yes".
  2. [3, 4, 5, 2, 1] — permutation after sorting, the 1-st element is changed, so answer is "No".
  3. [5, 2, 3, 4, 1] — permutation after sorting, the 3-rd element is not changed, so answer is "Yes".
  4. [5, 4, 3, 2, 1] — permutation after sorting, the 4-th element is not changed, so answer is "Yes".
  5. [5, 1, 2, 3, 4] — permutation after sorting, the 3-rd element is changed, so answer is "No".

简化版题意:

有N个不同的蛋糕排成一行,每个蛋糕上有记录它们位置的标签(从1开始)。把闭区间[L,R]内的蛋糕进行升序排序,问每次这样的操作后,第X个蛋糕是否仍在原来的位置。

注意:所有操作都是从初始位置独立地进行每一次排序。

输入数据:

第一行输入两个空格分隔的整数N,M,代表蛋糕序列长度和排序的次数。

第二行就是输入不同标签的蛋糕,标签无重复。

以下几行是执行排序的操作,看提示应该能懂

输入在文件(EOF)末尾终止。———》while(cin>>)连续输入的格式

输出

对于每一次排序,如果X位置的蛋糕没有改变,则打印“是”,否则打印“否”。

思路就是先记录下旧位置的数,然后把相应位置的数拿出一个单独数组里,排完之后再放回原数组对应位置,把新位置的数和进行比对。注意规范输出格式。。。

代码如下:

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

void Ans(int sortnum, vector<int> v,int l, int r,int pos)
{
		int data = v[pos - 1];//先记录下来原始数据位置
		int lft = l - 1, rgt = r - 1;//换算成数组从0开始计数的习惯
		int sz = rgt - lft + 1;//辅助数组大小
		vector<int> help(sz);
		for (int i = 0; i < sz; i++)
			help[i] = v[lft++];
		sort(help.begin(), help.end());//排序

		int k = 0;
		int lv = l - 1;

		for (int i = 0; i < sz; i++)//把辅助数组的值更新到原数组到对应位置上去
			v[lv++] = help[k++];//i控制的是次数

		if (data == v[pos - 1])//比对
			cout << "Yes" << '\n';
		else
			cout << "No" << '\n';

}
int main() 
{
	int cakenum, sortnum;
	while (cin >> cakenum >> sortnum)
	{
		vector<int> v(cakenum);
		for (int i = 0; i < cakenum; i++)
			cin >> v[i];
        
		for (int i = 0; i < sortnum; i++)
		{
			int l, r, pos;
			cin >> l >> r >> pos;
            Ans(sortnum, v, l, r, pos);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值