You are given a set of Pi=(i,0).
The given points are vertices of a plot of a piecewise function. The PjPj+1.
In one move you can increase the 1. Note that the corresponding segments also change.
For example, the following plot shows a function for P5 one time:
Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).
Let the height of the plot be the maximum P0,P1,…,P2n). The height of the plot on the picture above is 3.
Your problem is to say which minimum possible height can have the plot consisting of k. Note that it is unnecessary to minimize the number of moves.
It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 1018.
The first line of the input contains two integers 1≤n,k≤1018) — the number of vertices in a plot of a piecewise function and the area we need to obtain.
Print one integer — the minimum possible height of a plot consisting of 1018.
4 3
1
4 12
3
999999999999999999 999999999999999986
1
One of the possible answers to the first example:
The area of this plot is 3, the height of this plot is 1.
There is only one possible answer to the second example:
The area of this plot is 12, the height of this plot is 3.
1 #include <bits/stdc++.h> 2 #define ll long long int 3 using namespace std; 4 5 ll n,m; 6 int main(){ 7 cin>>n>>m; 8 ll ans = m/n; 9 ans += m%n==0?0:1; 10 cout<<ans<<endl; 11 return 0; 12 }
Mikhail walks on a Cartesian plane. He starts at the point (0,0), he can go to any of the following points in one move:
- (1,0);
- (1,1);
- (0,1);
- (−1,1);
- (−1,0);
- (−1,−1);
- (0,−1);
- (1,−1).
If Mikhail goes from the point diagonal move.
Mikhail has ki moves.
Note that Mikhail can visit any point any number of times (even the destination point!).
The first line of the input contains one integer 1≤q≤104) — the number of queries.
Then y-coordinate of the destination point of the query and the number of moves in the query, correspondingly.
Print diagonal moves among all possible movements.
3
2 2 3
4 3 7
10 1 9
1
6
-1
One of the possible answers to the first test case: (0,0)→(1,0)→(1,1)→(2,2).
One of the possible answers to the second test case: (0,0)→(0,1)→(1,2)→(0,3)→(1,4)→(2,3)→(3,2)→(4,3).
In the third test case Mikhail cannot reach the point (10,1) in 9 moves.
仔细分析就出来了。
1 #include <bits/stdc++.h> 2 #define ll long long int 3 using namespace std; 4 5 int main(){ 6 ll t,x,y,z; 7 cin>>t; 8 while(t--){ 9 cin>>x>>y>>z; 10 ll sum = min(x,y); 11 ll ans = abs(x-y); 12 if(z<max(x,y)){ 13 cout<<"-1"<<endl; 14 }else{ 15 if(ans%2){ 16 cout<<z-1<<endl; 17 }else{ 18 if(z==sum+ans){ 19 cout<<z<<endl; 20 continue; 21 } 22 ll cnt = z-sum-ans; 23 if(cnt%2==0){ 24 cout<<z<<endl; 25 }else{ 26 cout<<z-2<<endl; 27 } 28 } 29 } 30 } 31 return 0; 32 }
Let's call some positive integer classy if its decimal representation contains no more than 7277420000 are not.
You are given a segment L≤x≤R.
Each testcase contains several segments, for each of them you are required to solve the problem separately.
The first line contains a single integer 1≤T≤104) — the number of segments in a testcase.
Each of the next 1≤Li≤Ri≤1018).
Print [Li;Ri].
4
1 1000
1024 1024
65536 65536
999999 1000001
1000
1
0
2
数位dp (看了好久才学会)
算是模板题吧
1 #include <bits/stdc++.h> 2 #define ll long long int 3 using namespace std; 4 ll dp[30][4],digth[30]; 5 6 7 ll dfs(int len, int num, bool lim){ 8 if(len == 0) 9 return 1; 10 if(!lim && dp[len][num] != -1) 11 return dp[len][num]; 12 ll ans = 0; 13 int up = lim ? digth[len] : 9; 14 15 for(int i = 0; i <= up; ++i){ 16 if(num < 3){ 17 if( !i ) 18 ans += dfs(len - 1, num, lim && i == digth[len] ); 19 else 20 ans += dfs(len - 1, num + 1, lim && i == digth[len] ); 21 }else if(num == 3){ 22 if(!i) 23 ans += dfs(len -1, num, lim && i == digth[len] ); 24 } 25 } 26 if(!lim ) 27 dp[len][num] = ans; 28 return ans; 29 } 30 31 ll solve(ll x){ 32 int k = 0; 33 while(x){ 34 digth[ ++k ] = x%10; 35 x /= 10; 36 } 37 return dfs(k,0,true); 38 } 39 40 int t; 41 ll n,m; 42 int main(){ 43 scanf("%d", &t); 44 memset( dp , -1 , sizeof(dp) ) ; 45 while(t--){ 46 scanf("%lld%lld",&n,&m); 47 // printf("%lld %lld\n",solve(m),solve(n-1)); 48 printf("%lld\n", solve(m)-solve(n-1)); 49 } 50 return 0; 51 }
Vasya has two arrays m, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [6].
Two arrays Ai=Bi.
Vasya wants to perform some of these operations on array B become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays Bequal.
The first line contains a single integer n (1≤n≤3⋅105) — the length of the first array.
The second line contains A.
The third line contains a single integer m (1≤m≤3⋅105) — the length of the second array.
The fourth line contains B.
Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays B in such a way that they became equal.
If there is no way to make array equal, print "-1".
5
11 2 3 5 7
4
11 7 3 7
3
2
1 2
1
100
-1
3
1 2 3
3
1 2 3
3
指针模拟
1 #include <bits/stdc++.h> 2 #define ll long long int 3 #define N 300005 4 using namespace std; 5 int n,m; 6 ll an[N],bn[N]; 7 int main(){ 8 scanf("%d",&n); 9 for(int i = 1; i <= n; ++i){ 10 scanf("%lld", &an[i]); 11 an[i] += an[i-1]; 12 } 13 scanf("%d", &m); 14 for(int i = 1; i <= m; ++i){ 15 scanf("%lld", &bn[i]); 16 bn[i] += bn[i-1]; 17 } 18 if(an[n]!=bn[m]){ 19 printf("-1\n"); 20 return 0; 21 } 22 int pox = 0; 23 for(int i = 1,j = 1;i <= n&&j <= m;){ 24 if(an[i] == bn[j]){ 25 pox++; 26 i++;j++; 27 }else if(an[i] < bn[j]){ 28 i++; 29 }else{ 30 j++; 31 } 32 } 33 printf("%d\n", pox); 34 return 0; 35 }