欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Codeforces 1256A 1257A

程序员文章站 2022-10-28 08:58:52
题目链接:https://codeforces.com/problemset/problem/1256/A A. Payment Without Change time limit per test 1 second memory limit per test 256 megabytes input ......

题目链接:https://codeforces.com/problemset/problem/1256/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 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.

you have to answer qq independent test cases.

input

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

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.

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.

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
思路:输入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/a

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.

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.

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