输入:
3
4 2 3 1 10 5 9 7
输出:
1
#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct node {
int id, x;
}a[100000 + 10];
bool cmp(node x, node y) {
return x.x < y.x;
}
int main()
{
int n;
cin >> n;
int x = pow(2, n);
for (int i = 1; i <= x; i++)
{
cin >> a[i].x;
a[i].id = i;
}
//把队伍分成两组,前一半一组,后一半一组
sort(a+1, a + (x / 2)+1, cmp);//前一半排序
sort(a + (x / 2)+2, a + x+1, cmp);//后一半排序
int x1 = a[x / 2].x;
int y = a[x].x;
//cout << x1 << " " << y << endl;
if (x1 < y) {//两组的分高的pk, 输出输的编号
cout << a[x/2].id << endl;
}
else {
cout << a[x].id << endl;
}
return 0;
}
树状数组做法:(想复习一下数状数组,就顺手写了,有点菜,(●’◡’●))
#include <iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn = 100000 + 10;
int n;
struct node {
int x, id;
}tree[maxn];//保存为结点
int lowbit(int k) {
return k & -k;
}
void updata(int index, node num,int x) {
while (index <=x) {
if (tree[index].x < num.x) {
tree[index].x = num.x;
tree[index].id = num.id;
}
index += lowbit(index);
}
}
int main() {
//int n;
cin >> n;
int x = pow(2, n);
for (int i = 1; i <= x/2; i++) {//创建两棵树,维护大的值
// int b;
node b;
cin >> b.x;
b.id = i;
updata(i, b,x/2);
}
for (int i = x/2+1; i <= x ; i++) {
// int b;
node b;
cin >> b.x;
b.id = i;
updata(i, b, x);
}
int c = tree[x / 2].x;
int y = tree[x].x;
// 对比两棵树的头部,输出较小的那个
if (c <y) {
cout << tree[x / 2].id;
}
else {
cout << tree[x].id;
}
return 0;
}