Karen is getting ready for a new school day!
![](http://codeforces.com/predownloaded/10/10/101006b28ff0912f16c1d39e67206ca4b0b6da23.png)
It is currently hh:mm, given in a 24-hour format. As you know, Karen loves palindromes, and she believes that it is good luck to wake up when the time is a palindrome.
What is the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome?
Remember that a palindrome is a string that reads the same forwards and backwards. For instance, 05:39 is not a palindrome, because05:39 backwards is 93:50. On the other hand, 05:50 is a palindrome, because 05:50 backwards is 05:50.
The first and only line of input contains a single string in the format hh:mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59).
Output a single integer on a line by itself, the minimum number of minutes she should sleep, such that, when she wakes up, the time is a palindrome.
![](https://images2018.cnblogs.com/blog/1060545/201802/1060545-20180219145159324-2017562437.png)
In the first test case, the minimum number of minutes Karen should sleep for is 11. She can wake up at 05:50, when the time is a palindrome.
In the second test case, Karen can wake up immediately, as the current time, 13:31, is already a palindrome.
In the third test case, the minimum number of minutes Karen should sleep for is 1 minute. She can wake up at 00:00, when the time is a palindrome.
思路:暴力模拟就可以了。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int n,m,s,ans; int main(){ scanf("%d:%d",&n,&m); while(n/10+n%10*10!=m){ ans++; m++;m%=60; if(!m){ n++;n%=24; } } cout<<ans; }
To stay woke and attentive during classes, Karen needs some coffee!
![](http://codeforces.com/predownloaded/02/15/02159e70847762eaf87fdca280025a766c8a564b.png)
Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed "The Art of the Covfefe".
She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.
Karen thinks that a temperature is admissible if at least k recipes recommend it.
Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.
The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive.
The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.
![](https://images2018.cnblogs.com/blog/1060545/201802/1060545-20180219145521614-1613832209.png)
In the first test case, Karen knows 3 recipes.
- The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
- The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
- The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.
A temperature is admissible if at least 2 recipes recommend it.
She asks 4 questions.
In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are3: 92, 93 and 94 degrees are all admissible.
In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.
In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.
In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.
In the second test case, Karen knows 2 recipes.
- The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
- The second one, "What good is coffee that isn't brewed at at least 36.3306 times the temperature of the surface of the sun?", recommends brewing the coffee at exactly 200000 degrees.
A temperature is admissible if at least 1 recipe recommends it.
In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.
思路:前缀和搞一下就可以了。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 200010 using namespace std; int n,k,q; int ans[MAXN],sum[MAXN]; int main(){ scanf("%d%d%d",&n,&k,&q); for(int i=1;i<=n;i++){ int x,y; scanf("%d%d",&x,&y); ans[y+1]--;ans[x]++; } for(int i=1;i<=200000;i++) sum[i]=sum[i-1]+ans[i]; memset(ans,0,sizeof(ans)); for(int i=1;i<=200000;i++) if(sum[i]>=k) ans[i]++; memset(sum,0,sizeof(sum)); for(int i=1;i<=200000;i++) sum[i]=sum[i-1]+ans[i]; for(int i=1;i<=q;i++){ int x,y; scanf("%d%d",&x,&y); cout<<sum[y]-sum[x-1]<<endl; } }
On the way to school, Karen became fixated on the puzzle game on her phone!
![](http://codeforces.com/predownloaded/13/27/1327478902a073d7774242480818f1d4d93d7270.png)
The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0.
One move consists of choosing one row or column, and adding 1 to all of the cells in that row or column.
To win the level, after all the moves, the number in the cell at the i-th row and j-th column should be equal to gi, j.
Karen is stuck on one level, and wants to know a way to beat this level using the minimum number of moves. Please, help her with this task!
The first line of input contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and the number of columns in the grid, respectively.
The next n lines each contain m integers. In particular, the j-th integer in the i-th of these rows contains gi, j (0 ≤ gi, j ≤ 500).
If there is an error and it is actually not possible to beat the level, output a single integer -1.
Otherwise, on the first line, output a single integer k, the minimum number of moves necessary to beat the level.
The next k lines should each contain one of the following, describing the moves in the order they must be done:
- row x, (1 ≤ x ≤ n) describing a move of the form "choose the x-th row".
- col x, (1 ≤ x ≤ m) describing a move of the form "choose the x-th column".
If there are multiple optimal solutions, output any one of them.
![](https://images2018.cnblogs.com/blog/1060545/201802/1060545-20180219145901364-1213634500.png)
In the first test case, Karen has a grid with 3 rows and 5 columns. She can perform the following 4 moves to beat the level:
![](http://codeforces.com/predownloaded/dd/0f/dd0fcb1e5b1fe4c79e563210a46087f050e31b23.png)
In the second test case, Karen has a grid with 3 rows and 3 columns. It is clear that it is impossible to beat the level; performing any move will create three 1s on the grid, but it is required to only have one 1 in the center.
In the third test case, Karen has a grid with 3 rows and 3 columns. She can perform the following 3 moves to beat the level:
Note that this is not the only solution; another solution, among others, is col 1, col 2, col 3.
思路:模拟。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 110 using namespace std; int l[MAXN*510],r[MAXN*510]; int l1[MAXN*510],r1[MAXN*510]; int n,m,ans,ans1,tot1,tot2,tot3,tot4; int num[MAXN][MAXN],num1[MAXN][MAXN]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&num[i][j]),num1[i][j]=num[i][j]; for(int i=1;i<=n;i++){ int minn=1000; for(int j=1;j<=m;j++) if(num[i][j]<minn) minn=num[i][j]; for(int j=1;j<=minn;j++) l[tot1+j]=i; tot1+=minn;ans+=minn; for(int j=1;j<=m;j++) num[i][j]-=minn; } for(int j=1;j<=m;j++){ int minn=1000; for(int i=1;i<=n;i++) if(num[i][j]<minn) minn=num[i][j]; for(int i=1;i<=minn;i++) r[tot2+i]=j; tot2+=minn;ans+=minn; for(int i=1;i<=n;i++) num[i][j]-=minn; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(num[i][j]!=0){ cout<<"-1";return 0; } ans1=ans;ans=0; for(int j=1;j<=m;j++){ int minn=1000; for(int i=1;i<=n;i++) if(num1[i][j]<minn) minn=num1[i][j]; for(int i=1;i<=minn;i++) r1[tot3+i]=j; tot3+=minn;ans+=minn; for(int i=1;i<=n;i++) num1[i][j]-=minn; } for(int i=1;i<=n;i++){ int minn=1000; for(int j=1;j<=m;j++) if(num1[i][j]<minn) minn=num1[i][j]; for(int j=1;j<=minn;j++) l1[tot4+j]=i; tot4+=minn;ans+=minn; for(int j=1;j<=m;j++) num1[i][j]-=minn; } cout<<min(ans,ans1)<<endl; if(ans1<ans){ for(int i=1;i<=tot1;i++) cout<<"row "<<l[i]<<endl; for(int i=1;i<=tot2;i++) cout<<"col "<<r[i]<<endl; } else{ for(int i=1;i<=tot3;i++) cout<<"col "<<r1[i]<<endl; for(int i=1;i<=tot4;i++) cout<<"row "<<l1[i]<<endl; } }
Karen has just arrived at school, and she has a math test today!
![](http://codeforces.com/predownloaded/6e/aa/6eaa412f483cdb71aef60901bb7cf8d9f8e80d76.png)
The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.
There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.
Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.
The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.
Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?
Since this number can be quite large, output only the non-negative remainder after dividing it by 109 + 7.
The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.
The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.
Output a single integer on a line by itself, the number on the final row after performing the process above.
Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.
![](https://images2018.cnblogs.com/blog/1060545/201802/1060545-20180219150314359-823810756.png)
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15.
Karen performs the operations as follows:
![](http://codeforces.com/predownloaded/c6/cf/c6cf3ef421efd24bfead24a2fa641179623870ee.png)
The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.
In the second test case, the numbers written on the first row are 3, 7, 5 and 2.
Karen performs the operations as follows:
![](http://codeforces.com/predownloaded/1e/40/1e4099491b0cabfca6571e038c4a2938e929840d.png)
The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output.
思路:乘法逆元+组合数学+杨辉三角。
打表可以找到规律,是一种那个类似杨辉三角的组合。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define mod 1000000007 using namespace std; int n,flag; long long s[201000]; long long inv(long long x){ return x==1?1:(-(mod/x)*inv(mod%x)%mod); } void turn(){ for(int i=1;i<n;i++) if(i&1)s[i]=(s[i]+s[i+1])%mod; else s[i]=(s[i]-s[i+1])%mod; n--; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%I64d",&s[i]); if(n==1){ printf("%I64d ",s[1]);return 0; } if(n&1) turn(); long long ans=0,C=1,k=0,m=(n-1)/2,flag=(n%4==0); for(int i=1;i<=n;i++){ if(i&1) ans=(ans+C*s[i]%mod)%mod; else if(flag) ans=(ans-C*s[i]%mod)%mod; else ans=(ans+C*s[i]%mod)%mod; if(!(i&1)){ C=(C*(m-k)%mod)*inv(k+1)%mod;k++; } } ans=(ans%mod+mod)%mod; printf("%I64d ",ans); }
On the way home, Karen decided to stop by the supermarket to buy some groceries.
![](http://codeforces.com/predownloaded/bd/67/bd67ad108c1af032534c33a7537bb924563e44d6.png)
She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.
The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.
Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.
There is, however, a constraint with the coupons. For all i ≥ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).
Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?
The first line of input contains two integers n and b (1 ≤ n ≤ 5000, 1 ≤ b ≤ 109), the number of goods in the store and the amount of money Karen has, respectively.
The next n lines describe the items. Specifically:
- The i-th line among these starts with two integers, ci and di (1 ≤ di < ci ≤ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively.
- If i ≥ 2, this is followed by another integer, xi (1 ≤ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
![](https://images2018.cnblogs.com/blog/1060545/201802/1060545-20180219150927308-1601430575.png)
In the first test case, Karen can purchase the following 4 items:
- Use the first coupon to buy the first item for 10 - 9 = 1 dollar.
- Use the third coupon to buy the third item for 12 - 2 = 10 dollars.
- Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars.
- Buy the sixth item for 2 dollars.
The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.
In the second test case, Karen has enough money to use all the coupons and purchase everything.
思路:树形DP
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 5010 #define INF 0x7f7f7f7f using namespace std; int n,tot; long long b; int son[MAXN]; int head[MAXN]; long long d[MAXN],c[MAXN]; long long dp[MAXN][MAXN][2]; struct edge{ int to,next; }e[MAXN]; void add(int u,int v){ e[tot].to=v;e[tot].next=head[u];head[u]=tot++; } void dfs(int u){ for(int i=0;i<MAXN;i++) dp[u][i][0]=dp[u][i][1]=INF; dp[u][1][1]=d[u]-c[u]; dp[u][1][0]=d[u]; dp[u][0][0]=0; son[u]=1; for(int k=head[u];k!=-1;k=e[k].next){ int v=e[k].to; dfs(v); for(int i=son[u];i>=0;i--){ for(int j=0;j<=son[v];j++){ dp[u][i+j][0]=min(dp[u][i+j][0],dp[v][j][0]+dp[u][i][0]); dp[u][i+j][1]=min(dp[u][i+j][1],min(dp[v][j][1],dp[v][j][0])+dp[u][i][1]); } } son[u]+=son[v]; } } int main(){ memset(head,-1,sizeof head); scanf("%d%lld",&n,&b); memset(son,0,sizeof(son)); for(int i=1;i<=n;i++){ if(i==1) scanf("%lld%lld",d+i,c+i); else{ int x; scanf("%lld%lld%d",d+i,c+i,&x); add(x,i); } } dfs(1);int res=0; for(int i=0;i<=n;i++){ if(dp[1][i][0]<=b) res=max(res,i); if(dp[1][i][1]<=b) res=max(res,i); } printf("%d ",res); }