是不是手机写博客不用审核,以后就用手机写了哈哈
A. Payment Without Change
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
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.
Input
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.
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 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).
Example
inputCopy
4
1 2 3 4
1 2 3 6
5 2 6 27
3 3 5 18
outputCopy
YES
NO
NO
YES
排除法,给定四个数,凑出最后一个数。
B题改进代码
#include<bits/stdc++.h>
using namespace std;
int t;
int n, a[110], b[110];
int main(){
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
b[a[i]] = i;
}
int tmp = 1;
for(int i = 1; i <= n; i++){
for(int j = b[i]-1; j >= tmp; j--){
if(a[j] > a[j+1])swap(a[j], a[j+1]);
}
tmp = max(tmp, b[i]);
}
for(int i = 1; i <= n; i++)
cout << a[i] << (i==n?"\n":" ");
}
return 0;
}
c题
长度为n的河流,你有m的板子,一次最多跳d。
求贪心策略
这是网上找的代码,自己的一直不行。关键是中间的条件,自己的木板也是分了三个条件可是总是结束进程。
,
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,d;
int a[1200],s[1200],b[1200];
int main()
{
while(~scanf("%d %d %d",&n,&m,&d))
{
memset(s,0,sizeof(s));//后缀和
memset(a,0,sizeof(a));//输入的每个木板的长度
memset(b,0,sizeof(b));//存储木板摆放位置即最后结果
for(int i=1; i<=m; i++)
scanf("%d",&a[i]);
for(int i=m; i>=1; i--)
s[i]=s[i+1]+a[i];
int j=0,k=0,flag=0;//j为第几个木板,k为所能到达的地方,flag用来标记能否达到n+1这个位置
while(1)
{
j++;
k+=d;
if(!a[j]&&k<n+1)//如果没有木板并且达不到n+1的位置,直接跳出循环
break;
if(k+s[j]>=n+1)//如果达到的位置与后缀和大于等于n+1证明可以到达,赋值之后就跳出循环
{
flag=1;//标记可到达
k=n+1-s[j];//开始从这个坐标给予赋值(赋予的值就是用到的木板是第几个)
for(int i=j; i<=m; i++)
{
while(a[i]--)
b[k++]=i;
}
break;
}
while(1)//这里就是对于木板放的位置所赋予的值。
{
b[k]=j;
a[j]--;
if(a[j]==0)
break;
k++;
}
}
if(flag==1)
{
printf("YES\n");
for(int h=1; h<=n; h++)
printf("%d%c",b[h],h==n?'\n':' ');
}
else
printf("NO\n");
}
return 0;
}
下面只看了题解也…
D 题交换任意相邻位置,是01的字符串最小。
En个数,分成若干组且每组至少有3个数,都有最大最小值的差,使所有组的最大最小差的和最小