给你n个数,问你从小到大排列,a[i]与a[i+1]之差-1的总和
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
int main()
{
int n;
int a[1010];
while (~scanf("%d", &n)) {
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int ans = 0;
for (int i = 1; i < n; i++) {
ans += (a[i] - a[i - 1] - 1);
}
printf("%d\n", ans);
}
return 0;
}
-
B. Buying a TV Set
给你a,b,x,y
问你n/m=x/y,n<=a,m<=b,问你n,m有几对
其实就是求出x/y的简化之后的对数
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll a,b,x,y;
while(~scanf("%lld%lld%lld%lld",&a,&b,&x,&y))
{
ll minn=0;
ll m=__gcd(x,y);
x=x/m,y=y/m;
ll p1=a/x,p2=b/y;
minn=min(p1,p2);
printf("%lld\n",minn);
}
return 0;
}
-
C. Coffee Break
给你n杯咖啡,时间为m,两倍咖啡之间至少要隔d分钟
给你每杯咖啡喝的期望时间
请输出最少要喝几天,每杯咖啡喝的时候是第几天,特判
首先开结构体,输入每杯咖啡喝的时间,那么咖啡当前id为i
开一个优先队列,把咖啡喝的时间val从小到大排
如果下一杯咖啡的val跟优先队列的头的val相差<=d,那就把咖啡放入队列,得到答案为q.size()+!
否则,就是可以把当前最小val的咖啡之后和这杯,把队首踢掉,放入这杯咖啡,得到答案为队首的答案
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int ans[maxn];
struct node
{
int id,val;
}a[maxn];
bool cmp(const node &x,const node &y)
{
return x.val<y.val;
}
bool operator<(const node &x,const node &y)
{
return x.val>y.val;
}
priority_queue <node> q;
int main()
{
int n,m,d;
scanf("%d%d%d",&n,&m,&d);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
ans[a[1].id]=1;
node x;
x.id=1,x.val=a[1].val;
q.push(x);
int res=1;
for(int i=2;i<=n;i++)
{
node y=q.top();
if(a[i].val-y.val<=d)
{
ans[a[i].id]=q.size()+1;
node e;
e.id=q.size()+1,e.val=a[i].val;
q.push(e);
}
else
{
node e;
e.id=y.id,e.val=a[i].val;
q.pop();
q.push(e);
ans[a[i].id]=y.id;
}
res=max(res,ans[a[i].id]);
}
printf("%d\n",res);
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
return 0;
}
-
D. Glider
给你n和m
给你n段区间,给你每段区间的L和R,在这段区间内飞高度不下降,否则往前飞一米,往下掉一米
m是初始高度,如果m==0了就停止飞,问你最多能飞多远
从哪里开始飞随便
双指针 l 和 r ,r先往右跑,直到不能走,在让l往又走,可以继续了,就继续,不断更新答案
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
struct node
{
int x,y;
}a[maxn];
bool cmp(const node &a,const node &b)
{
return a.x<b.x;
}
int main()
{
int n,h;
scanf("%d%d",&n,&h);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+n+1,cmp);
int l=1,r=1,tot=0,ans=0;
for(int i=1;i<=n;i++)
{
ans=max(ans,a[r].y-a[l].x+h-tot);
r++;
tot+=(a[r].x-a[r-1].y);
while(tot>=h)
{
tot-=(a[l+1].x-a[l].y);
l++;
}
}
printf("%d\n",ans);
return 0;
}