zoukankan      html  css  js  c++  java
  • CodeForces 484B Maximum Value

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #define sc(x)  scanf("%d", &x)
     7 #define sc2(x,y)    scanf("%d%d", &x, &y)
     8 #define pf(x)    printf("%d
    ",x)
     9 #define PF(x)    printf("%d ",x)
    10 #define FOR(i,b,e)    for(int i=b;i<=e;i++)
    11 #define FOR1(i,b,e)    for(int i=b;i>=e;i--)
    12 #define CL(x,y)      memset(x,y,sizeof(x))
    13 using namespace std;
    14 const int MAX = 1000000;
    15 vector <int> v;
    16 int n;
    17 long long used[MAX];
    18 int main()
    19 {
    20     int tmp, ans;
    21     while(~sc(n))
    22     {
    23         ans = 0;
    24         CL(used, 0);
    25         FOR(i,0,n-1)
    26         {
    27             sc(tmp);
    28             used[tmp] = 1;
    29             v.push_back(tmp);
    30         }
    31         sort(v.begin(), v.end());
    32         FOR(i,0,n-2)
    33         {
    34             if(v[i] == 0 || v[i] == v[i+1])
    35                 continue;
    36             FOR(j,i+1,n-1)
    37             {
    38                 if(used[v[j]])
    39                     ans = ans > (v[j] % v[i]) ? ans : (v[j] % v[i]);
    40                 used[v[j]] = 0;
    41             }
    42         }
    43         pf(ans);
    44     }
    45     return 0;
    46 }
    View Code

    以上代码超时,不可以

     方法二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #define sc(x)  scanf("%d", &x)
     7 #define sc2(x,y)    scanf("%d%d", &x, &y)
     8 #define pf(x)    printf("%d
    ",x)
     9 #define PF(x)    printf("%d ",x)
    10 #define FOR(i,b,e)    for(int i=b;i<=e;i++)
    11 #define FOR1(i,b,e)    for(int i=b;i>=e;i--)
    12 #define CL(x,y)      memset(x,y,sizeof(x))
    13 using namespace std;
    14 const int MAX = 1000000;
    15 vector <int> v;
    16 int n, used[MAX+5];
    17 int main()
    18 {
    19     int tmp, ans;
    20     while(~sc(n))
    21     {
    22         ans = 0;
    23         CL(used,0);
    24         FOR(i,0,n-1)
    25         {
    26             sc(tmp);
    27             if(!used[tmp])//这里进行了取出重复的
    28             {
    29                 v.push_back(tmp);
    30                 used[tmp] = true;
    31             }
    32         }
    33         sort(v.begin(), v.end());
    34         n = v.size();
    35         FOR(i,0,n-1)
    36         {
    37             if(v[i] == 0)  continue;
    38             int pos = 0, k = 2, num = v[i];
    39             while(pos<n-1)
    40             {
    41                 pos = lower_bound(v.begin(), v.end(), num * k++) - v.begin() - 1;//最多就是插在尾部(n的位置)
    42                 tmp = v[pos] % num;
    43                 ans = ans > tmp ? ans : tmp;
    44                 if(pos == n-1)
    45                 {
    46                     break;
    47                 }
    48             }
    49         }
    50         pf(ans);
    51     }
    52     return 0;
    53 }
    View Code

    这里减少了动态数组中存在的数,减少了不必要的判断

    方法三:

     1 /********************************/
     2 /*Problem:                        */
     3 /*User:         shinelin        */
     4 /*Memory:         1000K            */
     5 /*Time:         1000MS            */
     6 /*Language:     C++                */
     7 /********************************/
     8 #include <cstdio>
     9 #include <iostream>
    10 #include <cstdlib>
    11 #include <ctime>
    12 #include <cctype>
    13 #include <cstring>
    14 #include <string>
    15 #include <list>
    16 #include <map>
    17 #include <queue>
    18 #include <deque>
    19 #include <stack>
    20 #include <vector>
    21 #include <set>
    22 #include <algorithm>
    23 #include <cmath>
    24 using namespace std;
    25 
    26 #define INF 0x7fffffff
    27 #define LL long long
    28 
    29 vector<int> a;
    30 int N;
    31 
    32 bool BigMod(int d)
    33 {
    34     int k = lower_bound(a.begin(), a.end(), d) - a.begin();
    35     for (int i = k; i < N; i ++)
    36     {
    37         if(a[i] == a[i-1]) continue;
    38         int j = 2 * a[i];
    39         while (j <= a[N-1])
    40         {
    41             int cur = lower_bound(a.begin(), a.end(), j) - a.begin();
    42             if(cur > 0 && a[cur-1] % a[i] > d)
    43             {
    44                 return true;
    45             }
    46             j += a[i];
    47         }
    48         if(a[N-1] % a[i] > d)
    49             return true;
    50     }
    51     return false;
    52 }
    53 int main()
    54 {
    55     int x, Min, Max, Mid;
    56     scanf("%d", &N);
    57     for (int i = 0; i < N; i ++)
    58     {
    59         scanf("%d", &x);
    60         a.push_back(x);
    61     }
    62     sort(a.begin(), a.end());
    63     Min = 0;
    64     Max = a[N-1];
    65     while(Min < Max)
    66     {
    67         Mid = (Max + Min) >> 1;
    68         if(BigMod(Mid))
    69         {
    70             Min = Mid + 1;
    71         }
    72         else
    73         {
    74             Max = Mid;
    75         }
    76     }
    77 //    Mid = (Max + Min) >> 1;
    78     printf("%d
    ", Min);
    79     return 0;
    80 }
    View Code

    必须真正有二分的思想解决(没看懂呢)

  • 相关阅读:
    checkpoint出现的时间
    快速断开当前数据库的所有连接的方法
    SQLSERVER备份数据库的时候copy only选项的意思
    SQLSERVER备份事务日志的作用
    SQLSERVER使用密码加密备份文件以防止未经授权还原数据库
    Windows Azure终于到来中国了
    SQLSERVER2005的安装目录结构(下)
    SQLSERVER2005的安装目录结构(上)
    给大家分享两款正在使用的reflector插件
    一套内容采集系统 解放编辑人员
  • 原文地址:https://www.cnblogs.com/ghostTao/p/4394864.html
Copyright © 2011-2022 走看看