1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include<set> #include<algorithm> using namespace std; int a[10]; struct point { int num; int index; int sor; }; bool cmp(point a, point b) { return a.num < b.num; } point p[10]; int main() { int n; scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d",&p[i].num); p[i].index = i; } sort(p, p + n, cmp); for(int i = 0; i < n; i++) p[i].sor = i; for(int i = 1; i < n ;i++){ for(int j = i - 1;j >= 0; j--){ // printf("i:%d %d %d j: %d %d %d\n",i,p[i].index,p[i].sor,j,p[j].index,p[j].sor); int il = min(p[i].index, p[i].sor); int ir = max(p[i].index, p[i].sor); int jl = min(p[j].index, p[j].sor); int jr = max(p[j].index, p[j].sor); bool flag = false; if(il == jl || ir == jr) flag = true; if(il < jl && ir > jl) flag = true; if(jl < il && jr > il) flag = true; int Min = min(min(p[i].index,p[j].index), min(p[i].sor,p[j].sor)); int Max = max(max(p[i].index,p[j].index), max(p[i].sor,p[j].sor)); if(flag) { for(int k = 0; k < i; k++){ if(p[i].index == p[k].index && p[i].sor == p[k].sor){ p[k].index = Min; p[k].sor = Max; } } p[i].index = Min; p[i].sor = Max; for(int k = 0; k < j; k++){ if(p[j].index == p[k].index && p[j].sor == p[k].sor){ p[k].index = Min; p[k].sor = Max; } } p[j].index = Min; p[j].sor = Max; } } } set<int> s; // for(int i = 0; i < n; i++) //printf("%d %d %d\n",i,p[i].index,p[i].sor); for(int i = 0; i < n; i++) s.insert(p[i].index); printf("%d\n", s.size()); }
|