牛客網--今日頭條2018校招演算法方向(第一批)編程題1、編程題2

來源:互聯網
上載者:User

1、

[編程題] 編程題1

時間限制:1秒

空間限制:32768K

P為給定的二維平面整數點集。定義 P 中某點x,如果x滿足 P 中任意點都不在 x 的右上方地區內(橫縱座標都大於x),則稱其為“最大的”。求出所有“最大的”點的集合。(所有點的橫座標和縱座標都不重複, 座標軸範圍在[0, 1e9) 內)

如下圖:實心點為滿足條件的點的集合。請實現代碼找到集合 P 中的所有 ”最大“ 點的集合并輸出。


輸入描述:

第一行輸入焦點集的個數 N, 接下來 N 行,每行兩個數字代表點的 X 軸和 Y 軸。對於 50%的資料,  1 <= N <= 10000;對於 100%的資料, 1 <= N <= 500000;


輸出描述:
輸出“最大的” 點集合, 按照 X 軸從小到大的方式輸出,每行兩個數字分別代表點的 X 軸和 Y軸。

輸入例子1:
51 25 34 67 59 0

輸出例子1:
4 67 59 0
AC代碼:

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxn = 5e5+5;struct node{int x , y;node(int x,int y):x(x),y(y){}node(){}bool operator<(const node& n)const {  return this->x < n.x;} };struct node nd[maxn];int flag[maxn]; int main(){int n;while(scanf("%d",&n) == 1){for(int i = 0; i < n; i++)scanf("%d%d",&nd[i].x,&nd[i].y);sort(nd,nd+n);memset(flag,0,sizeof(flag));flag[n-1] = 1;int Max1 = nd[n-1].y;for(int i = n-2; i >= 0; i--){if(nd[i].y < Max1) continue;Max1 = nd[i].y;flag[i] = 1;}for(int i = 0; i < n; i++){if(flag[i])printf("%d %d\n",nd[i].x,nd[i].y);} }return 0;}


2、

[編程題] 編程題2

時間限制:3秒

空間限制:131072K

給定一個數組序列, 需要求選出一個區間, 使得該區間是所有區間中經過如下計算的值最大的一個:

區間中的最小數 * 區間所有數的和最後程式輸出經過計算後的最大值即可,不需要輸出具體的區間。如給定序列  [6 2 1]則根據上述公式, 可得到所有可以選定各個區間的計算值:

 

[6] = 6 * 6 = 36;

[2] = 2 * 2 = 4;

[1] = 1 * 1 = 1;

[6,2] = 2 * 8 = 16;

[2,1] = 1 * 3 = 3;

[6, 2, 1] = 1 * 9 = 9;

 

從上述計算可見選定區間 [6] ,計算值為 36, 則程式輸出為 36。

區間內的所有數字都在[0, 100]的範圍內;
輸入描述:

第一行輸入數組序列長度n,第二行輸入數組序列。對於 50%的資料,  1 <= n <= 10000;對於 100%的資料, 1 <= n <= 500000;


輸出描述:
輸出數組經過計算後的最大值。

輸入例子1:
36 2 1

輸出例子1:
36
AC代碼:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 5e5+10;int a[maxn];int n;int main(){while(scanf("%d",&n) == 1){for(int i = 1; i <= n; i++){scanf("%d",&a[i]);}int Max1 = 0;for(int i = 1; i <= n; i++){int Min1 = a[i];int sum = a[i];for(int j = i-1; j >=1; j--){if(a[j] >= Min1)  sum += a[j];else break;}for(int j = i+1; j <=n; j++){if(a[j] >= Min1)  sum += a[j];else break;}Max1 = max(Max1,sum * Min1);}printf("%d\n",Max1);}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.