CF1316E-Team Building(dp+贪心)
题意:https://codeforces.com/contest/1316/problem/E?csrf_token=0963fae4dc3b2bb886fa535d67322918
思路:
首先,如果告诉你p+k==n是很好做的,简单dp
但是p+k可能<n,这就要删掉一些观众。v
解:dp【i】【mark】的mark就是二进制表示那几个位置已经有人上去比赛了,比如我现在这个人要站2号位,他会从000,001,100,101更新上来,更新为010,011,110,111。
就是先把观众排序,从大到小能当观众就去当观众,就是最优的(这个就是试着去更新dp最大值)见85~90行
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\\Input.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 strcat 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 <cassert> 21 #include <iomanip> 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 25 //****************** 26 clock_t __START,__END; 27 double __TOTALTIME; 28 void _MS(){__START=clock();} 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 30 //*********************** 31 #define rint register int 32 #define fo(a,b,c) for(rint a=b;a<=c;++a) 33 #define fr(a,b,c) for(rint a=b;a>=c;--a) 34 #define mem(a,b) memset(a,b,sizeof(a)) 35 #define pr printf 36 #define sc scanf 37 #define ls rt<<1 38 #define rs rt<<1|1 39 typedef pair<int,int> PII; 40 typedef vector<int> VI; 41 typedef unsigned long long ull; 42 typedef long long ll; 43 typedef double db; 44 const db E=2.718281828; 45 const db PI=acos(-1.0); 46 const ll INF=(1LL<<60); 47 const int inf=(1<<30); 48 const db ESP=1e-9; 49 const int mod=(int)1e9+7; 50 const int N=(int)1e5+10; 51 52 struct node 53 { 54 int x; 55 int v[10]; 56 friend bool operator<(node a,node b) 57 { 58 return a.x>b.x; 59 } 60 }a[N]; 61 ll dp[N][150]; 62 63 int clac(int x) 64 { 65 int cnt=0; 66 while(x>0) 67 { 68 if(x&1)cnt++;x/=2; 69 } 70 return cnt; 71 } 72 73 int main() 74 { 75 int n,p,k; 76 sc("%d%d%d",&n,&p,&k); 77 for(int i=1;i<=n;++i)sc("%d",&a[i].x); 78 for(int i=1;i<=n;++i) 79 for(int j=1;j<=p;++j) 80 sc("%d",&a[i].v[j]); 81 sort(a+1,a+1+n); 82 int top=(1<<p)-1; 83 for(int i=1;i<=n;++i) 84 { 85 for(int j=0;j<=top;++j)//当观众 or 舍弃 86 { 87 dp[i][j]=dp[i-1][j]; 88 if(i-clac(j)<=k&&clac(j)<i) 89 dp[i][j]+=a[i].x; 90 } 91 for(int j=0;j<=top;++j)//当参赛者 92 { 93 if(clac(j)>=i)continue; 94 for(int pos=1;pos<=p;++pos) 95 { 96 if((j&(1<<(pos-1)))==0) 97 { 98 dp[i][j|(1<<(pos-1))]=max(dp[i][j|(1<<(pos-1))],dp[i-1][j]+a[i].v[pos]); 99 } 100 } 101 } 102 } 103 104 pr("%lld\n",dp[n][top]); 105 return 0; 106 } 107 108 /**************************************************************************************/