今天又一场练习赛。比之前强一点,能A两个题了……尽管两个题都是水题……
但是很不幸……第二道水题又wa了五发……还是……没想好就写代码。下面贴题目:
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.
Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position rinclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.
Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.
Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.
5 5 5 4 3 2 1 1 5 3 1 3 1 2 4 3 4 4 4 2 5 3
Yes No Yes Yes No
6 5 1 4 3 2 5 6 2 4 3 1 6 2 4 5 4 1 3 3 2 6 3
Yes No Yes No Yes
Explanation of first test case:
- [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
- [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
- [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
- [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
- [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
开始用排序写,
T到崩溃……
然后改改区间还是T。这道题用排序就T。
于是……想了好久,发现可以在l到r的区间内,找比当前x的值大的数的个数,如果和r-x相等,那么就肯定还是原来位置的数,
比如说1 3 2 4 5,对2 4 5进行排序,这三个数从小到大排4的位置是不变的。
所以源代码如下:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
int num[11000],num1[11000];
int i,j,l,r,x;
int main()
{
scanf("%d %d", &n, &m);
for (i = 1; i <= n; i++)
{
scanf("%d",&num[i]);
}
for (i = 1; i <= m; i++)
{
scanf("%d%d%d",&l,&r,&x);
int sum = 0;
for (j = l; j <= r; j++)
{
if (num[j] > num[x])
sum++;
}
if (sum == (r - x))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
另外需要写的就是今上午做的一道搜索题。题目如下:1.The shredder takes as input a target number and a sheet of paper with a number written on it.
2.It shreds (or cuts) the sheet into pieces each of which has one or more digits on it.
3.The sum of the numbers written on each piece is the closest possible number to the target number, without going over it.
For example, suppose that the target number is 50, and the sheet of paper has the number 12346. The shredder would cut the sheet into four pieces, where one piece has 1, another has 2, the third has 34, and the fourth has 6. This is because their sum 43 (= 1 + 2 + 34 + 6) is closest to the target number 50 of all possible combinations without going over 50. For example, a combination where the pieces are 1, 23, 4, and 6 is not valid, because the sum of this combination 34 (= 1 + 23 + 4 + 6) is less than the above combination's 43. The combination of 12, 34, and 6 is not valid either, because the sum 52 (= 12 + 34 + 6) is greater than the target number of 50.
![](https://i-blog.csdnimg.cn/blog_migrate/945777e48ec1ab43c93d4ce8bb1def99.jpeg)
Figure 1. Shredding a sheet of paper having the number 12346 when the target number is 50
There are also three special rules :
1.If the target number is the same as the number on the sheet of paper, then the paper is not cut.
For example, if the target number is 100 and the number on the sheet of paper is also 100, then
the paper is not cut.
2.If it is not possible to make any combination whose sum is less than or equal to the target number, then error is printed on a display. For example, if the target number is 1 and the number on the sheet of paper is 123, it is not possible to make any valid combination, as the combination with the smallest possible sum is 1, 2, 3. The sum for this combination is 6, which is greater than the target number, and thus error is printed.
3.If there is more than one possible combination where the sum is closest to the target number without going over it, then rejected is printed on a display. For example, if the target number is 15, and the number on the sheet of paper is 111, then there are two possible combinations with the highest possible sum of 12: (a) 1 and 11 and (b) 11 and 1; thus rejected is printed. In order to develop such a shredder, you have decided to first make a simple program that would simulate the above characteristics and rules. Given two numbers, where the first is the target number and the second is the number on the sheet of paper to be shredded, you need to figure out how the shredder should "cut up" the second number.
50 12346 376 144139 927438 927438 18 3312 9 3142 25 1299 111 33333 103 862150 6 1104 0 0
43 1 2 34 6 283 144 139 927438 927438 18 3 3 12 error 21 1 2 9 9 rejected 103 86 2 15 0 rejected
这是一道DFS的题目。
思路见代码:
/*
先将数字分开用数组存储。
DFS:从第一个数开始依次结合,如果最后得到的数大于要求的数,则返回,直到小于,sum+= 该数。
再从该数开始往后依次结合。
visit用于记录断开的点。
*//*
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <vector>
using namespace std;
int bits[7],res[7],visit[7];
int num,t;
int flag,Max;
int i;
void DFS(int sum,int a,int step)
{
if (sum > t)
return ;
if (step == i)
{
if (sum <= t)
{
if (sum == Max)
flag = 2;
else if (sum > Max)
{
Max = sum;
flag = 1;
for (int r = 0; r < i; r++)
{
res[r] = visit[r];
}
}
}
return ;
}
int ans = a * 10 + bits[step];
if (step < i - 1)
{
visit[step] = 0;
DFS(sum,ans,step+1);
}
visit[step] = 1;
sum += ans;
DFS(sum,0,step +1);
}
int main()
{
while (cin >> t >> num)
{
if (t == 0 && num == 0)
break;
flag = 0;
Max = 0;
i = 0;
while (num)
{
bits[i] = num % 10;
i++;
num = num / 10;
}
reverse(bits,bits + i);
DFS(0,0,0);
// for (int j = 0; j < i; j++)
// cout << res[j]<< " ";
if (flag == 0)
cout << "error" << endl;
else if (flag == 1)
{
cout << Max << " ";
for (int j = 0; j < i; j++)
{
if (res[j-1] == 1 && j != 0)
cout << " ";
cout << bits[j];
}
cout << endl;
}
else if (flag == 2)
cout << "rejected" << endl;
}
return 0;
}
*/
还在看优先队列……看完再总结……
还要加油