减2或减3(很搞的贪心)2019牛客国庆集训派对day6

题意:https://ac.nowcoder.com/acm/contest/1111/D

问你先减二x次的情况下,最少减几次3。

思路:

%3不为0的要先减2,然后%3为0的要先减大的(比如9 3 3 会比3 3 9 更优)

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 //******************
 24 int abss(int a);
 25 int lowbit(int n);
 26 int Del_bit_1(int n);
 27 int maxx(int a,int b);
 28 int minn(int a,int b);
 29 double fabss(double a);
 30 void swapp(int &a,int &b);
 31 clock_t __STRAT,__END;
 32 double __TOTALTIME;
 33 void _MS(){__STRAT=clock();}
 34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 35 //***********************
 36 #define rint register int
 37 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 38 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 39 #define mem(a,b) memset(a,b,sizeof(a))
 40 #define pr printf
 41 #define sc scanf
 42 #define ls rt<<1
 43 #define rs rt<<1|1
 44 typedef long long ll;
 45 const double E=2.718281828;
 46 const double PI=acos(-1.0);
 47 //const ll INF=(1LL<<60);
 48 const int inf=(1<<30);
 49 const double ESP=1e-9;
 50 const int mod=(int)1e9+7;
 51 const int N=(int)1e6+10;
 52 
 53 int ans[N];
 54 void PR(int _[],int n)
 55 {
 56     for(int i=0;i<=n;++i)
 57         pr("%d ",_[i]);
 58     pr("
");
 59 }
 60 priority_queue<int>q,q3;
 61 int main()
 62 {
 63     int n,m;
 64     while(~sc("%d%d",&n,&m))
 65     {
 66         int sum=0;
 67         while(!q.empty())q.pop();
 68         while(!q3.empty())q3.pop();
 69         for(int i=1;i<=n;++i)
 70         {
 71             int tt;
 72             sc("%d",&tt);
 73             sum+=(tt+2)/3;
 74             if(tt%3==0)
 75                 q3.push(tt);
 76             else if(tt%3==1)
 77                 q.push(tt);
 78             else if(tt%3==2)
 79                 q.push(tt);
 80         }
 81         ans[0]=sum;
 82         for(int i=1;i<=m;++i)
 83         {
 84             bool f=0;
 85             if(!q.empty())
 86             {
 87                 f=1;
 88                 int tt=q.top();q.pop();
 89                 sum--;
 90                 if(tt-2>0)
 91                 {
 92                     tt-=2;
 93                     if(tt%3==0)
 94                         q3.push(tt);
 95                     else
 96                         q.push(tt);
 97                 }
 98             }
 99             if(!f)
100             {
101                 if(!q3.empty())
102                 {
103                     int tt=q3.top();q3.pop();
104                     q.push(tt-2);
105                 }
106             }
107             ans[i]=sum;
108         }
109         ll ANS=0;
110     //    PR(ans,m);
111         for(int i=0;i<=m;++i)
112             ANS+=ans[i],ANS%=mod;
113         pr("%lld
",ANS);
114     }
115     return 0;
116 }
117 
118 /**************************************************************************************/
119 
120 int maxx(int a,int b)
121 {
122     return a>b?a:b;
123 }
124 
125 void swapp(int &a,int &b)
126 {
127     a^=b^=a^=b;
128 }
129 
130 int lowbit(int n)
131 {
132     return n&(-n);
133 }
134 
135 int Del_bit_1(int n)
136 {
137     return n&(n-1);
138 }
139 
140 int abss(int a)
141 {
142     return a>0?a:-a;
143 }
144 
145 double fabss(double a)
146 {
147     return a>0?a:-a;
148 }
149 
150 int minn(int a,int b)
151 {
152     return a<b?a:b;
153 }
原文地址:https://www.cnblogs.com/--HPY-7m/p/11638147.html