【codevs 1135】【openjudge 4366】
1135 選擇客棧 2011年NOIP全國聯賽提高組
時間限制: 1 s
空間限制: 128000 KB
題目等級 : 鑽石 Diamond
題解
題目描述 Description
麗江河邊有 n 家很有特色的客棧,客棧按照其位置順序從1 到n 編號。每家客棧都按照某一種色調進行裝飾(總共k 種,用整數0 ~ k-1 表示),且每家客棧都設有一家咖啡店,每家咖啡店均有各自的最低消費。
兩位遊客一起去麗江旅遊,他們喜歡相同的色調,又想嘗試兩個不同的客棧,因此決定分別住在色調相同的兩家客棧中。晚上,他們打算選擇一家咖啡店喝咖啡,要求咖啡店位於兩人住的兩家客棧之間(包括他們住的客棧),且咖啡店的最低消費不超過p。
他們想知道總共有多少種選擇住宿的方案,保證晚上可以找到一家最低消費不超過p元的咖啡店小聚。
輸入描述 Input Description
共n+1 行。
第一行三個整數 n,k,p,每兩個整數之間用一個空格隔開,分別表示客棧的個數,色調的數目和能接受的最低消費的最高值;
接下來的 n 行,第i+1 行兩個整數,之間用一個空格隔開,分別表示i 號客棧的裝飾色調和i 號客棧的咖啡店的最低消費。
輸出描述 Output Description
輸出只有一行,一個整數,表示可選的住宿方案的總數。
範例輸入 Sample Input
5 2 3
0 5
1 3
0 2
1 4
1 5
範例輸出 Sample Output
3
資料範圍及提示 Data Size & Hint
【輸入輸出範例說明】
客棧編號 ① ② ③ ④ ⑤
色調 0 1 0 1 1
最低消費 5 3 2 4 5
2 人要住同樣色調的客棧,所有可選的住宿方案包括:住客棧①③,②④,②⑤,④⑤,
但是若選擇住 4、5 號客棧的話,4、5 號客棧之間的咖啡店的最低消費是 4,而兩人能承受
的最低消費是 3 元,所以不滿足要求。因此只有前 3 種方案可選。
【資料範圍】
對於 30%的資料,有n≤100;
對於 50%的資料,有n≤1,000;
對於 100%的資料,有2≤n≤200,000,0 < k≤50,0≤p≤100, 0≤最低消費≤100。
1.暴力 80
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int MAXN = 200005;int n,k,p,x,y;long long ans = 0;vector < int > s[55];int use[MAXN],sum[MAXN];void init(){ memset(sum,0,sizeof(sum)); memset(use,0,sizeof(use)); ans = 0; return;}void work(int x){ int len = s[x].size(); if(len < 2) return;// cout << x << " " << len << endl; for(int i = 0; i < len - 1; i ++) { for(int j = i + 1; j < len; j ++) { int a = s[x][i]; int b = s[x][j]; if(sum[b] - sum[a - 1] > 0) ans ++ ; // cout << "x" << x << " ans" << ans << endl; } } return;}int main(){ scanf("%d %d %d",&n,&k,&p); init(); for(int i = 1; i <= n; i ++) { scanf("%d %d",&x,&y); s[x].push_back(i); if(y <= p) use[i] = 1; } for(int i = 1; i <= n; i ++) { sum[i] = sum[i - 1] + use[i]; //cout << "kiss" << sum[i] << endl; } for(int i = 0; i <= k; i ++) work(i); printf("%lld\n",ans); return 0;}
2.AC
記錄所有顏色的酒店的出現的最後一次的位置,出現次數
假設當前客棧為x
找左邊有沒有<=p的咖啡店
然後再找左邊有多少個同顏色客棧
就好啦
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 75;int n,k,p,ans = 0,cnt,x,y;int num[MAXN],sum[MAXN],color[MAXN];int main(){ memset(num,0,sizeof(num)); memset(color,0,sizeof(color)); memset(sum,0,sizeof(sum)); scanf("%d %d %d",&n,&k,&p); for(int i = 1; i <= n; i ++) { scanf("%d %d",&x,&y); if(y <= p) cnt = i;//cnt記錄使用中色彩的酒店的左邊的最後一個咖啡店 if(cnt >= color[x])//如果在上一個位置出現的右邊就可以 sum[x] = num[x]; color[x] = i; ans += sum[x]; num[x] ++; } printf("%d\n",ans); return 0;}