题一 F - 寒冰王座 HDU - 1248
原题
http://acm.hdu.edu.cn/showproblem.php?pid=1248
Input
输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量.然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表死亡骑士手中钞票的面值.
注意:地精商店只有题中描述的三种道具.
Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.
Sample Input
2
900
250
Sample Output
0
50
答案
虽然本题是一道动态规划题但是还是只能用最通俗的方法——QAQ本蒟蒻不会动态规划
(做法比较巧妙,寻找150,200,350的关系(350=150+200,故350可以用150+200直接代替,另外200又可以看做150+50))
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int t,n,sum,n1;
cin>>t;
while(t--)
{
cin>>n;
sum=n;
n1=sum/150;
sum=sum%150;
if(sum/50<=n1)
{
sum=sum%50;
}
else
sum=sum-n1*50;
cout<<sum<<endl;
}
return 0;
}
题二 J - 盐水的故事 HDU - 1408
原题
挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下…,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什么时候能挂完呢?
Input
输入数据包含多个测试实例,每个实例占一行,由VUL和D组成,其中 0<D<VUL<5000。
Output
对于每组测试数据,请输出挂完盐水需要的时间,每个实例的输出占一行。
Sample Input
10 1
Sample Output
13
答案
本题考察模拟,有一个double的坑,如果用int定义容量和一滴的体积我怎么写都是WA——最后借用朋友的代码来订正吧
#include <iostream>
using namespace std;
int main()
{
double a,b,sum;
while(cin>>a>>b)
{
sum=0;
int i=1,ii;
ii=i;
while(a>0)
{
while(ii--)
{
if(a>0)
{
sum=sum+1;
a=a-b;
}
}
if(a>0)
{
sum=sum+1;
i++;
ii=i;
}
}
cout<<sum<<endl;
}
return 0;
}
题三 A - Jolly Jumpers SCU - 1997
原题
翻译:
如果连续元素之间差的绝对值取1到n-1的所有值,则n>0整数的序列称为jolly jumper。例如,
1 4 2 3
是一个快乐的跳投者,因为绝对差异分别是3、2和1。这个定义意味着一个整数的任何序列都是一个jolly jumper。您需要编写一个程序来确定一系列序列中的每一个序列是否都是一个jolly jumper。
Input
每一行输入包含一个n<3000的整数,后跟表示序列的n个整数。
Output
对于每一行输入,生成一行输出,表示“Jolly”或“notjolly”。
Sample Input
4 1 4 2 3
5 1 4 2 -1 6
Sample Output
Jolly
Not jolly
答案
#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int n, a[10000+8], buffer[10000+8];
int main()
{
while(~scanf("%d", &n))
{
int id = 0;
for(int i = 0; i<n; i++)
{
scanf("%d", &a[i]);
if(i)buffer[id++] = abs(a[i]-a[i-1]);
}
sort(buffer, buffer+id);
bool flag = 1;
for(int i = 0; i<id; i++)
{
if(buffer[i] != i+1)
{
flag = 0;
break;
}
}
if(flag)printf("Jolly\n");
else printf("Not jolly\n");
}
return 0;
}
题四 D - 二分查找(二) 计蒜客 - T1561
原题
蒜头君手上有个长度为 nn 的数组 AA。由于数组实在太大了,所以蒜头君也不知道数组里面有什么数字,所以蒜头君会经常询问在数组 AA 中,大于等于 xx 的最小值是多大?
输入格式
第一行输入两个整数 nn 和 mm,分别表示数组的长度和查询的次数。
接下来一行有 nn 个整数 aiai。
接下来 mm 行,每行有 11 个整数 xx,表示蒜头君询问的整数。
输出格式
对于每次查询,如果可以找到,输出这个整数。
否则输出 −1−1。
数据范围
1≤n,m≤105,0≤x≤1061≤n,m≤105,0≤x≤106。
Sample Input
10 5
1 1 1 2 3 5 5 7 8 9
0
1
4
9
10
Sample Output
1
1
5
9
-1
答案
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5+6;
int a[MAXN] = {
};
int lower_bound(int *A, int l, int r, int x) {
while (l <= r) {
int mid = l+((r-l)>>1);
if (A[mid]>=x) {
r=mid-1;
} else {
l=mid+1;
}
}
return l;
}
int main() {
//读入数据
int n,m;
scanf("%d%d", &n, &m);
int i;
for (i=0; i<n; i++) {
scanf("%d", &a[i]);
}
//排序
sort(a, a+n);
//查询数据
for (i=0; i<m; i++) {
int x;
scanf("%d", &x);
if (x<a[0]) {
printf("%d\n", a[0]);
} else if (x>a[n-1]) {
printf("-1\n");
} else {
int pos = lower_bound(a, 0, n-1, x);
printf("%d\n", a[pos]);
}
}
return 0;
}