B. Maximum Value (Codeforces Round #276 (div1 ),
B. Maximum Valuetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
You are given a sequenceAConsistingNIntegers. Find the maximum possible value of (integer remainderAIDividedAJ), Where 1 limit ≤ limitI, Bytes,JLimit ≤ limitNAndAILimit ≥ limitAJ.
Input
The first line contains integerN-The length of the sequence (1 sequence ≤ sequenceNLimit ≤ limit 2-105 ).
The second line containsNSpace-separated integersAI(1 digit ≤ DigitAILimit ≤ limit 106 ).
Output
Print the answer to the problem.
Sample test (s) input
33 4 5
Output
2
Find the maximum value of a [j] % a [I] in a [j]
Because ai <1000000; can be hash, if a vertex does not exist, record the maximum value smaller than it.
For the value after finding the maximum modulo, take a [I] = I, that is, This vertex exists. The maximum value after the modulo operation must be I + 1 + k * I, in this way, each time I query is added,
Find the value closest to the maximum value.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=2000000+100;int a[maxn];int main(){ int n; scanf("%d",&n); int x; for(int i=0;i<n;i++) { scanf("%d",&x); a[x]=x; } for(int i=0;i<maxn;i++) { if(a[i]!=i) a[i]=a[i-1]; } int ans=0; for(int i=2;i<maxn;i++) { if(a[i]==i) { for(int j=i+i-1;j<maxn;j=j+i) { if(a[j]%i>ans&&a[j]>i) ans=a[j]%i; } } } printf("%d\n",ans); return 0;}
How can codeforces change the id color)
It's not Beijing time. It's four hours later. The color is not clear. I only know that 200 or 300 is blue.
ACM problems how does my code always time out ?? Http: // codeforcescom/contest/50/problem/B
Your algorithm needs optimization. In fact, you don't need to find the matching I j for each pair. You just need to find the number. Let me give you some simple code. You should understand it.
# Include <stdio. h>
Double table [256];
Char str [100001];
Void main ()
{
Char * p = str;
Int I;
Double n = 0;
Gets (str );
While (* p)
Table [* (p ++)] + = 1;
For (int I = 1; I <256; I ++)
N + = table [I] * table [I];
Printf ("%. 0lf", n );
}