CF 990B B. Micro-World【數組操作/貪心/STL/二分搜索】
阿新 • • 發佈:2018-06-22
putc 最終 http world [] HR name AC end
【鏈接】:CF
【題意】:對任意一個數a[i] ,可以對任意 滿足 i != j 且 a[i] > a[j] && a[i] <= a[j] +k 的 a[j] 可以被刪掉,求使最終剩下的個數最少。
【分析】:掃一遍,二分搜索合法的。
【代碼】:
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include<list> #include<assert.h> #include<bitset> #include<numeric> using namespace std; typedef long long ll; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const ll LNF = 1e18; const int maxn = 1e6+ 100; const int maxm = 100; const double PI = acos(-1.0); const double eps = 1e-8; //const int dx[] = {-1,1,0,0,1,1,-1,-1}; //const int dy[] = {0,0,1,-1,1,-1,1,-1}; int dx[] = {-1,0,1,0}; int dy[] = {0,1,0,-1}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} }; int n,k; int a[maxn], b[maxn]; int main() { while(cin>>n>>k) { int cnt=0; for(int i=0;i<n;i++) cin>>a[i];/* for(int i=0;i<n;i++) { cout<<i<<" "; } cout<<endl;*/ sort(a,a+n,greater<int>());/* for(int i=0;i<n;i++) { cout<<a[i]<<' '; } cout<<endl;*/ for(int i=0;i<n;i++) { int p = lower_bound(a,a+n,a[i]+k,greater<int>())-a; //第一個 <= (a[i]+k) 的位置 //cout<<"a[i]+k="<<a[i]+k<<" p="<<p<<" a[p]="<<a[p]<<" a[i]="<<a[i]; if(a[p]>a[i]) { cnt++; //cout<<" YES"; } //cout<<endl; } cout<<n-cnt<<endl; } } /* 7 1 101 53 42 102 101 55 54 42 53 54 55 101 101 102 outputCopy 3 42 55 102 inputCopy 6 5 20 15 10 15 20 25 25 outputCopy 1 inputCopy 7 1000000 1 1 1 1 1 1 1 outputCopy 7 7 1 101 53 42 102 101 55 54 101 53 42 102 101 55 54 102 101 101 55 54 53 42 a[i]+k=103 p=0 a[p]=102 a[i]=102 a[i]+k=102 p=0 a[p]=102 a[i]=101 YES a[i]+k=102 p=0 a[p]=102 a[i]=101 YES a[i]+k=56 p=3 a[p]=55 a[i]=55 a[i]+k=55 p=3 a[p]=55 a[i]=54 YES a[i]+k=54 p=4 a[p]=54 a[i]=53 YES a[i]+k=43 p=6 a[p]=42 a[i]=42 */
[模擬]
#include<cstdio> #include<string> #include<cstdlib> #include<cmath> #include<iostream> #include<cstring> #include<set> #include<queue> #include<algorithm> #include<vector> #include<map> #include<cctype> #include<stack> #include<sstream> #include<list> #include<assert.h> #include<bitset> #include<numeric> using namespace std; typedef long long ll; typedef unsigned long long ULL; typedef pair<int,int> P; const int INF = 0x3f3f3f3f; const ll LNF = 1e18; const int maxn = 1e6+ 100; const int maxm = 100; const double PI = acos(-1.0); const double eps = 1e-8; //const int dx[] = {-1,1,0,0,1,1,-1,-1}; //const int dy[] = {0,0,1,-1,1,-1,1,-1}; int dx[] = {-1,0,1,0}; int dy[] = {0,1,0,-1}; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} }; int n,k,j; int a[maxn], b[maxn]; int main() { while(cin>>n>>k) { int cnt, j=0; for(int i=0;i<n;i++) cin>>a[i]; sort(a,a+n); cnt=n; for(int i=0;i<n;i++) { while(a[j]<a[i]) { if(a[j]+k>=a[i]) cnt--; j++; } } cout<<cnt<<endl; } }
CF 990B B. Micro-World【數組操作/貪心/STL/二分搜索】