版本比较
666
正文
增加 86 行删除 66 行
12345612389101112131445678910161112131819202122232425141516171819202128292223242526272829313233343536373031323334353637383940414240414243444546474849505152535455564344454647484950515859606162636452535455666768697071727374565758596061626364656667686970717276777374757677787980818283848586878889909192939495969798#include <iostream>#include <algorithm>#include <cmath>#include <set>#include <vector>#include <stack>#include<bits/stdc++.h>#define int long longusing namespace std;long long n, s, dp[600][600], ans;struct node { long long w, x;}a[600];bool cmp(node s1, node s2) { if (s1.w == s2.w) { return s1.x < s2.x;int n,m,tzn;int a[300005];void teyi(){ int sum=-1e15; for(int i=1;i<=n;i++){ sum=max(sum,a[i]); } return s1.w < s2.w; cout<<sum; return;}int path[600][600];vector<int> sel;stack<int> st;int main() { cin >> n >> s; for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j] = 0;void teer(){ int sum=-1e15; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j)continue; sum=max(sum,a[i]&a[j]); } } for (int i = 1; i <= n; i++) { cin >> a[i].w; cout<<sum; return;}void tesan(){ int sum=a[1]; for(int i=2;i<=n;i++){ sum&=a[i]; } for (int i = 1; i <= n; i++) { cin >> a[i].x; } sort(a + 1, a + n + 1, cmp); for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { path[i][j] = 0; cout<<sum; return;}void tesi(){ int ans=-1e15; for(int i=1;i<=n;i++){ int sum=2147483648; for(int j=1;j<=n;j++){ if(i==j)continue; sum&=a[j]; } ans=max(ans,sum); } for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { if (a[i].x > j) { dp[i][j] = dp[i - 1][j]; path[i][j] = 0; } else { if (dp[i - 1][j] >= dp[i - 1][j - a[i].x] + a[i].w) { dp[i][j] = dp[i - 1][j]; path[i][j] = 0; } else { dp[i][j] = dp[i - 1][j - a[i].x] + a[i].w; path[i][j] = 1; } } } cout<<ans; return;}void dfs(int x,int y,int z){ if(n-x+1<m-y)return; if(y==m){ tzn=max(tzn,z); return; } int i = n, j = s; while (i > 0 && j >= 0) { if (path[i][j] == 1) { sel.push_back(i); j -= a[i].x; } i--; for(int i=x+1;i<=n;i++){ dfs(i,y+1,z&a[i]); dfs(i,y,z); } reverse(sel.begin(), sel.end()); long long last_pos = -1; for (int idx : sel) { long long cur = a[idx].w; if (last_pos >= cur) { cur = last_pos + 1; } ans += cur; last_pos = cur;}void tewu(){ dfs(0,0,2147483647); cout<<tzn;}void teliu(){ dfs(0,0,2147483647); cout<<tzn;}void teqi(){ sort(a+1,a+n+1); int sum=a[n],cnt=1; for(int i=n-1;i>=1;i--){ if(cnt==m)break; cnt++; sum&=a[i]; } cout << ans; return 0; cout<<sum; return;}signed main(){ freopen("and.in","r",stdin); freopen("and.out","w",stdout); cin>>n>>m; bool flag=0,flag2=0; for(int i=1;i<=n;i++){ cin>>a[i]; if(a[i]>4)flag=1; if(a[i]!=i-1)flag2=1; } if(m==1)teyi(); else if(m==2)teer(); else if(m==n)tesan(); else if(m==n-1)tesi(); else if(n<=30){ dfs(0,0,2147483647); cout<<tzn; } else if(flag==0)tewu(); else if(flag2==0)teliu(); else teqi(); return 0;}