zoukankan      html  css  js  c++  java
  • Saruman's Army (POJ 3069)

    直线上有N个点。点i的位置是Xi。从这N个点中选择若干个,给它们加上标记。对每一个点,其距离为R以内的区域里必须又带有标记的点(自己本身带有标记的点,可以认为与其距离为0的地方有一个带有标记的点)。在满足这个条件的情况下,希望能为尽可能少的点添加标记。请问至少要有多少点被加上标记?

    #include "iostream"
    #include "algorithm"
    using namespace std;
    
    const int MAX_N = 1000;
    int N=6, R=10;
    int X[MAX_N] = {1,7,15,20,30,50};
    
    void solve() {
    	sort(X,X+N);
    	int i = 0, ans = 0;
    	while (i<N)
    	{
    		//S为最左侧没有被覆盖的点
    		int S = X[i++];
    		//一直向右前进直到距离S的距离大于R的点
    		while (i<N && X[i]<=S+R)	i++;
    		//P是新加上标记的点的位置
    		int P = X[i-1];
    		//一直向右前进找到距离P的距离大于R的点
    		while (i<N && X[i]<=P+R)	i++;
    		ans++;
    	}
    	cout << ans << endl;
    }
    
    int main() {
    	solve();
    	system("pause");
    	return 0;
    }#include "iostream"
    #include "algorithm"
    using namespace std;
    
    const int MAX_N = 1000;
    int N=6, R=10;
    int X[MAX_N] = {1,7,15,20,30,50};
    
    void solve() {
    	sort(X,X+N);
    	int i = 0, ans = 0;
    	while (i<N)
    	{
    		//S为最左侧没有被覆盖的点
    		int S = X[i++];
    		//一直向右前进直到距离S的距离大于R的点
    		while (i<N && X[i]<=S+R)	i++;
    		//P是新加上标记的点的位置
    		int P = X[i-1];
    		//一直向右前进找到距离P的距离大于R的点
    		while (i<N && X[i]<=P+R)	i++;
    		ans++;
    	}
    	cout << ans << endl;
    }
    
    int main() {
    	solve();
    	system("pause");
    	return 0;
    }
    

     

  • 相关阅读:
    LeetCode 382. Linked List Random Node
    LeetCode 398. Random Pick Index
    LeetCode 1002. Find Common Characters
    LeetCode 498. Diagonal Traverse
    LeetCode 825. Friends Of Appropriate Ages
    LeetCode 824. Goat Latin
    LeetCode 896. Monotonic Array
    LeetCode 987. Vertical Order Traversal of a Binary Tree
    LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
    LeetCode 636. Exclusive Time of Functions
  • 原文地址:https://www.cnblogs.com/sky-z/p/5595996.html
Copyright © 2011-2022 走看看