poj 3190 挤奶预订
大意:
给出一些线段,求最少分组使线段互不重叠,并求出每条线段所在组的编号
思路:
看到了一篇好文章:《浅谈信息学竞赛中的区间问题》
此问题应该属于里面的第二类——多个资源调度问题
跟线段覆盖是不一样的,一开始想错了
对于每条线段,无论放在哪里,末尾对后面线段的影响是一定的
所以当前如果有末端点与改线段始端点不重合的线段那么放就好了
如果没有,就新建分组。
每次处理新线段时,从以末端点的小根堆中弹出堆顶
若能更新,弹出原堆顶,压入新线段
若不能,新建分组。
可保证堆中每条线段时刻代表该线段所在分组的最末端点
//挤奶预定
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 50;
struct zt{
LL l,r,num;
}L[MAXN << 1];
bool cmp(zt a,zt b){
return a.l < b.l;
}
bool cmp2(zt a,zt b){
return a.num < b.num;
}
LL n;
int num[MAXN],tot,last[MAXN];
struct em{
LL l,r,num;
LL loc;
};
bool operator < (em a,em b){
return a.r > b.r;
}
priority_queue <em> q;
int id[MAXN];
int cnt;
int main(){
scanf("%lld",&n);
for(int i = 1;i <= n;i ++){
scanf("%lld%lld",&L[i].l,&L[i].r);
L[i].num = i;
}
sort(L + 1,L + n + 1,cmp);
for(int i = 1;i <= n;i ++){
if(q.empty()){
tot ++;
num[L[i].num] = tot;
last[tot] = L[i].r;
q.push((em){L[i].l,L[i].r,L[i].num,num[L[i].num]});
continue;
}
em u = q.top();
if(u.r < L[i].l){
q.pop();
last[u.loc] = L[i].r;
num[L[i].num] = u.loc;
q.push((em){L[i].l,L[i].r,L[i].num,u.loc});
}
else {
tot ++;
num[L[i].num] = tot;
last[tot] = L[i].r;
q.push((em){L[i].l,L[i].r,L[i].num,num[L[i].num]});
}
}
printf("%d\n",tot);
sort(L + 1,L + n + 1,cmp2);
for(int i = 1;i <= n;i ++)
{
printf("%d\n",num[i]);
}
}
poj3040津贴
同codevs2169零用钱
//津贴
#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
struct zt
{
LL v,b;
}M[100010];
LL n,c;
LL cnt,a,b,ans;
bool cmp(zt a,zt b)
{
return a.v > b.v;
}
int main()
{
scanf("%lld%lld",&n,&c);
for(int i = 1;i <= n;i ++)
{
scanf("%lld%lld",&a,&b);
if(a >= c)ans += b;
else{cnt++;M[cnt].v = a;M[cnt].b = b;}
}
n = cnt;
sort(M + 1,M + n + 1,cmp);
while(true)
{
LL temp = c;
for(int i = 1;i <= n;i ++)
if(M[i].b && temp - M[i].v >= 0){
while(temp - M[i].v >= 0 && M[i].b > 0){
temp -= M[i].v;
M[i].b --;
}
}
if(temp > 0){
for(int i = n;i >= 1;i --){
if(M[i].b){
while(M[i].b && temp > 0){
temp -= M[i].v;
M[i].b --;
}
if(temp <= 0)break;
}
}
}
if(temp > 0)break;
ans ++;
}
printf("%lld",ans);
}
poj3253篱笆
同codevs合并果子
//篱笆
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
priority_queue<int,vector<int>,greater<int> > q;
int n,a[100010];
long long ans;
int u,t;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i ++){
scanf("%d",&a[i]);
q.push(a[i]);
}
while(!q.empty())
{
int u = q.top();
q.pop();
if(q.empty())break;
int t = q.top();
q.pop();
ans += u + t;
q.push(u + t);
}
printf("%lld",ans);
return 0;
}
poj3262保护花朵
思路:对于A、B之后的奶牛,不管A、B顺序如何,吃掉的花一定为(A.T+B.T)*sum.D
所以为A、B确定一个毁坏花最少的顺序就好了
若假定先赶走A优于先赶走B,则可得2*A.T*B.D < 2*B.T*A.D
约分得A.T/A.D < B.T/B.D
精度long double 就够了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define LD long double
using namespace std;
const int MAXN = 100000 + 50;
struct zt
{
LL T,D;
LD v;
}l[MAXN];
int n;
bool cmp(zt a,zt b)
{
return a.v < b.v;
}
LL sum[MAXN],ans;
int main()
{
scanf("%d",&n);
for(int i = 1;i <= n;i ++)
{
scanf("%lld%lld",&l[i].T,&l[i].D);
l[i].v = (LD)(((LD)l[i].T)/((LD)l[i].D));
}
sort(l + 1,l + n + 1,cmp);
for(int i = n;i >= 1;i --)
{
sum[i] = sum[i + 1] + l[i].D;
}
for(int i = 1;i < n;i ++)
{
ans += l[i].T*sum[i + 1];
}
printf("%lld",ans*2);
return 0;
}
暂时这些
待更新