zoukankan      html  css  js  c++  java
  • luoguP5444 [APIO2019]奇怪装置 数论+贪心

    本题的关键在于求循环节.  

    如果能想到从循环节入手的话这道题还是比较友好的.    

    code: 

    #include <cstdio> 
    #include <cstring> 
    #include <algorithm>  
    #include <cmath>   
    #define ll long long  
    #define N 1000009 
    #define setIO(s) freopen(s".in","r",stdin) 
    using namespace std;    
    int n;   
    ll A,B,G,L[N],R[N];  
    ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } 
    struct line 
    {
        ll l,r;      
        line(ll l=0,ll r=0):l(l),r(r){}  
        bool operator<(const line &b) const  { return l<b.l; }
    }s[N<<1];   
    int main() 
    { 
        // setIO("input");  
        int i,j,tot=0; 
        scanf("%d%lld%lld",&n,&A,&B);    
        G=A/gcd(A,B+1)*B;        
        for(i=1;i<=n;++i) 
        { 
            scanf("%lld%lld",&L[i],&R[i]);                                
            if(L[i]-R[i]+1>=G) { printf("%lld
    ",G); return 0; }   
            else 
            {
                L[i]%=G,R[i]%=G;    
                if(L[i]<=R[i]) s[++tot]=line(L[i],R[i]);      
                else s[++tot]=line(0,R[i]),s[++tot]=line(L[i],G-1);     
            }
        }     
        sort(s+1,s+1+tot);   
        ll ans=0;  
        for(i=1;i<=tot;i=j) 
        {                             
            ll nr=s[i].r;             
            for(j=i;j<=tot&&s[j].l<=nr;++j) nr=max(nr,s[j].r);     
            ans+=nr-s[i].l+1;     
        }
        printf("%lld
    ",ans);  
        return 0;
    }
    

      

  • 相关阅读:
    HDU 1002 A + B Problem II
    leetcode 42.接雨水
    无向图 及其术语
    C++优先队列详解
    C++优先队列详解
    最短路
    最短路
    CF DP练习题
    CF DP练习题
    干货
  • 原文地址:https://www.cnblogs.com/guangheli/p/12310773.html
Copyright © 2011-2022 走看看