1、
Given a sequence of as many as 10,000 integers (0 < integer < 100,000), what is the maximum decreasing subsequence? Note that the subsequence does not have to be consecutive. (USACO 2.2)
#include <stdio.h>
#define SIZE 200000
#define MAX(x,y) ((x)>(y)?(x):(y)) int best[SIZE]; // best[] holds values of the optimal sub-sequence int main (void) { FILE *in = fopen ("input.txt", "r"); int i, n, k, x, sol; int low, high; fscanf (in, "%d", &n); // N = how many integers to read in // read in the first integer fscanf (in, "%d", &best[0]); sol = 1; for (i = 1; i < n; i++) { best[i] = -1; fscanf (in, "%d", &x); if(x >= best[0]) { k = 0; best[0] = x; } else { // use binary search instead low = 0; high = sol-1; for(;;) { k = (int) (low + high) / 2; // go lower in the array if(x > best[k] && x > best[k-1]) { high = k - 1; continue; } // go higher in the array if(x < best[k] && x < best[k+1]) { low = k + 1; continue; } // check if right spot if(x > best[k] && x < best[k-1]) best[k] = x; if(x < best[k] && x > best[k+1]) best[++k] = x; break; } } sol = MAX (sol, k + 1); } printf ("best is %d\n", sol); fclose(in); return 0; }
Scan from left to right, maintain a decreasing sequence. For each number, binary search in the decreasing sequence to see whether it can be substituted.
int[] findDecreasing(int[] a) {
int[] ds = new int[a.length];
Arrays.fill(ds, 0);
int dsl = 0;
int lastdsl = 0;
for (int i=0; i<a.length; i++) {
// binary search in ds to find the first element ds[j] smaller than
a[i]. set ds[j] = a[i], or append a[i] at the end of ds
int s=0, t=dsl-1;
while (s<=t) {
int m = s+(t-s)/2;
if (ds[m] < a[i]) {
t = m - 1;
} else {
s = m + 1;
}
}
// now s must be at the first ds[j]<a[i], or at the end of ds[]
ds[s] = a[i];
if (s > dsl) { dsl = s; lastdsl = i; }
}
// now trace back.
for (int i=lastdsl-1, j=dsl-1; i>=0 && j >= 0; i--) {
if (a[i] == ds[j]) { j --; }
else if (a[i] < ds[j]) { ds[j--] = a[i]; }// 应该 改成 else if (a[i] < ds[j] && a[i] > ds[j+1]) { ds[j--] = a[i]; }否则 样例 9 4 1 3 2 5 4 是不对的
}
return Arrays.copyOfRange(ds, 0, dsl+1);
}
int[] ds = new int[a.length];
Arrays.fill(ds, 0);
int dsl = 0;
int lastdsl = 0;
for (int i=0; i<a.length; i++) {
// binary search in ds to find the first element ds[j] smaller than
a[i]. set ds[j] = a[i], or append a[i] at the end of ds
int s=0, t=dsl-1;
while (s<=t) {
int m = s+(t-s)/2;
if (ds[m] < a[i]) {
t = m - 1;
} else {
s = m + 1;
}
}
// now s must be at the first ds[j]<a[i], or at the end of ds[]
ds[s] = a[i];
if (s > dsl) { dsl = s; lastdsl = i; }
}
// now trace back.
for (int i=lastdsl-1, j=dsl-1; i>=0 && j >= 0; i--) {
if (a[i] == ds[j]) { j --; }
else if (a[i] < ds[j]) { ds[j--] = a[i]; }// 应该 改成 else if (a[i] < ds[j] && a[i] > ds[j+1]) { ds[j--] = a[i]; }否则 样例 9 4 1 3 2 5 4 是不对的
}
return Arrays.copyOfRange(ds, 0, dsl+1);
}
在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往楼下跑了,然后身高矮的排在队伍的前面,身高较高的就要排在队尾。突然,有一天出操负责人想了一个主意,想要变换一下队形,就是当大家都从楼上跑下来后,所有的学生都随机地占在一排,然后出操负责人从队伍中抽取出一部分学生,使得队伍中剩余的学生的身高从前往后看,是一个先升高后下降的“山峰”形状。据说这样的形状能够给大家带来好运,祝愿大家在学习的道路上勇攀高峰。(注,山峰只有一边也符合条件,如1,1、2,2、1均符合条件)
- 输入:
-
输入可能包含多个测试样例。
对于每个测试案例,输入的第一行是一个整数n(1<=n<=1000000):代表将要输入的学生个数。
输入的第二行包括n个整数:代表学生的身高(cm)(身高为不高于200的正整数)。
- 输出:
-
对应每个测试案例,输出需要抽出的最少学生人数。
- 样例输入:
-
6 100 154 167 159 132 105 5 152 152 152 152 152
code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define _DEBUG 1
#define MAXN 1000010
#define MAXH 400
int arr[MAXN];//
int lis[MAXH];//最大上升子序列
int l2r[MAXN];//从左到有上升子序列的大小
int r2l[MAXN];//从右到左上升子序列的大小
int count;
bool d_equals(int a,int b){
return a==b;
}
int biSearch(int i,int key){//在lis中前i个元素中,找到第一个小于key的位置的后一位置
int l=0,r=i-1;
int mid;
while(l<r-1){
mid=l+((r-l)>>1);
if(d_equals(lis[mid],key))
return mid;
if(lis[mid]>key){
r=mid;
}else{
l=mid;
}
}
if(lis[l]>key || d_equals(lis[l],key) )
return l;
else
return r;
}
int main(){
#if _DEBUG==1
freopen("1500.in","r",stdin);
#endif
int n,i;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
//从左到右寻找最大子序列
count=0;
int pi;//应该放的位置,即以该点结束的序列的最长长度-1
lis[count++]=arr[0];
l2r[0]=count-1;
for(i=1;i<n;i++){
if(arr[i]>lis[count-1]){
pi=count++;
}
else{
pi=biSearch(count,arr[i]);//找到第一个小于num的位置的后一位置
}
lis[pi]=arr[i];
l2r[i]=count-1;
}
//从右到左寻找最大子序列
count=0;
lis[count++]=arr[n-1];
r2l[n-1]=count-1;
for(i=n-2;i>=0;i--){
if(arr[i]>lis[count-1]){
pi=count++;
}
else{
pi=biSearch(count,arr[i]);//找到第一个小于num的位置的后一位置
}
lis[pi]=arr[i];
r2l[i]=count-1;
}
int max=-1;
for(i=0;i<n;i++){//特别注意理解
if(l2r[i]+r2l[i]+1>max)
max=l2r[i]+r2l[i]+1;
}
printf("%d\n",n-max);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define _DEBUG 1
#define MAXN 1000010
#define MAXH 400
int arr[MAXN];//
int lis[MAXH];//最大上升子序列
int l2r[MAXN];//从左到有上升子序列的大小
int r2l[MAXN];//从右到左上升子序列的大小
int count;
bool d_equals(int a,int b){
return a==b;
}
int biSearch(int i,int key){//在lis中前i个元素中,找到第一个小于key的位置的后一位置
int l=0,r=i-1;
int mid;
while(l<r-1){
mid=l+((r-l)>>1);
if(d_equals(lis[mid],key))
return mid;
if(lis[mid]>key){
r=mid;
}else{
l=mid;
}
}
if(lis[l]>key || d_equals(lis[l],key) )
return l;
else
return r;
}
int main(){
#if _DEBUG==1
freopen("1500.in","r",stdin);
#endif
int n,i;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
//从左到右寻找最大子序列
count=0;
int pi;//应该放的位置,即以该点结束的序列的最长长度-1
lis[count++]=arr[0];
l2r[0]=count-1;
for(i=1;i<n;i++){
if(arr[i]>lis[count-1]){
pi=count++;
}
else{
pi=biSearch(count,arr[i]);//找到第一个小于num的位置的后一位置
}
lis[pi]=arr[i];
l2r[i]=count-1;
}
//从右到左寻找最大子序列
count=0;
lis[count++]=arr[n-1];
r2l[n-1]=count-1;
for(i=n-2;i>=0;i--){
if(arr[i]>lis[count-1]){
pi=count++;
}
else{
pi=biSearch(count,arr[i]);//找到第一个小于num的位置的后一位置
}
lis[pi]=arr[i];
r2l[i]=count-1;
}
int max=-1;
for(i=0;i<n;i++){//特别注意理解
if(l2r[i]+r2l[i]+1>max)
max=l2r[i]+r2l[i]+1;
}
printf("%d\n",n-max);
}
return 0;
}