(1)題目大大意就是尋找乙個(arr[i],arr[j]),(j>i&&arr[j]>arr[i])求(j-i)的最大值
(2)貪心思路,見**1
(3)思路1維護乙個單調遞減的棧,如果發現此時的arr[i]小於此時棧頂元素,或者棧為空那麼進,else(不二分最後一組資料會超時,)二分查詢棧(陣列模擬)中的第乙個比此時元素小的下標更新ans.
(1)**
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define max 2005
using namespace std;
const int maxn = 10000007;
struct arr inof[maxn];
bool cmp(arr a, arr b)
int main()
int ans = 0,ec = -9999;
sort(inof, inof + n,cmp);
for (int i = 0; i < n; i++)
cout << ans << endl;
return 0;
}(2)**
include
#include
using namespace std;
int b[50000],a[50000];
int n,q;
int findpos(int val) //找到小於等於val的數的最小下標
return b[lo];
}int main()
}
printf("%d\n", ans);
return 0;
}
51nod1272 最大距離 棧 貪心
1272 最大距離 codility 基準時間限制 1 秒 空間限制 131072 kb 分值 20 難度 3級演算法題 給出乙個長度為n的整數陣列a,對於每乙個陣列元素,如果他後面存在大於等於該元素的數,則這兩個數可以組成一對。每個元素和自己也可以組成一對。例如 可以組成11對,如下 數字為下標 ...
51Nod 1272 最大距離
acm模版 方法有很多種,這裡介紹兩種寫法。第一種比較容易想到的寫法是將鍵值和下標封裝在結構體中進行排序,然後從尾部檢索一遍即可,複雜度o nlogn 第二種是使用單調棧優化,可以使複雜度低至o n one include include include using namespace std co...
51nod 1272 最大距離
codility 基準時間限制 1 秒 空間限制 131072 kb 分值 20 難度 3級演算法題 給出乙個長度為n的整數陣列a,對於每乙個陣列元素,如果他後面存在大於等於該元素的數,則這兩個數可以組成一對。每個元素和自己也可以組成一對。例如 可以組成11對,如下 數字為下標 0,0 0,2 1,...