This is a big collection of water issues. I hope everyone will be happy;
The first two questions are for me, and the last two are for Tao shu. In addition, because of my laziness, all the code below is written by Tao shu. Thank you very much;
During the summer vacation, Uncle Tao sacrificed a lot of his precious time for the purpose of training. You should train well next !!!!
Go to the topic:
Problem A spoj quest5
Sign-in question:
Sort all the edges in the order of the right endpoint, select a point that is not pinned each time, and then remove all the edges that overlap with it;
Code:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10100;struct Table{ int l,r; bool operator < (const Table &rhs) const{ return r < rhs.r; }}table[maxn];int main(){ int kase,n; scanf("%d",&kase); while(kase--){ scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d%d",&table[i].l,&table[i].r); } sort(table,table+n); int ans = 0,r = -1; for(int i = 0;i < n;i++){ if(r < table[i].l){ ans++; r = table[i].r; } } printf("%d\n",ans); } return 0;}
View code
Problem B spoj favdice
It's also a question for everyone. It's not just a question for high school teachers ~~~
The expected number of throws from an I plane to an I + 1 plane is N/(n-I ).
Code:
#include <cstdio>int main(){ int kase,n; scanf("%d",&kase); while(kase--){ scanf("%d",&n); double ans = 0; for(int i = 1;i <= n;i++) ans += (n+0.0)/(i+0.0); printf("%.2f\n",ans); } return 0;}
View code
Problem C spoj gnyr09f
DP question, DP may be difficult for everyone at the beginning, but it is still relatively simple to calm down and analyze;
DP (I, j, k) indicates the number of solutions where the current sum is J and the ending number is K.
Consider the two situations at position I:
1. Put 0: dp (I, j, 0) = dp (I-1, J, 0) + dp (I-1, J, 1)
2. Put 1: dp (I, j, 1) = dp (I-1, J, 0)
If j> = 1, DP (I, j, 1) + = dp (I-1, J-1, 1)
#include <cstdio>#include <cstring>int dp[101][101][2];int main(){ int kase,hehe,n,k; scanf("%d",&kase); while(kase--){ scanf("%d%d%d",&hehe,&n,&k); for(int i = 0;i <= k;i++) dp[1][i][0] = dp[1][i][1] = 0; dp[1][0][0] = dp[1][0][1] = 1; for(int i = 2;i <= n;i++){ for(int j = 0;j <= k;j++){ dp[i][j][0] = dp[i-1][j][0]+dp[i-1][j][1]; dp[i][j][1] = dp[i-1][j][0]; if(k >= 1) dp[i][j][1] += dp[i-1][j-1][1]; } } printf("%d %d\n",hehe,dp[n][k][0]+dp[n][k][1]); } return 0;}
View code
Problem D spoj venom
Second question: If someone treats it as a violent question and is confused, you are welcome to join the congage team for a marathon ~~~
The time when the second hero died is directly calculated and determined ~~~
Code:
#include <cstdio>typedef long long LL;int H,P,A;bool judge(int n){ LL ans = (LL)(n+1)*n/2*P-(LL)(n-1)*A-H; if(ans >= 0) return 1; return 0;}int main(){ int kase; scanf("%d",&kase); while(kase--){ scanf("%d%d%d",&H,&P,&A); int l = 1,r = 1000000,ans = l; while(l <= r){ int mid = (l+r)>>1; if(judge(mid)) r = mid-1,ans = mid; else l = mid+1; } printf("%d\n",ans*2-1); } return 0;}
View code