注:这两个题目的代码由于我这个小白实在是没能完全掌握map的用法所以用的简单的语句写的比较长,以后有机会会简化的!!加油!!冲鸭!!!
题目——Sunscreen
题目描述
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they’re at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn’t tan at all…
The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
输入
- Line 1: Two space-separated integers: C and L
- Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPFi and maxSPFi
- Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri
输出
A single line with an integer that is the maximum number of cows that can be protected while tanning
样例输入
3 2
3 10
2 5
1 5
6 2
4 1
样例输出
2
思路:
奶牛按照minSPF从大到小排序。对于每一个不低于当前奶牛minSPF的防晒霜,都不会低于后面其他奶牛的minSPF。当前奶牛选择maxSPF较大的防晒霜去使用,对整体影响更小。
#include <bits/stdc++.h>//这个代码是比较麻烦的其实可以用map
using namespace std;
int C,L;
int paixu(int a[],int b[])
{
int temp,temp1;
for(int i=1;i<5000;i++)
{
for(int j=0;j<5000-i;j++)
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp1=b[j];
b[j]=b[j+1];
b[j+1]=temp1;
}
}
}
int main()
{
int min1[5000]={0},max1[5000]={0},step=0,spf[5000]={0},num[5000]={0};
cin>>C>>L;
for(int i=0;i<C;i++)
{
scanf("%d %d",&min1[i],&max1[i]);
}
for(int i=0;i<L;i++)
{
scanf("%d %d",&spf[i],&num[i]);
}
paixu(min1,max1);
paixu(spf,num);
for(int i=0;i<L;i++)
{
for(int j=0;j<C;j++)
{
if(spf[i]>=min1[j]&&spf[i]<=max1[j]&&num[i]>0&&min1[j]!=0&&max1[j]!=0)
{
step++;
num[i]--;
min1[j]=0;
max1[j]=0;
}
}
}
cout<<step<<endl;
return 0;
}
题目 Radar Installation
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
Figure A Sample Input of Radar Installations
输入
The input consists of several test cases.
The first line of each case contains two integers n(1≤n≤1000)and d(1≤d≤200), where n is the number of islands in the sea and d is the distance of coverage of the radar installation.
This is followed by n lines each containing two integers representing the coordinate of the position of each island. (-1000≤x,y≤1000)
Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
输出
For each test case output one line of the minimal number of radar installations needed. “-1” installation means no solution for that case.
样例输入 Copy
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
样例输出 Copy
2
1
思路:
可通过计算算出能够管辖每个建筑物的在x轴上的区间 l [ i ]~ r [ i ],问题转化为:给定N个区间,在x轴上放置最少的点,使每个区间包含至少一个点。
将每个区间的左端点 l[ i ] 从小到大排序,用pos表示已经放好的最后一台监控设备的坐标。依次考虑每个区间。如果当前的 l[ i ]大于pos,则多放一台设备,pos=r[ i ].否则就仍让最后一台已经安防的设备来管理当前区间,pos=min(r[ i ],pos)。
这里要注意l和r数组,都是double型的!!!
#include <bits/stdc++.h>
using namespace std;
int n,d,step=0;
double l[1005]={0},r[1005]={0};
struct node{
int x;
int y;
}q[1005];
double paixu(double a[],double b[])
{
double temp,temp1;
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp1=b[j];
b[j]=b[j+1];
b[j+1]=temp1;
}
}
}
double tanxin()
{
double pos=-5000;
for(int i=0;i<n;i++)
{
if(pos<l[i])
{
step++;
pos=r[i];
}
else
pos=min(r[i],pos);
}
return step;
}
int main()
{
int ans=0;
while(cin>>n>>d&&n&&d)
{
for(int i=0;i<n;i++)
{
scanf("%d %d",&q[i].x,&q[i].y);
l[i]=q[i].x-sqrt(d*d-q[i].y*q[i].y);
r[i]=q[i].x+sqrt(d*d-q[i].y*q[i].y);
}
for(int i=0;i<n;i++)
{
if(q[i].y<(-1*d)||q[i].y>d)
{
ans=-1;
cout<<"-1"<<endl;
break;
}
else if(n==1&&q[i].y<=d&&q[i].y>=(-1*d))
{
ans=1;
cout<<"1"<<endl;
break;
}
}
if(ans!=-1&&ans!=1){
paixu(l,r);
cout<<tanxin()<<endl;
}
}
return 0;
}