Codeforces 1256A 1257A

題目連接:https://codeforces.com/problemset/problem/1256/Aios

A. Payment Without Change
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have aa coins of value nn and bb coins of value 11. You always pay in exact change, so you want to know if there exist such xx and yy that if you take xx (0xa0≤x≤a) coins of value nn and yy (0yb0≤y≤b) coins of value 11, then the total value of taken coins will be SS.ide

You have to answer qq independent test cases.ui

Input

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

The only line of the test case contains four integers aa, bb, nn and SS (1a,b,n,S1091≤a,b,n,S≤109) — the number of coins of value nn, the number of coins of value 11, the value nn and the required total value.spa

Output

For the ii-th test case print the answer on it — YES (without quotes) if there exist such xx and yy that if you take xx coins of value nn and yy coins of value 11, then the total value of taken coins will be SS, and NO otherwise.code

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

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
思路:輸入a,b,n,s,每一個表明的意思爲:a個含有價值n的硬幣、b個含有價值1的硬幣、價值爲n的硬幣、由這些硬幣組成的目標數。先判斷b個爲1的硬幣是否能直接達到s,能的話則直接輸出,不能的話則進行下一步。
先判斷價值爲n的硬幣最多能取多少個,即s對n取整,再將s減去s/n,再判斷剩下的能不能由b個價值爲1的硬幣組成,能的話則知足,不能的話則不知足。
AC代碼
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int q;
    cin >> q;
    while(q--)
    {
        int a = 0,b = 0,sum = 0,n = 0,s = 0,temp = 0,min1 = 0;
        cin >> a >> b >> n >> s;
        if(b >= s)
        {
            cout << "YES" << endl;
            continue;
        }
        temp = s / n;
        min1 = min(a,temp);
        sum = s - min1 * n;
        if(b >= sum)
        {
            cout << "YES" << endl;
            continue;
        }
        else
        {
            cout << "NO" << endl;
            continue;
        }
    }    
    return 0;
}

題目連接:https://codeforces.com/contest/1257/problem/Ablog

A. Two Rival Students
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are the gym teacher in the school.ip

There are nn students in the row. And there are two rivalling students among them. The first one is in position aa, the second in position bb. Positions are numbered from 11 to nn from left to right.ci

Since they are rivals, you want to maximize the distance between them. If students are in positions pp and ss respectively, then distance between them is |ps||p−s|.

You can do the following operation at most xx times: choose two adjacent (neighbouring) students and swap them.

Calculate the maximum distance between two rivalling students after at most xx swaps.

Input

The first line contains one integer tt (1t1001≤t≤100) — the number of test cases.

The only line of each test case contains four integers nn, xx, aa and bb (2n1002≤n≤100, 0x1000≤x≤100, 1a,bn1≤a,b≤n, aba≠b) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.

Output

For each test case print one integer — the maximum distance between two rivaling students which you can obtain.

Example
input
Copy
3
5 1 3 2
100 33 100 1
6 0 2 3
output
Copy
2
99
1
Note

In the first test case you can swap students in positions 33 and 44. And then the distance between the rivals is equal to |42|=2|4−2|=2.

In the second test case you don't have to swap students.

In the third test case you can't swap students.

 思路:狀況1:兩我的的距離加上可移動的次數都小於等於最遠距離的話,直接輸出。

狀況2:兩我的的距離加上可移動距離大於最遠距離,則說明可移動次數x足夠用了。再來判斷,要使兩我的達到最遠距離,與最遠距離還差多少,若是可移動次數x大於差值,則兩人的距離可達最大,不然兩我的的最遠距離爲原先的距離加上可移動距離x,即爲答案

AC代碼

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int juli,n,x,a,b;
        cin >> n >> x >> a >> b;
        juli = abs(a - b);
        if(juli + x <= n - 1)//移動後的距離 小於等於 最遠距離 
        {
            cout << x + juli << endl;//直接輸出 
            continue;
        }
        if(n - juli <= x)//n-juli爲 離最遠距離差多少  
        {    
            cout << n - 1 << endl;
            continue;
        }
        else
        {
            cout << juli + x << endl;
            continue;
        }
    }
    return 0;
}
//100 25 70 10
相關文章
相關標籤/搜索