Question F: Escape Room
Time Limit: 1 Sec Memory Limit: 128 MBCommits: 63 Resolved: 27
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]
Topic description
Can you open the door?
enter
The next line contains N space-separated integer Ai (1≤Ai≤N).
It’s guaranteed that at least one possible permutation exists.
output
sample input
4
1 2 2 1
Sample output 4 2 1 3
**************************************************** ************************
is a thinking question about the longest ascending subsequence, so it belongs to dp. The
meaning of the question:
give you n numbers are The maximum length of the longest ascending subsequence of each number from this position to the last digit
Find a matching sequence (if multiple outputs are the smallest)
//
Sample output: 4 2 1 3
(the longest ascending length of 4 is 1 ; the longest ascending length of 2 is 2; the longest ascending length of 1
is
2; The smallest fills in 4 3 2 1 sequentially
but it has to be traversed every time, I tried to traverse the largest and smallest at the same time, but still tle
my tle error code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; intmain () { int n; int a [ 100005 ] = { 0 }; int ans [ 100005 ] = { 0 }; scanf("%d",&n); int maxx = 0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); if(a[i]>maxx) maxx = a[i]; } int p=1,q=maxx; //min max order int cnt1=1,cnt2=n; // min max num while(1) { if(p>q) break; for(int i=0;i<n;i++) { if(a[i]==p) { ans[i] = cnt2; cnt2 - ; } if(a[n-1-i]==q) { ans[n-1-i]=cnt1; cnt1++; } } // for(int i=0;i<n;i++) // { // printf("%d ",ans[i]); // } //printf("\n"); p++; q--; } for(int i=0;i<n-1;i++) { printf("%d ",ans[i]); } printf("%d",ans[n-1]); }
Then the teammates thought of an algorithm: use a three-dimensional structure
To make it easier to understand I wrote the group example: 1 1 2 3 3 2 1 1
Step 1: Mark the subscript of the maximum ascending sequence of the first dimension in the second dimension (for recording the original position):
First dimension x: 1 1 2 3 3 2 1 1
Second dimension num: 1 2 3 4 5 6 7 8
Sort by first dimension:
x: 1 1 1 1 2 2 3 3
num:1 2 7 8 3 6 4 5
The third dimension: 8 7 6 5 4 3 2 1 (fill in the corresponding x in reverse order)
In order to change x back to its original position
reorder the entire structure by num
return to the original order
x: 1 1 2 3 3 2 1 1
num:1 2 3 4 5 6 7 8
r: 8 7 4 2 1 3 6 5
The third dimension of the output is the answer
Correct code:
#include <sstream> #include <iostream> #include <string> #include<stdlib.h> #include<stdio.h> #include<algorithm> using namespace std; struct po //结构体 { int x,num,r; }; bool cmp1 (after a, after b) { if(a.x!=b.x) return a.x<b.x; return a.num<b.num; } bool cmp2(po a,po b) { return a.num<b.num; } intmain () { struct po s[100008]= {0}; int n,i,x,t; scanf("%d",&n); for(i=1; i<=n; i++) { scanf("%d",&t); s[i].num = i; //fill the original order into num s[i].x=t; } sort(s + 1 ,s+ 1 + n,cmp1); //sort by x int k= n; for (i= 1 ; i<=n; i++ ) //fill in reverse order { s[i].r=k; k--; } sort(s + 1 ,s+ 1 + n,cmp2); //sort by num to restore the original order of the structure for (i= 1 ;i<n;i++)printf( " %d " ,s[i].r ); //output r printf("%d\n",s[n].r); return 0; }