本文主要是介绍UVALIVE 5000 Underwater Snipers(二分+贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二分答案
贪心判断
// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62template<class T>
inline bool read(T &n)
{T x = 0, tmp = 1; char c = getchar();while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();if(c == EOF) return false;if(c == '-') c = getchar(), tmp = -1;while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();n = x*tmp;return true;
}
template <class T>
inline void write(T n)
{if(n < 0){putchar('-');n = -n;}int len = 0,data[20];while(n){data[len++] = n%10;n /= 10;}if(!len) data[len++] = 0;while(len--) putchar(data[len]+48);
}
//-----------------------------------const int MAXN=10010;int n,s;
ll k,d;struct Point
{ll x,y;bool operator < (const Point& b) const{return y<b.y;}
}a[MAXN],b[MAXN];bool check(ll y)
{for(int i=1;i<=n;i++){ll temp=sqrt(d*d-(a[i].y-y)*(a[i].y-y));if(temp<0) return false;b[i].x=a[i].x-temp;b[i].y=a[i].x+temp;}sort(b+1,b+n+1);int num=0,i=1;while(i<=n){ll now=b[i].y;num++;while(i<=n && now>=b[i].x) i++;}if(num>s) return false;return true;
}ll solve(ll l,ll r)
{ll ans=-1;while(l<=r){ll mid=MID(l,r);if(check(k-mid)){l=mid+1;ans=mid;}elser=mid-1;}return ans;
}int main()
{
// freopen("data.txt","r",stdin);int T,cas=1;read(T);while(T--){read(k),read(n),read(s),read(d);for(int i=1;i<=n;i++)read(a[i].x),read(a[i].y);ll ans=solve(0,d);printf("Case %d: ",cas++);if(ans==-1)puts("IMPOSSIBLE");elsewrite(ans),putchar('\n');}return 0;
}
这篇关于UVALIVE 5000 Underwater Snipers(二分+贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!