【Codeforces Round #509 (Div. 2)】A B C D E

71 篇文章 0 订阅

A
题意 给你n个数 里面有最大值和最小值
问你最小值和最大值之间差了几个数 我们只要 m a x x − m i n n + 1 − n maxx-minn+1-n maxxminn+1n就是答案

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
int arr[1025];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n ,minn = 1e9+5,maxx=-1;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),maxx=max(maxx,arr[i]),minn = min(minn,arr[i]);
    printf("%d\n",maxx-minn-n+1);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


B
题意 给你 a b x y a b x y abxy
问你有多少 w &lt; = a h &lt; = b w&lt;=a h&lt;=b w<=ah<=b 满足 w / h = x / y w/h = x/y w/h=x/y
做法 可以直接gcd处理掉 x y x y xy 以后取 m i n ( a / x , b / y ) min(a/x,b/y) min(a/x,b/y)
也可以写个二分 算是检验一下二分就写了个二分(然后竟然爆ll 写炸了 我真是个笨b

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    ll a,b,x,y;
    scanf("%lld%lld%lld%lld",&a,&b,&x,&y);
    ll ck = gcd(x,y);
    x/=ck;
    y/=ck;
    ll tmp = b/y;
    ll l = 0,r = tmp;
    while(l<=r)
    {
        ll mid = (l+r)>>1;
        if(mid<=a/x) l = mid + 1;
        else r = mid - 1;
    }
    printf("%lld\n",r);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


C
题意 有n杯咖啡 每天有m个小时工作时间 喝完一杯咖啡必须等d时间才能喝下一杯
问你怎么安排使得喝完所有咖啡的天数最小
做法 直接上贪心 set上二分即可 但是本着练练自己线段树的水平写了个线段树
(忘记<<2 这次<<1无限RE 调到思考人生
线段树主要是单点修改 update
区间查询最大值 query
查询大于值等于x的最左边的下标 query_
我们只要对咖啡时间排个序 从最小的咖啡时间开始打上天数cnt 然后找最小的大于他时间+d的即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 200025;
struct node
{
    ll a;
    int id;
    bool operator< (const node other) const
    {
        return a < other.a;
    }
}arr[MAX_N];
int ans[MAX_N],cnt;
ll maxx[MAX_N<<2];
int n,m,d;
void up(int rt)
{
    maxx[rt] = max(maxx[rt<<1],maxx[rt<<1|1]);
}
void build(int rt,int l,int r)
{
    if(l==r)
    {
        maxx[rt] = arr[l].a;
        return ;
    }
    int mid = (l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    up(rt);
}
void update(int rt,int l,int r,int x)
{
    if(l==r)
    {
        maxx[rt] = 0;
        return;
    }
    int mid = (l+r)>>1;
    if(x<=mid) update(rt<<1,l,mid,x);
    else update(rt<<1|1,mid+1,r,x);
    up(rt);
}
ll query(int rt,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)
        return maxx[rt];
    int mid = (l+r)>>1;
    ll maxx1 = 0,maxx2 = 0;
    if(x<=mid) maxx1 = query(rt<<1,l,mid,x,y);
    if(mid<y) maxx2 = query(rt<<1|1,mid+1,r,x,y);
    return max(maxx1,maxx2);
}
int query_(int rt,int l,int r,ll x)
{
    if(l==r)
    {
        return l;
    }
    int mid = (l+r)>>1;
    if(maxx[rt<<1]>=x) return query_(rt<<1,l,mid,x);
    else return query_(rt<<1|1,mid+1,r,x);
}
void solve(int i)
{
    if(i>n) return;
    ans[arr[i].id] = cnt;
    ll tmp_ = arr[i].a;
    update(1,1,n,i);
    ll tmp = query(1,1,n,i,n);
    if(tmp>tmp_ + d)
        {
            int xb = query_(1,1,n,tmp_+d+1);
            ans[arr[xb].id] = cnt;
            solve(xb);
        }
    else  return;
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    cnt = 1;
    scanf("%d%d%d",&n,&m,&d);
    memset(ans,-1,sizeof(ans));
    for(int i = 1;i<=n;++i) scanf("%lld",&arr[i].a),arr[i].id = i;
    sort(arr+1,arr+1+n);
    build(1,1,n);
    for(int i = 1;i<=n;++i)
    {
        if(ans[arr[i].id]!=-1) continue;
        else
        {
            ll tmp = query(1,1,n,i,n);
            if(tmp>arr[i].a+d)
                {
                    solve(i);
                }
            else ans[arr[i].id] = cnt,update(1,1,n,i);
            cnt++;
        }
    }
    printf("%d\n",cnt-1);
    for(int i = 1;i<=n;++i)
        i==n?printf("%d\n",ans[i]):printf("%d ",ans[i]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


D
题意 飞机飞h秒掉到地上 在经过n个段的时候飞机不会下降
问你飞机最多能飞多少秒
做法 首先飞机肯定是要从这些段的左端点开始是最优的
那么我们枚举左端点 用双指针的方法不断的去检验最大值
tmp1代表当前枚举的节点 tmp2代表能向右最远的节点

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 200025;
struct node
{
    ll l,r;
}arr[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,h;
    scanf("%d%d",&n,&h);
    ll ans = h;
    for(int i = 1;i<=n;++i) scanf("%lld%lld",&arr[i].l,&arr[i].r);
    ll tmp1 = 1,tmp2 = 1;
    ll l = arr[1].l,r = arr[1].r+h;
    while(tmp1<=n)
    {
        while(tmp2+1<=n&&arr[tmp2+1].l<r)
        {
            tmp2++;
            r+=arr[tmp2].r-arr[tmp2].l;
        }
        ans = max(ans,r-l);
        tmp1++;
        l = arr[tmp1].l;
        r+=arr[tmp1].l - arr[tmp1-1].r;
    }
    printf("%lld\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


E
题意 构造一棵树 使得删掉每个边 两个联通快的最大值是题目输入的那样
我们知道 你无论如何删 n肯定是最大值 所以我们在输入的时候就可以判断是否有n 如果没n那么肯定就是 N O NO NO的情况
我们用n当根
然后我们要知道 实际上除了最大值每个数x出现了几次 就是类似于x与最大值n之间那个链有几个比他小的元素 因为比x小 所以你切了以后一边就是 那个数 另一边是最大值 哪怕最大值和几个比x大的元素有连边 但是取了max还是最大值 所以不影响
图大概是这样的
我是图

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
const int MAX_N = 1025;
int cnt[MAX_N];
bool vis[MAX_N];
queue<int > q;
vector<pll> ans;
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,a,b;
    bool flag = true;
    scanf("%d",&n);
    for(int i = 1;i<n;++i)
    {
        scanf("%d%d",&a,&b);
        if(a>b) swap(a,b);
        if(b!=n) flag = false;
        cnt[a]++;
        vis[a] = true;
    }
    for(int i = 1;i<n;++i)
        if(!vis[i]) q.push(i);
    if(!flag)
    {
        printf("NO\n");
        return 0;
    }
    for(int i = 1;i<n;++i)
    {
        if(cnt[i])
        {
        if(cnt[i]==1) ans.push_back(pll(i,n));
        else
        {
            int last = i;
            while(cnt[i]!=1)
            {
            int now = q.front();
            q.pop();
            if(now>i)
            {
                flag = false;
                break;
            }
            else
            {
                ans.push_back(pll(last,now));
                last = now;
                cnt[i]--;
            }
            }
            if(flag) ans.push_back(pll(last,n));
        }
        }
    }
    if(!flag)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n");
    int sz = ans.size();
    for(int i = 0;i<sz;++i)
        printf("%d %d\n",ans[i].first,ans[i].second);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值