当前位置:   article > 正文

小a与星际探索_c++星际探险家在其最新的宇宙探险中遭遇了一系列新的挑战。此次探险中,探险家的飞

c++星际探险家在其最新的宇宙探险中遭遇了一系列新的挑战。此次探险中,探险家的飞

https://ac.nowcoder.com/acm/contest/317/C

C++版本一

题解:DP简单题

hacked

只能过95%左右

  1. /*
  2. *@Author: STZG
  3. *@Language: C++
  4. */
  5. #include <bits/stdc++.h>
  6. #include<iostream>
  7. #include<algorithm>
  8. #include<cstdlib>
  9. #include<cstring>
  10. #include<cstdio>
  11. #include<string>
  12. #include<vector>
  13. #include<bitset>
  14. #include<queue>
  15. #include<deque>
  16. #include<stack>
  17. #include<cmath>
  18. #include<list>
  19. #include<map>
  20. #include<set>
  21. //#define DEBUG
  22. #define RI register int
  23. using namespace std;
  24. typedef long long ll;
  25. //typedef __int128 lll;
  26. const int N=100000+10;
  27. const int MOD=1e9+7;
  28. const double PI = acos(-1.0);
  29. const double EXP = 1E-8;
  30. const int INF = 0x3f3f3f3f;
  31. ll t,n,m,k,q;
  32. ll ans,cnt,flag,temp;
  33. ll a[N];
  34. ll dp[N];
  35. char str;
  36. int main()
  37. {
  38. #ifdef DEBUG
  39. freopen("input.in", "r", stdin);
  40. //freopen("output.out", "w", stdout);
  41. #endif
  42. //scanf("%d",&n);
  43. //scanf("%d",&t);
  44. //while(t--){}
  45. scanf("%lld",&n);
  46. for(int i=1;i<=n;i++){
  47. scanf("%lld",&a[i]);
  48. }
  49. dp[1]=a[1];
  50. flag=1;
  51. for(int i=2;i<=n;i++){
  52. for(int j=1;j<i;j++){
  53. if(a[i]<a[j]&&dp[j])
  54. dp[i]=max(dp[i],dp[j]^a[i]);
  55. }
  56. }
  57. if(dp[n]!=0){
  58. cout << dp[n] << endl;
  59. }else{
  60. cout << -1 << endl;
  61. }
  62. //cout << "Hello world!" << endl;
  63. return 0;
  64. }

C++版本二

题解:线性基

  1. #include <bits/stdc++.h>
  2. #define lld I64d
  3. using namespace std ;
  4. inline long long Readin() {
  5. long long K = 0 , F = 1 ; char C = ' ' ;
  6. while( C < '0' or C > '9' ) F = C == '-' ? -1 : 1 , C = getchar() ;
  7. while( C <= '9' and C >= '0' ) K = ( K << 1 ) + ( K << 3 ) + C - '0' , C = getchar() ;
  8. return F * K ;
  9. }
  10. const int Bit = 12 ;
  11. int N , A[3005] , Base[Bit+5] ;
  12. inline void Insert( int Num ) {
  13. for(register int i = Bit ; --i >= 0 ; )
  14. if( 1 << i & Num )
  15. if( not Base[i] ) {
  16. Base[i] = Num ;
  17. break ;
  18. }
  19. else Num ^= Base[i] ;
  20. }
  21. inline int Query( int Num ) {
  22. for(register int i = Bit ; --i >= 0 ; )
  23. Num = max( Num , Num ^ Base[i] ) ;
  24. return Num ;
  25. }
  26. int main() {
  27. N = Readin() ;
  28. for(register int i = 0 ; ++i <= N ; A[i] = Readin() ) ;
  29. if( A[1] <= A[N] ) return not printf( "-1\n" ) ;
  30. for(register int i = 1 ; ++i < N ; )
  31. if( A[1] > A[i] and A[i] > A[N] )
  32. Insert( A[i] ) ;
  33. return not printf( "%d\n" , Query( A[1] ^ A[N] ) ) ;
  34. }

C++版本三

std

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. using namespace std;
  4. const int MAXN = 10003, INF = 1e9 + 10;
  5. void chmin(int &a, int b) {a = (a < b ? a : b);}
  6. void chmax(int &a, int b) {a = (a > b ? a : b);}
  7. int sqr(int x) {return x * x;}
  8. inline int read() {
  9. char c = getchar(); int x = 0, f = 1;
  10. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  11. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  12. return x * f;
  13. }
  14. int N, mx;
  15. bool f[MAXN][MAXN];
  16. struct Node {
  17. int id, val;
  18. bool operator < (const Node &rhs) const {
  19. return val > rhs.val;
  20. }
  21. }a[MAXN];
  22. signed main() {
  23. //freopen("a2.in", "r", stdin);
  24. //cout << (457 ^ 23);
  25. N = read();
  26. for(int i = 1; i <= N; i++) a[i].id = i, a[i].val = read(); mx = 6001;
  27. sort(a + 1, a + N + 1);
  28. for(int i = 1, flag = 1; i <= N; i++) {
  29. if(a[i].id == 1) {flag = 0, f[i][a[i].val] = 1; continue;}
  30. if(flag) continue;
  31. if(a[i].id == N) {
  32. int k = i - 1;
  33. while(k && a[i].val == a[k].val) k--;
  34. if(!k) break;
  35. for(int j = mx; j >= 0; j--) {
  36. f[i][j] |= f[k][j ^ a[i].val];
  37. if(f[i][j]) {printf("%d", j); return 0;}
  38. }
  39. break;
  40. }
  41. else if(a[i].val == a[i - 1].val) {
  42. memcpy(f[i], f[i - 1], sizeof(f[i]));
  43. }
  44. else {
  45. for(int j = 0; j <= mx; j++)
  46. f[i][j] |= (f[i - 1][j ^ a[i].val] | f[i - 1][j]);
  47. }
  48. }
  49. puts("-1");
  50. return 0;
  51. }

C++版本四

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<string.h>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<queue>
  8. #define ll long long
  9. #define pb push_back
  10. #define test printf("here!!!")
  11. using namespace std;
  12. const int mx=5000+10;
  13. const ll mod=1e9+7;
  14. int dp[mx];
  15. struct Node
  16. {
  17. int p;
  18. int id;
  19. friend bool operator <(Node a,Node b)
  20. {
  21. return a.p>b.p;
  22. }
  23. }a[mx];
  24. int maxn=-1;
  25. int n,t;
  26. int main()
  27. {
  28. scanf("%d",&n);
  29. for (int i=1;i<=n;++i)
  30. {
  31. scanf("%d",&a[i].p);
  32. a[i].id=i;
  33. }
  34. sort(a+1,a+n+1);
  35. int cp,np;
  36. for (int i=1;i<=n;++i)
  37. {
  38. if (a[i].id==1)
  39. {
  40. cp=i;
  41. }
  42. if (a[i].id==n)
  43. {
  44. np=i;
  45. }
  46. }
  47. if (cp>np) printf("-1\n");
  48. else
  49. {
  50. dp[a[cp].p]=1;
  51. for (int i=cp+1;i<=np;++i)
  52. {
  53. for (int j=0;j<=mx;++j)
  54. {
  55. if (dp[j]) dp[j^a[i].p]=a[i].id;
  56. }
  57. }
  58. int res=-1;
  59. for (int j=mx;j>=0;--j)
  60. {
  61. if (dp[j]==n)
  62. {
  63. res=j;
  64. break;
  65. }
  66. }
  67. if (res==0) printf("-1\n");
  68. else printf("%d\n",res);
  69. }
  70. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/714516
推荐阅读
相关标签
  

闽ICP备14008679号