codeforces 484b//Maximum Value// Codeforces Round #276(Div. 1)

题意:给一个数组,求其中任取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=,INF=0x7FFFFFFF;
typedef long long lon;
const double EPS=1e-; int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon n;
cin>>n;
vector<lon> vct(n);
for(int i=;i<n;++i)
{
cin>>vct[i];
}
sort(vct.begin(),vct.end());
vct.erase(unique(vct.begin(),vct.end()),vct.end());
lon res=,last=;
for(int i=;i<vct.size();++i)
{
if(vct[i]!=)
for(lon j=;;++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-)break;
if(cur>1e6+)break;
}
}
cout<<res<<endl;
return ;
}
上一篇:处理异常json串的jar包JsonSerde


下一篇:dmesg和addr2line 定位 segfault