img
牛客周赛 Round 10
A
O(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 26 27 28 29 30 31 32 33 #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main () { int n; cin >> n; vector<int > num (n) ; for (int i = 0 ; i < n; i++) { cin >> num[i]; } vector<int > dp (n, 1 ) ; int ans = 1 ; for (int i = 1 ; i < n; i++) { if (abs (num[i] - num[i-1 ]) <= 1 ) { dp[i] = dp[i - 1 ] + 1 ; ans = max (ans, dp[i]); } } cout << ans; return 0 ; }
B
还是那个排列函数
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 #include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std;const int N = 15 ;int a[N];string s; signed main () { ios::sync_with_stdio (false ); cin.tie (0 ); cout.tie (0 ); cin>>s; int n = s.size (); for (int i=0 ; i<n; i++) a[i] = s[i] - 'a' ; sort (a, a+n); int res = 0 ; do { bool flag = true ; for (int i=1 ; i<n; i++) { if (a[i]==a[i-1 ]) { flag = false ; break ; } } if (flag) res++; }while (next_permutation (a, a+n)); cout<<res<<endl; return 0 ; }
C
计算得到
\(z = y / (v + x*t) + t\)
该函数先单调递减,然后单调递增
三分!!
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 100010 ;const double INF = 1e6 +10 ;double v0,x,y; double time (double t) { return y / (v0 + t * x) + t; } signed main () { ios::sync_with_stdio (false ); cin.tie (0 ),cout.tie (0 ); cin >> v0 >> x >> y; double l = 0 ,r = INF; for (int i=0 ;i<100 ;i++) { double mid = (r - l) / 3.0 ; double mid1 = l + mid; double mid2 = r - mid; if (time (mid1)>time (mid2)) l = mid1; else r = mid2; } printf ("%.7lf" ,time (l)); return 0 ; }
求导O(1)也可以做
自己求()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <bits/stdc++.h> using namespace std; typedef long long LL; void solve () { long double v0,x,y; cin >> v0 >> x >> y; if (v0 > sqrt (x*y)) printf ("%.12llf" ,y/v0); else printf ("%.12llf" ,2 *sqrt (y/x) - v0/x); } int main () { ios::sync_with_stdio (0 ),cin.tie (0 ),cout.tie (0 ); int t = 1 ; while (t--) solve (); return 0 ; }
D
本题由于连续的一定是满足连续的情况的
有一个结论就是n的字符串一定是有n*(n+1)/2个回文串的
接下来只需要遍历左右就可以了
注意加上min和break
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 #include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i <= n; i++) #define frep(i, a, n) for (int i = a; i >= n; i--) using ll = long long ;using namespace std;#define int long long const int N=1e6 +100 ;int mod=1e9 +7 ;int a[N];signed main () { int n; cin>>n; int ans=0 ; rep (i,1 ,n) { cin>>a[i]; ans=(ans+(1 +a[i])*a[i]/2 )%mod; } rep (i,2 ,n) { int l=i-1 ; int r=i+1 ; while (l>=1 &&r<=n) { ans=(ans+min (a[l],a[r]))%mod; if (a[l]!=a[r]) { break ; } l--; r++; } } cout<<ans; return 0 ; }
img