2019.3.27

1.贪心:最优队列

http://poj.org/problem?id=3253

倒推,最小的两个和在一起,加入队列(模拟倒割)

#include<iostream>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
int f[20005];
priority_queue<int, vector < int > ,greater < int > > q;
int main()
{
  int n;
  cin>>n;
  LL ans=0;
  for(int i=0;i<n;i++)
  {
     cin>>f[i];
     q.push(f[i]);
  }
  while(q.size()>1)
   {
       int a=q.top();
       q.pop();
       int b=q.top();
       q.pop();
       ans+=a+b;
       q.push(a+b);
   }
  cout<<ans<<endl;
  return 0;
}
POJ 3253

2.贪心:

http://acm.hdu.edu.cn/showproblem.php?pid=2037

按开始时间顺序排,然后查找从当前时间到其结束时间内有没有完整节目,没有加入答案,并且用now表示该事件的结束时间,让下一个数一直跳到now为止。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<cstring>
 5 #define LL long long
 6 #define mem(a) memset(a,0,sizeof(a))
 7 using namespace std;
 8 int f[20005];
 9 struct node{
10 int x,y;
11 }g[105],p;
12 int be[30],ed[30];
13 int main()
14 {
15   int n;
16   while(cin>>n&&n)
17   {
18      mem(be);
19      int ans=0;
20    for(int i=0;i<n;i++)
21    {
22      cin>>g[i].x>>g[i].y;
23    }
24    for(int i=0;i<n-1;i++)
25    {
26        for(int j=i+1;j<n;j++)
27        {
28            if(g[i].x>g[j].x)
29            {
30               p=g[i];
31               g[i]=g[j];
32               g[j]=p;
33            }
34        }
35    }
36    int now=0;
37    for(int i=0;i<n;i++)
38    {
39       if(g[i].x<now) continue;
40        int t=0;
41      for(int j=i+1;j<n;j++)
42      {
43        if(g[j].y<=g[i].y)
44        t++;
45        else if(g[j].y>g[i].y)
46        {
47            if(t)
48            break;
49        }
50      }
51      if(t==0)
52      {
53        ans++;
54        now=g[i].y;
55      }
56    }
57    cout<<ans<<endl;
58   }
59   return 0;
60 }
今年暑假不AC

3.贪心:

写的很麻烦,找到大于y和小于y的

https://vjudge.net/problem/161026/origin

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<cstring>
 5 #define LL long long
 6 #define mem(a) memset(a,0,sizeof(a))
 7 using namespace std;
 8 int f[1500],g[1005];
 9 int main()
10 {
11   int n,k,p,x,y,b=0,s=0,sum=0;
12   cin>>n>>k>>p>>x>>y;
13   for(int i=1;i<=k;i++)
14    {
15       cin>>f[i];
16       sum+=f[i];
17       if(f[i]>y)b++;
18       else if(f[i]<y) s++;
19    }
20   int t=n-k,v=0;
21   if(sum+t>x) {cout<<"-1"<<endl;return 0;}
22   if(s>=n/2+1) {cout<<"-1"<<endl;return 0;}
23   if(b>n/2) {
24     for(int i=1;i<=t;i++)
25         cout<<"1 ";
26     return 0;
27   }
28   int m=k-s-b;
29   if(m+b>n/2)
30   {
31     for(int i=1;i<=t;i++)
32         cout<<"1 ";
33      sum+=t;
34   }
35 
36   else
37   {
38       for(int i=1;i<=(1+n)/2-b-m;i++)
39       {
40           g[++v]=y;
41           sum+=y;
42       }
43       for(int i=1;i<=n/2-s;i++)
44      {
45         g[++v]=1;
46         sum+=1;
47      }
48   }
49 
50   if(sum>x) {cout<<"-1"<<endl;return 0;}
51   for(int i=1;i<=v;i++)
52     cout<<g[i]<<" ";
53 
54   return 0;
55 }
school marks

 

posted @ 2019-03-29 18:38  XXrl  阅读(139)  评论(0编辑  收藏  举报