Sorting the TombstonesTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeURAL 1252
Description
There is time to throw stones and there are time to sort stones ... An old desolate cemetery was a long dismal row of nameless tombstones there are
NTombstones of various shapes. The weights of all the stones is different. People has decided to make the cemetery look more presentable, sorting the tombstone according to their weight. The local custom allows to transpose stones if there is exactly
KOther stones between them.
Input
The first input line contains an integer
N(1≤
N≤130000). Each of the next
NLines contains an integer
X, the weight of a stone in grams (1≤
X≤130000).
Output
The output should contain the single integer-the maximal value of
K(0≤
K<
N), that's makes possible the sorting of the stones according to their weights.
Sample Input
input |
Output |
53021564017 |
1 |
The main topic: give you n number, let you ask the number of interval K can be exchanged on both sides, let this n number order, ask this K max is how much. For example: K = 1, i.e. 56 and 17 can be swapped position, 21 and 40 can be swapped position. Problem-solving ideas: We can set the starting position of each number is the IDX, the order should be in the position of DST. Then DST = idx + k*x. K is the required value and x represents an integer. So that all the numbers can be exchanged to reach an orderly position, then dst[i] = Idx[i] + k[i] * X[i]. So the K that we ask for is all gcd (K[i]*x[i], ans). Also note that the order consists of two kinds, increment and decrement, and the result takes two kinds of maximum values.
#include <stdio.h> #include <algorithm> #include <bits/stdc++.h> #include <string.h> #include <bitset> #include <math.h> #include <iostream>using namespace std;const int maxn = 1e6;struct stone{int Wei,idx;} Stones[maxn];int GCD (int a,int b) {return b = = 0? a:gcd (b,a%b);} BOOL Cmp1 (Stone A,stone b) {return A.wei < B.wei;} BOOL Cmp2 (Stone A,stone b) {return a.wei > B.wei;} int main () {int n; while (scanf ("%d", &n)!=eof) {for (int i = 1; I <= n; i++) {scanf ("%d", &stones[i].wei); Stones[i].idx = i; } sort (STONES+1,STONES+1+N,CMP1); int nn = 0, ans = 0, gcd = 0; for (int i = 1; I <= n; i++) {int tmp = ABS (I-STONES[I].IDX); if (TMP) {nn++; gcd = gcd (gcd,tmp); }} if (nn = = 0) {ans = n-1; } ans = max (ans,gcd-1); Sort (STONES+1,STONES+1+N,CMP2); nn = 0, gcd = 0; for (int i = 1; I <= N; i++) {int tmp = ABS (I-STONES[I].IDX); if (TMP) {nn++; GCD = gcd (gcd,tmp); }} if (nn = = 0) {ans = n-1; } ans = max (ans,gcd-1); printf ("%d\n", ans); } return 0;} /*53021564017*/
URAL 1252--sorting The Tombstones —————— "GCD application"