题意:给一个数组,求其中任取2个元素,大的模小的结果最大值。
一个数x,它的倍数-1(即kx-1),模x的值是最大的,然后kx-2,kx-3模x递减。那么lower_bound(kx)的前一个就是最优的值,用它模x更新。一旦最优值是最后一个元素,那么更新完后break;对数组排序完后对每个元素进行如上操作。(跳过1),复杂度为n/2+n/3+......=n(1/2+1/3+......)=nlogn。不过超时了。因为如果数组中全是2,最后一个为99999,复杂度为n/2+n/2+......=n*n/2。因此数组要去重。
乱码:
//#pragma comment(linker,"/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> #include <stack> #include <list> using namespace std; const int SZ=1000010,INF=0x7FFFFFFF; typedef long long lon; const double EPS=1e-9; int main() { std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); lon n; cin>>n; vector<lon> vct(n); for(int i=0;i<n;++i) { cin>>vct[i]; } sort(vct.begin(),vct.end()); vct.erase(unique(vct.begin(),vct.end()),vct.end()); lon res=0,last=0; for(int i=0;i<vct.size();++i) { if(vct[i]!=1) for(lon j=2;;++j) {//j<(n/(i+2))+100 lon cur=vct[i]*j; lon pos=lower_bound(vct.begin(),vct.end(),cur)-vct.begin(); --pos; //if(i==199999)cout<<n<<" "<<pos<<" "<<vct[pos]<<endl; res=max(res,vct[pos]%vct[i]); //cout<<i<<" "<<cur<<endl; if(pos==n-1)break; if(cur>1e6+100)break; } } cout<<res<<endl; return 0; }