Arthur and Table CodeForces, Arthur urcodeforces
Arthur and Table CodeForces-557C
First, sort by length.
The table legs with a length of p have a [p.
To make the table legs with a length of p the longest, cut down the sum {Number of legs with a length less than p}-a [p] + 1 leg at the cost. You also need to beam all the table legs longer than p. Enumerate p.
Key points (I can understand it after reading the question): the time/space complexity can be greatly reduced through a condition with a maximum of 200 energy.
Well, so... is it greedy?
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 struct Leg 5 { 6 int len,cost,len2; 7 bool operator<(const Leg& b) const 8 { 9 return len<b.len||(len==b.len&&cost<b.cost);10 }11 }l[100100];12 int l2[210];13 int sum1,n,ans=0x3f3f3f3f,now,now2,now3,sum2;14 int main()15 {16 int i,j;17 scanf("%d",&n);18 for(i=1;i<=n;i++)19 scanf("%d",&l[i].len);20 for(i=1;i<=n;i++)21 scanf("%d",&l[i].cost);22 sort(l+1,l+n+1);23 for(i=1;i<=n;i++)24 if(l[i].len!=l[i-1].len)25 l[i].len2=l[i-1].len2+1;26 else27 l[i].len2=l[i-1].len2;28 for(i=1;i<=n;i++)29 sum2+=l[i].cost;30 for(i=1;i<=n;i++)31 {32 sum2-=l[i].cost;33 now++;34 if(l[i].len2==l[i+1].len2) continue;35 now2=sum1-now+1;36 if(now2<=0)37 {38 ans=min(sum2,ans);39 }40 else41 {42 now3=0;43 for(j=0;j<=200;j++)44 {45 if(l2[j]>=now2)46 {47 now3+=j*now2;48 break;49 }50 now2-=l2[j];51 now3+=j*l2[j];52 }53 ans=min(now3+sum2,ans);54 }55 for(j=i;l[j].len2==l[j-1].len2;j--)56 l2[l[j].cost]++;57 l2[l[j].cost]++;58 sum1+=now;59 now=0;60 }61 printf("%d",ans);62 return 0;63 }