Codeforces Round #598 (Div. 3) A.B題

題目鏈接:https://codeforces.com/contest/1256/problem/A

A. Payment Without Change

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have 𝑎a coins of value 𝑛n and 𝑏b coins of value 11. You always pay in exact change, so you want to know if there exist such 𝑥x and 𝑦y that if you take 𝑥x (0≤𝑥≤𝑎0≤x≤a) coins of value 𝑛n and 𝑦y (0≤𝑦≤𝑏0≤y≤b) coins of value 11, then the total value of taken coins will be 𝑆S.

You have to answer 𝑞q independent test cases.

Input

The first line of the input contains one integer 𝑞q (1≤𝑞≤1041≤q≤104) — the number of test cases. Then 𝑞q test cases follow.

The only line of the test case contains four integers 𝑎a, 𝑏b, 𝑛n and 𝑆S (1≤𝑎,𝑏,𝑛,𝑆≤1091≤a,b,n,S≤109) — the number of coins of value 𝑛n, the number of coins of value 11, the value 𝑛n and the required total value.

Output

For the 𝑖i-th test case print the answer on it — YES (without quotes) if there exist such 𝑥x and 𝑦y that if you take 𝑥x coins of value 𝑛n and 𝑦y coins of value 11, then the total value of taken coins will be 𝑆S, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example

input

Copy

4
1 2 3 4
1 2 3 6
5 2 6 27
3 3 5 18

output

Copy

YES
NO
NO
YES
#include "iostream"
#include "string.h"
#include "math.h"
#include "stdio.h"
#include "vector"
using namespace std;
 
//簡單思維題,不要被long long卡了就好
int main()
{
    int  t ; cin >> t;
    while(t --)
    {
        long long  a , b , n , s;
        cin >> a >> b >> n >> s;
        //全加上也不夠
        if(a*n + b < s)
        {
            cout << "NO" << endl;
        }
        else
        {
            if(s % n <= b)
                cout << "YES" << endl;
            else 
                cout << "NO" << endl;
        }
 
    }
    return 0;
}

B. Minimize the Permutation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a permutation of length 𝑛n. Recall that the permutation is an array consisting of 𝑛n distinct integers from 11 to 𝑛n in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (𝑛=3n=3 but there is 44 in the array).

You can perform at most 𝑛−1n−1 operations with the given permutation (it is possible that you don't perform any operations at all). The 𝑖i-th operation allows you to swap elements of the given permutation on positions 𝑖i and 𝑖+1i+1. Each operation can be performed at most once. The operations can be performed in arbitrary order.

Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order.

You can see the definition of the lexicographical order in the notes section.

You have to answer 𝑞q independent test cases.

For example, let's consider the permutation [5,4,1,3,2][5,4,1,3,2]. The minimum possible permutation we can obtain is [1,5,2,4,3][1,5,2,4,3] and we can do it in the following way:

  1. perform the second operation (swap the second and the third elements) and obtain the permutation [5,1,4,3,2][5,1,4,3,2];
  2. perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation [5,1,4,2,3][5,1,4,2,3];
  3. perform the third operation (swap the third and the fourth elements) and obtain the permutation [5,1,2,4,3][5,1,2,4,3].
  4. perform the first operation (swap the first and the second elements) and obtain the permutation [1,5,2,4,3][1,5,2,4,3];

Another example is [1,2,4,3][1,2,4,3]. The minimum possible permutation we can obtain is [1,2,3,4][1,2,3,4] by performing the third operation (swap the third and the fourth elements).

Input

The first line of the input contains one integer 𝑞q (1≤𝑞≤1001≤q≤100) — the number of test cases. Then 𝑞q test cases follow.

The first line of the test case contains one integer 𝑛n (1≤𝑛≤1001≤n≤100) — the number of elements in the permutation.

The second line of the test case contains 𝑛n distinct integers from 11 to 𝑛n — the given permutation.

Output

For each test case, print the answer on it — the lexicograhically minimum possible permutation obtained by performing some of the given operations in some order.

Example

input

Copy

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

output

Copy

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

Note

Recall that the permutation 𝑝p of length 𝑛n is lexicographically less than the permutation 𝑞q of length 𝑛n if there is such index 𝑖≤𝑛i≤n that for all 𝑗j from 11 to 𝑖−1i−1 the condition 𝑝𝑗=𝑞𝑗pj=qj is satisfied, and 𝑝𝑖<𝑞𝑖pi<qi. For example:

  • 𝑝=[1,3,5,2,4]p=[1,3,5,2,4] is less than 𝑞=[1,3,5,4,2]q=[1,3,5,4,2] (such 𝑖=4i=4 exists, that 𝑝𝑖<𝑞𝑖pi<qi and for each 𝑗<𝑖j<i holds 𝑝𝑗=𝑞𝑗pj=qj),
  • 𝑝=[1,2]p=[1,2] is less than 𝑞=[2,1]q=[2,1] (such 𝑖=1i=1 exists, that 𝑝𝑖<𝑞𝑖pi<qi and for each 𝑗<𝑖j<i holds 𝑝𝑗=𝑞𝑗pj=qj).
#include "iostream"
#include "string.h"
#include "math.h"
#include "stdio.h"
#include "vector"
using namespace std;

//greedy
//會錯題意了…題目中說的i操作只能進行一次,i操作指的是"i和i+1交換"這一操作
//讀題 讀題 讀題
int main()
{

    int arr[105];
    int mark[105];
    int T; cin >> T;
    while(T --)
    {
        int step = 0;
        memset(mark , 0 , sizeof(mark));
        memset(arr , 0 , sizeof(arr));
        //大體思路,找一個最小的且前邊能移動 或 step到了n-1結束
        //處理完了打標記爲1,繼續處理
        mark[0] = 1;
        //輸入
        int n ; scanf("%d" , &n);
        for(int i = 1 ; i <= n ; i ++)
            scanf("%d" , &arr[i]);


        //fl=1表示步數沒用完就已經輸出了。0則用完了退出
        int fl = 0;
        while(1)
        {
            //步數用完,退出
            if(step == n-1)
            {
                break;
            }

            //已經找出了自小字典序序列
            int fd = 0;
            for(int i = 1 ; i <= n ; i++)
                if(arr[i] != i)
                    fd = 1;
            if(fd == 0)
                break;

            //所有位置已經不能移動
            int fa = 0;
            for(int i = 1 ; i <= n-1 ; i++)
                if(mark[i] == 0)
                    fa = 1;
            if(fa == 0)
                break;


            //找一個最小且i-1處可以移動的數字,記錄位置和值
            int minn = INT_MAX;
            int p = 0 ;
            for(int i = 1 ; i <= n ; i++)
            {
                if(arr[i] < minn && mark[i-1] == 0)
                {
                    p = i;
                    minn = arr[i];
                }
            }
            //對這個數進行前移處理,每移動一個打標記
            while(1)
            {
                if(step == n-1)
                {
                    for(int i = 1 ; i <= n ; i ++)
                        cout << arr[i] << " ";
                    cout << endl;
                    fl = 1;
                    break;
                }
                if(arr[p-1] < arr[p])
                {
                    mark[p-1] = 1;
                    break;
                }
                //若p-1處>p處且mark[p-1]==0 可以移動
                if(arr[p-1] > arr[p] && mark[p-1] == 0)
                {
                    //進行交換
                    int tmp = arr[p];
                    arr[p] = arr[p-1];
                    arr[p-1] = tmp;
                    //將"第i個操作"中第i個位置打標記
                    mark[p-1] = 1;
                    step ++;
//                    cout << step << "!" << endl;
                }

            }

        }

        if(fl == 0)
        {
            for(int i = 1 ; i <= n ; i ++)
                cout << arr[i] << " ";
            cout << endl;
        }

    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章