![]() | 1 / 6 | Problem G | POJ 2376 | Cleaning Shifts |
![]() | 1 / 2 | Problem H | POJ 1328 | Radar Installation |
![]() | 1 / 2 | Problem I | POJ 3190 | Stall Reservations |
![]() | 1 / 3 | Problem J | POJ 2393 | Yogurt factory |
![]() | 1 / 2 | Problem K | POJ 1017 | Packets |
![]() | 1 / 1 | Problem L | POJ 3040 | Allowance |
![]() | 1 / 1 | Problem M | POJ 1862 | Stripies |
![]() | 1 / 3 | Problem N | POJ 3262 | Protecting the Flowers |
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
int x;
int y;
};
bool cmp(node a, node b)
{
if(a.x==b.x) return a.y>b.y;
return a.x<b.x;
}
node a[25005];
int main()
{
int n, t;
while(~scanf("%d%d", &n, &t))
{
for(int i=0; i<n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
sort(a, a+n, cmp);
if(a[0].x>1)
{
printf("-1\n"); continue;
}
int id=a[0].y;
int cnt=1;
int temp=0;
int max;
for(int i=1; i<n; i++)
{
if(id>=t)
{
break;
}
if(a[i].y<=id) continue;
if(a[i].x>id+1)
{
temp=1;
break;
}
if(a[i].x<=id+1)
{
max=a[i].y;
for(int j=i+1; j<n; j++)
{
if(a[j].x>id+1)break;
if(a[j].y>max)
{
max=a[j].y;
}
}
id=max;
cnt++;
}
}
if(temp==1 || id<t)
printf("-1\n");
else
printf("%d\n", cnt);
}
return 0;
}
POJ 1328 取圆与X轴交点,再贪心。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct point{
double x;
double y;
};
bool cmp(point a, point b)
{
return a.x<b.x;
}
point a[1005];
int n, d;
int main()
{
int t=1;
int xx, yy;
while(~scanf("%d%d", &n, &d))
{
if(n==0 &&d==0) break;
int tmp=0;
for(int i=0; i<n; i++)
{
scanf("%d%d", &xx, &yy);
if(yy>d || d<0)
{
tmp=1;
}
a[i].x=xx-sqrt(1.0*d*d-1.0*yy*yy);
a[i].y=xx+sqrt(1.0*d*d-1.0*yy*yy);
}
if(tmp)
{
printf("Case %d: -1\n", t++);continue;
}
sort(a, a+n, cmp);
int ans=1;
double temp=a[0].y;
for(int i=1; i<n; i++)
{
if(a[i].y<=temp)
temp=a[i].y;
else if(a[i].x>temp)
{
ans++;
temp=a[i].y;
}
}
printf("Case %d: %d\n", t++, ans);
getchar();
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct cow{
int st;
int ed;
int p;
};
cow a[50005];
int num[50005];
bool operator <(cow a, cow b)
{
return a.ed>b.ed;
}
bool cmp(cow a, cow b)
{
return a.st<b.st;
}
priority_queue<cow> q;
int n;
int main()
{
while(~scanf("%d", &n))
{
for(int i=1; i<=n; i++)
{
scanf("%d%d", &a[i].st, &a[i].ed);
a[i].p=i;
}
sort(a+1, a+1+n, cmp);
int barn=1;
while(!q.empty())q.pop();
q.push(a[1]);
num[a[1].p]=barn;
for(int i=2; i<=n; i++)
{
cow temp = q.top();
if(temp.ed<a[i].st)
{
q.pop();
q.push(a[i]);
num[a[i].p]=num[temp.p];
}
else
{
q.push(a[i]);
num[a[i].p]=++barn;
}
}
printf("%d\n", barn);
for(int i=1; i<=n; i++)
printf("%d\n", num[i]);
}
return 0;
}
#include<cstdio>
#include<algorithm>
using namespace std;
int c[10005], y[10005];
int n, s;
long long ans;
int main()
{
while(~scanf("%d%d", &n, &s))
{
for(int i=0; i<n; i++)
scanf("%d%d", &c[i], &y[i]);
for(int i=1; i<n; i++)
c[i]=min(c[i-1]+s, c[i]);
ans=0;
for(int i=0; i<n; i++)
ans+=c[i]*y[i];
printf("%lld\n", ans);
}
return 0;
}
POJ 1017 手动模拟
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[6], t, m;
while(~scanf("%d%d%d%d%d%d", &a[0],&a[1],&a[2],&a[3],&a[4],&a[5]), a[0]|a[1]|a[2]|a[3]|a[4]|a[5])
{
int ans=0;
t=0;
ans+=a[3]+a[4]+a[5];
a[0]=max(a[0]-a[4]*11, 0);
t=a[3]*5-a[1];
if(t>0)
{
a[1]=0;
a[0]=max(a[0]-t*4, 0);
}
else
{
a[1]=a[1]-a[3]*5;
}
if(a[2]%4==0)
{
ans+=a[2]/4;
}
else
{
ans+=a[2]/4+1;
t=a[2]%4;
if(t==1)
{
m=a[1]-5;
if(m>=0)
a[1]-=5;
else
{
a[1]=0;
a[0]=max(a[0]+m*4,0);
}
a[0]=max(a[0]-7, 0);
}
else if(t==2)
{
m=a[1]-3;
if(m>=0)
a[1]-=3;
else
{
a[1]=0;
a[0]=max(a[0]+m*4, 0);
}
a[0]=max(a[0]-6, 0);
}
else
{
m=a[1]-1;
if(m>=0)
a[1]-=1;
else
{
a[1]=0;
a[0]=max(a[0]+4*m, 0);
}
a[0]=max(a[0]-5, 0);
}
}
if(a[1]%9==0)
ans+=a[1]/9;
else
{
ans+=a[1]/9+1;
t=a[1]%9;
a[0]=max(a[0]-(9-t)*4, 0);
}
if(a[0]%36==0)
ans+=a[0]/36;
else
ans+=a[0]/36+1;
printf("%d\n", ans);
}
return 0;
}
POJ 3040 求出当前最佳方案
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
struct state{
int v;
int b;
};
bool cmp(state a, state b)
{
return a.v<b.v;
}
state a[21];
int need[21];
int main()
{
int n, c;
while(~scanf("%d%d", &n, &c))
{
for(int i=0; i<n; i++)
scanf("%d%d", &a[i].v, &a[i].b);
sort(a, a+n, cmp);
int ans=0, lim=-1;
for(int i=n-1; i>=0; i--)
if(a[i].v>=c) ans+=a[i].b;
else
{
lim=i;
break;
}
while(1)
{
memset(need, 0, sizeof(need));
int res=c, ti;
for(int i=lim; i>=0; i--)
{
if(!a[i].b||!res) continue;
ti=res/a[i].v;
ti=min(ti,a[i].b);
need[i]=ti;
res-=ti*a[i].v;
}
if(res)
{
for(int i=0; i<=lim; i++)
{
if(a[i].v>=res && (a[i].b>need[i]))
{
need[i]++;
res=0;
break;
}
}
if(res) break;
}
int day=99999999;
for(int i=0; i<=lim; i++)
if(need[i])
day=min(day, a[i].b/need[i]);
ans+=day;
for(int i=0; i<=lim; i++)
a[i].b-=day*need[i];
}
printf("%d\n", ans);
}
}
POJ 1862 给出2种思路, 一种是利用直接求出的公式W=2sqrt(2sqrt(...)) ( (m1m2)^(0.5^(n-1))m3^(0.5^(n-2))...mn^0.5)
n-1
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a>b;
}
int main()
{
int n;
double a[105];
while(~scanf("%d", &n))
{
for(int i=0; i<n; i++)
scanf("%lf", &a[i]);
if(n==1)
{
printf("%.3lf\n", a[0]);
continue;
}
double ans=2;
sort(a, a+n, cmp);
for(int i=2; i<n; i++)
{
ans=sqrt(ans);
ans*=2;
}
a[0]=pow(a[0], pow(0.5, n-1));
for(int i=1; i<n; i++)
a[i]=pow(a[i], pow(0.5, n-i));
for(int i=0; i<n; i++)
ans*=a[i];
printf("%.3lf\n", ans);
}
return 0;
}
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a>b;
}
int main()
{
int n;
double a[105];
while(~scanf("%d", &n))
{
for(int i=0; i<n; i++)
scanf("%lf", &a[i]);
if(n==1)
{
printf("%.3lf\n", a[0]);
continue;
}
double ans=1;
sort(a, a+n, cmp);
a[1]=2*sqrt(a[0]*a[1]);
for(int i=2; i<n; i++)
a[i]=2*sqrt(a[i]*a[i-1]);
printf("%.3lf\n", a[n-1]);
}
return 0;
}
选择策略, 1 在二个中间选择之中,能根据time/eat小的那个为最优解 证明: 二个牛中 A,B,属性分别为分别为eatA,timeA,eatB,timeB 选A的时候损失timeA*eatB 选B的时候损失timeB*eatA 双方同除以eatA*eatB. 令time/eat为一个羊的比率x 可以证明x小的那个为最优解.
#include<cstdio>
#include<algorithm>
using namespace std;
struct cow{
int d;
int t;
double p;
};
bool cmp(cow a, cow b)
{
return a.p<b.p;
}
cow a[100005];
long long time[100005];
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i=0; i<n; i++)
{
scanf("%d%d", &a[i].t, &a[i].d);
a[i].p=a[i].t*1.0/a[i].d;
}
sort(a, a+n, cmp);
long long ans=0;
time[0]=0;
for(int i=1; i<n; i++)
time[i]=2*a[i-1].t+time[i-1];
for(int i=0; i<n; i++)
ans+=a[i].d*time[i];
printf("%lld\n", ans);
}
return 0;
}