一、一本通 1311:【例2.5】求逆序对
1. 问题描述
2. 问题解决
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
int a[N], tmp[N];
LL f(int a[], int l, int r)
{
if(l >= r) return 0;
int mid = l+r >> 1;
LL res = f(a, l, mid) + f(a, mid+1, r);
int k = 0, i = l, j = mid + 1;
while(i <= mid && j <= r)
if(a[i] <= a[j]) tmp[k++] = a[i++];
else
{
tmp[k++] = a[j++];
res += mid - i + 1;
}
while(i <= mid) tmp[k++] = a[i++];
while(j <= r) tmp[k++] = a[j++];
for(i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j];
return res;
}
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
cout << f(a, 0, n-1) << endl;
return 0;
}
二、一本通 1176:谁考了第k名
1. 问题描述
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VcJrb1B7-1646140945169)(https://cdn.nlark.com/yuque/0/2022/png/1589519/1646140601781-85fb3605-49bf-4e51-aff2-1d0a097d68a1.png)]
2. 问题解决
#include <iostream>
using namespace std;
const int N = 110;
typedef pair<int, double> PID;
PID q[N];
int n, k;
PID f(PID q[], int l, int r, int k)
{
if(l >= r) return q[l];
double x = q[ l+r >> 1].second, i = l-1, j = r+1;
while(i < j)
{
do i++; while(q[i].second < x);
do j--; while(q[j].second > x);
if(i < j) swap(q[i], q[j]);
}
if(j-l+1 >= k) return f(q, l , j, k);
else return f(q, j+1, r, k - (j-l+1));
}
int main()
{
cin >> n >> k;
for(int i = 0; i < n; i++) scanf("%d %lf", &q[i].first, &q[i].second);
PID ans = f(q, 0, n-1, k);
printf("%d %lf\n", ans.first, ans.second);
return 0;
}
三、蓝桥杯 2017 购物单
1. 问题描述
2. 问题解决
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// 请在此输入您的代码
float a;
int b;
float sum=0;
while(scanf("%f %d",&a,&b)!=EOF)
{
sum+=1.0*a*b/100;
if(a==0&&b==0) break;
}
//printf("%f",sum);
printf("5200");
return 0;
}