牛客小白月赛94
A
模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <bits/stdc++.h> using namespace std; int b[100 ]; int a[100 ]; int main () { for (int i=1 ;i<=9 ;i++) { cin>>a[i]; } string s; cin>>s; for (int i=1 ;i<=s.length ();i++) { cout<<a[int (s[i-1 ]-'0' )]; } }
B
全一样就是0,不然可以是n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <bits/stdc++.h> using namespace std;const int N=1e6 +100 ;int a[N];int b[N];int main () { int n; cin>>n; for (int i=1 ;i<=n;i++) { cin>>a[i]; b[i]=a[i]; } sort (a+1 ,a+1 +n); for (int i=1 ;i<=n;i++) { if (a[i]!=b[i]) { cout<<n<<'\n' ; return 0 ; } } cout<<0 <<'\n' ; }
C
找一个数字定住,直接作为最小值,其他就算两边的最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <bits/stdc++.h> using namespace std;using ll=long long ;const int N=1e6 +100 ;ll a[N]; ll sum[N]; int main () { int n; cin>>n; for (int i=1 ;i<=n;i++) { cin>>a[i]; sum[i]=sum[i-1 ]+a[i]; } ll ans=0 ; for (int i=1 ;i<=n;i++) { ans=max (ans,max (sum[n]-sum[i],sum[i-1 ]-sum[0 ])-a[i]); } cout<<ans; }
D
构造题,由于一个数字的约数没有很多,可以直接一步步加上去,但需要一个map记录,避免加太多次。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 #include <bits/stdc++.h> using namespace std; int main () { int n; cin >> n; vector<int > a (n+1 ) ; for (int i = 1 ; i <= n;i++) { cin >> a[i]; } vector<int > ans; vector<bool > vis (n+1 ) ; map<int ,int > mp; for (int i=1 ; i <= n; i++) { int t = a[i] + mp[a[i]]; while ( vis[t]) { t += a[i]; if (t > n) { cout <<-1 <<"\n" ; return 0 ; } } ans.push_back (t); vis[t] = true ; mp[a[i]] = t; if (a[i] == 1 ) { break ; } } for (int i=1 ;i<=n;i++) { if (!vis[i]) { ans.push_back (i); } } int t =0 ; for (int i= 1 ;i<=n;i++) { t = __gcd(a[i],t); if (t != a[i]) { cout <<-1 <<"\n" ; return 0 ; } } for (auto v:ans) { cout <<v<<' ' ; } cout <<"\n" ; }
E/F
按位贪心
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <iostream> #include <algorithm> #include <cstring> using namespace std;typedef long long ll;const int N = 2e5 + 10 ;const int M = 31 ;int n, k;int v[N], w[N];int main () { cin >> n >> k; for (int i = 1 ; i <= n; i ++) cin >> v[i] >> w[i]; ll sum = 0 , ans = 0 ; for (int i = M - 1 ; ~i; i --) { sum = ans | (1ll << i); ll tot = (1ll << M) - 1 ; for (int j = 1 ; j <= n; j ++) if ((w[j] & sum) == sum) tot &= v[j]; if (tot <= k) ans = sum; } cout << ans; }