题目链接: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:
- 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];
- 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];
- 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].
- 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;
}