暑假集训日记--8.8--搜索+练习赛

今天又一场练习赛。比之前强一点,能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.

Input

First line contains two space-separated integers nm (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 lirixi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.

Output

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.

Example
Input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Output
Yes
No
Yes
Yes
No
Input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Output
Yes
No
Yes
No
Yes
Note

Explanation of first test case: 

  1. [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". 
  2. [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No". 
  3. [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes". 
  4. [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes". 
  5. [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".
题目的意思就是先给一个序列,然后再给一个区间l,r,对这段区间里的数进行排序,看第x个数有没有改变。

开始用排序写,

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;

}

另外需要写的就是今上午做的一道搜索题。题目如下:

You have just been put in charge of developing a new shredder for the Shredding Company Although a "normal" shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, this new shredder needs to have the following unusual basic characteristics. 

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. 
 
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. 

 

Input
The input consists of several test cases, each on one line, as follows : <br> <br>tl num1 <br>t2 num2 <br>... <br>tn numn <br>0 0 <br> <br>Each test case consists of the following two positive integers, which are separated by one space : (1) the first integer (ti above) is the target number, (2) the second integer (numi above) is the number that is on the paper to be shredded. <br> <br>Neither integers may have a 0 as the first digit, e.g., 123 is allowed but 0123 is not. You may assume that both integers are at most 6 digits in length. A line consisting of two zeros signals the end of the input. <br> <br>
 

Output
For each test case in the input, the corresponding output takes one of the following three types : <br> <br>sum part1 part2 ... <br>rejected <br>error <br> <br>In the first type, partj and sum have the following meaning : <br> <br>1.Each partj is a number on one piece of shredded paper. The order of partj corresponds to the order of the original digits on the sheet of paper. <br> <br>2.sum is the sum of the numbers after being shredded, i.e., sum = part1 + part2 +... <br> <br>Each number should be separated by one space. <br>The message error is printed if it is not possible to make any combination, and rejected if there is <br>more than one possible combination. <br>No extra characters including spaces are allowed at the beginning of each line, nor at the end of each line. <br>
 

Sample Input
   
   
50 12346 376 144139 927438 927438 18 3312 9 3142 25 1299 111 33333 103 862150 6 1104 0 0
 

Sample Output
   
   
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;

}

*/


剩下的题感觉不需要总结了。

还在看优先队列……看完再总结……

还要加油

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值