【POJ-2586】Y2K Accounting Bug
题目:http://poj.org/problem?id=2586
解题:关键在于读懂题目,每5个月出一次报告(一共8次,这里的8就是下一句里面的!),每一次报告的时候都是亏损的。因此要看最后是否盈利,就是每5个月的情况,可以列举(一共有5种),我想的是确定前5个月的,那么6-10月和他对称,只需要判断最后两个月的钱。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
using namespace std;
int main() {
int months[13] = {0};
int month = 0;
int s, d;
while (cin >> s >> d) {
d = -d;
month = s * 5;
for (int i = 1; i <= 5; i++) {
months[i] = s;
months[5 + i] = months[i];
}
int i = 5;
while (month > 0) {
month -= s;
month += d;
months[i] = d;
months[5 + i] = months[i];
i--;
}
int sum = months[8] + months[9] + months[10];
for (int i = 11; i <= 12; i++) {
if (sum + s <= 0) {
sum += s;
months[i] = s;
}else {
sum += d;
months[i] = d;
}
}
month = month * 2 + months[11] + months[12];
if (month > 0) {
cout << month << endl;
}
else {
cout<< "Deficit" << endl;
}
}
}
【POJ-1328】
题目:http://poj.org/problem?id=1328
解题的关键:以小岛为圆心,雷达辐射范围为圆心建立一个圆,该圆与x轴有左、右两个交点,将小岛按照以上画的右交点在x轴上的位置从小到大排列,假定第一个雷达的位置在最左边的右交点上,之后一一遍历。如果某一个小岛为圆心的圆与x轴的右交点比另外一个小岛为圆心的圆的左交点小,那么这两个小岛一定可以被同一个雷达覆盖。如果不能,更新雷达位置为新小岛的右交点,继续。
理解!看到题目以后想到了去找最大的与x轴相交左右两个顶点,之后就想转换为区间问题,但是这个区间没有办法用int计数,所以从贪心角度出发。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node {
double left;
double right;
};
bool cmp(node i1,node i2){
return i1.right < i2.right;
}
int main() {
int n, d;
int cn = 1;
while (cin >> n >> d, n != 0 && d != 0) {
int flag = 0;
vector<node> islands(n);
for(int i=0;i<n;i++){
int x, y;
cin >> x >> y;
if (y > d) {
flag = 1;
}
islands[i].left = x - sqrt(d * d - y * y);
islands[i].right = x + sqrt(d * d - y * y);
}
if (flag == 1) {
cout << "Case " << cn << ": -1" << endl;
}else {
sort(islands.begin(), islands.end(), cmp);
int rn = 1;
double rpos = islands[0].right;
for (int i = 1; i < n; i++) {
if (islands[i].left <= rpos) {
continue;
}else {
rn++;
rpos = islands[i].right;
}
}
cout << "Case " << cn << ": " <<rn<< endl;
}
cn++;
}
}
还有一个踩雷地点就是我的rpos(雷达的位置按照习惯写了int),int和double表示的位置肯定有差别,所以导致第一次WA了