CodeForces 158B Taxi(贪心)
贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了.
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main() { int n; int tot[5],num; while(~scanf("%d",&n)) { memset(tot,0,sizeof(tot)); for(int i = 0;i < n;i++) { scanf("%d",&num); tot[num]++; } int ans = tot[4]; ans += tot[3]; tot[1] -= tot[3]; int tmp1 = ans += tot[2]/2; if(tot[2] % 2 == 1) { tot[1]-=2; ans++; } if(tot[1] > 0) ans += tot[1]%4==0 ?tot[1]/4:tot[1]/4 + 1; printf("%d ",ans); } }