OJ-Problems-Source/.ACM-Templates/TXTs/排序查找.txt

42 lines
1.0 KiB
Plaintext
Raw Normal View History

2016-11-22 09:38:35 +08:00
//<2F><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
const double EPS = 1e-9;
double TS(double l, double r) {
while (r - l > EPS) {
double mid1 = l + (r - l) / 3.0, mid2 = r - (r - l) / 3.0;
if (calc(mid1) > calc(mid2)) { r = mid2; }
else { l = mid1; }
}
return l;
}
//<2F><EFBFBD><E9B2A2><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ll cnt;
void mergeSort(int a[], int l, int r) {
if (l >= r) { return; }
int mid = (l + r) >> 1;
mergeSort(a, l, mid);
mergeSort(a, mid + 1, r);
vector<int> res(r - l + 1);
int i = l, j = mid + 1, k = 0;
while (i <= mid && j <= r) {
if (a[i] > a[j]) { res[k++] = a[j++]; cnt += mid + 1 - i; }
else { res[k++] = a[i++]; }
}
while (i <= mid) { res[k++] = a[i++]; }
while (j <= r) { res[k++] = a[j++]; }
for (k = l; k <= r; k++) { a[k] = res[k - l]; }
}
//<2F><><EFBFBD><EFBFBD>
void quickSort(int a[], int l, int r) {
if (l >= r) { return; }
int i = l, j = r, v = a[l];
while (i < j) {
while (i < j && a[j] >= v) { j--; }
a[i] = a[j];
while (i < j && a[i] <= v) { i++; }
a[j] = a[i];
}
a[i] = v;
quickSort(a, l, i - 1);
quickSort(a, i + 1, r);
}