小陈的开学第九周程序

一、codeforces

1.Payment Without Change

题意

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

You have to answer q independent test cases.

输入

The first line of the input contains one integer q (1≤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≤a,b,n,S≤109) — the number of coins of value n, the number of coins of value 1, the value n and the required total value.

输出

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 1, 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).

样例输入

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

样例输出

YES
NO
NO
YES

解题思路

就是问你你能不能用已有的钱,凑到他给的数目。
刚开始用了循环,结果超时了,就只能算啦。要注意要用Long Long,用%164d读入。

程序代码

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
long long a,b,n,s;
int main(){
   
	int T;
	scanf("%d",&T);
	while(T--){
   
		scanf("%164d %164d %164d %164d",&a,&b,&n,&s);
		if(s<n){
   
			if(b<s){
   
				printf("NO\n");
			}else{
   
				printf("YES\n");
			}
		}else if(s==n){
   
			printf("YES\n");
		}else{
   
			if(s%n==0 && a*n>=s){
   
				printf("YES\n");
			}else if(s%n==0 && a*n<s){
   
				s=s-(a*n);
				if(s>b){
   
					printf("NO\n");
				}else{
   
					printf("YES\n");
				}
			}else{
   
				int t=s/n;
				if(t<=a){
   
					int t1=s-t*n;
					if(t1<=b)
						printf("YES\n");
					else
						printf("NO\n");
				}else{
   
					int t1=s-a*n;
					if(t1<=b)
						printf("YES\n");
					else
						printf("NO\n");
				}
			}
		}
	}
	return 0;
}

2.Minimize the Permutation

题意

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

You can perform at most n−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 i+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]. The minimum possible permutation we can obtain is [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];
perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation [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].
perform the first operation (swap the first and the second elements) and obtain the permutation [1,5,2,4,3];
Another example is [1,2,4,3]. The minimum possible permutation we can obtain is [1,2,3,4] by performing the third operation (swap the third and the fourth elements).

输入

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

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

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

输出

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.

样例输入

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

样例输出

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

注意

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 1 to i−1 the condition pj=qj is satisfied, and pi<qi. For example:

p=[1,3,5,2,4] is less than q=[1,3,5,4,2] (such i=4 exists, that pi<qi and for each j<i holds pj=qj),
p=[1,2] is less than q=[2,1] (such i=1 exists, that pi<qi and for each j<i holds pj=qj).

解题思路

就是每个位置只能换一次,问你能得到的最小的字典序排序。
我的方法超时啦!老师说要用贪心,但是我有点不知道怎么贪心。所以我去网上看了一下别人的代码。每次选最小的数往前走就可以了。

程序代码:(TLE)

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
int a[100+5];
int find_cnt(int t,int n){
   
	for(int i=1;i<=n;i++){
   
		if(a[i]==t)
			return i;
	}
}
int find_num(int n){
   
	for(int i=1;i<=n;i++){
   
		if(a[i]!=i)
			return i;
	}
}
bool CS(int n){
   
	int flag=true;
	for(int i=1;i<=n;i++){
   
		if(a[i]!=i)
			return false;
	}
	return true;
}
int main(){
   
	int T;
	scanf("%d",&T);
	while(T--){
   
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		if(CS(n)){
   
			for(int i=1;i<=n;i++){
   
				if(i==1)
		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值