复赛一:分糖果(candy)

洛谷:P7909
OJ: P4970

暴力算法:90分

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <iostream>
using namespace std;
int main(){
    int n,l,r,maxn=-1;
    cin>>n>>l>>r;
    for (int i = l; i <=r; i++){
        maxn=max(maxn,i%n);
        if(maxn==n-1)break;
    }
    cout<<maxn<<endl;
}

LTE( 500000000 500004321 998244300)

优化方法一:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <iostream>
using namespace std;
int main(){
    int n,l,r,ans,a;
    cin>>n>>l>>r;
    a=l/n;
    if((a+1)*n>r)cout<<r-a*n;
    else cout<<n-1;
    return 0;
}

优化方法二:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <iostream>
using namespace std;
int main(){
    int n,l,r,ans,a;
    cin>>n>>l>>r;
    
    if(l%n+r-l>=n-1)cout<<n-1;
    else cout<<l%n+r-l;
    return 0;
}
Scroll to Top