A Combination Lock
N-bit 0~9 password lock between the two states, ask the minimum number of times to dial. Note that it may cross ' 0 '. directly on the code.
#include <bits/stdc++.h>
using namespace std;
Char num1[1010];
Char num2[1010];
int main () {
int n;
cin>>n;
cin>>num1>>num2;
int ans=0;
for (int i=0;i<n;i++) {
int tmp=abs (num1[i]-num2[i]);
Tmp=min (Tmp,num1[i]+10-num2[i]);
Tmp=min (Tmp,num2[i]+10-num1[i]);
ans+=tmp;
}
cout<<ans<<endl;
return 0;
B School Marks
is a person to take the N course, the total score can not be higher than X, all class scores in the median can not lower than Y. Now has tested the K gate, asks whether can reach the request.
Greedy. Because the total score can not be too high, if the median is not high enough directly with Y to fill, after completion of all tests 1 points.
#include <bits/stdc++.h>
using namespace std;
int a[10100];
int b[10100];
int main () {
int n,k,p,x,y;
cin>>n>>k>>p>>x>>y;
int cnt=0;
for (int i=1;i<=k;i++) {
cin>>a[i];
if (a[i]>=y) cnt++;
}
int mid= (n+1)/2;
int need=mid-cnt;
for (int i=k+1;i<=k+need;i++) {
a[i]=y
}
if (need<0) need=0;
for (int i=k+need+1;i<=n;i++) {
a[i]=1
}
int sum=0;
for (int i=1;i<=n;i++) {
sum+=a[i];
}
BOOL Ok=1;
if (sum>x) ok=0;
for (int i=1;i<=n;i++) b[i]=a[i];
Sort (b+1,b+n+1);
if (b[mid]<y) ok=0;
if (OK) {for
(int i=k+1;i<=n;i++) {
cout<<a[i]<< "";
}
} else{
cout<< "-1" <<endl;
}
return 0;
C Ice Cave
Take the ice maze. There are two kinds of ice, intact and broken, intact pass will become broken, broken walk will fall down, need to drop down at the end, ask if it can be achieved.
BFS. Search a perfect ice road to the end. If the end point is intact, it is necessary to have another adjacent lattice that is intact and able to walk back to the end. Note that the starting point and end point may coincide and require special judgment.
#include <bits/stdc++.h> using namespace std;
Char mp[510][510];
BOOL vis[510][510];
int dirn[]={-1,1,0,0};
int dirm[]={0,0,-1,1};
struct pt{int x,y;
PT (int x,int y): x (x), Y (y) {} pt () {}};
int main () {int n,m;
cin>>n>>m;
for (int i=1;i<=n;i++) {scanf ("%s", mp[i]+1);
int sn,sm;
int Tn,tm;
cin>>sn>>sm;
cin>>tn>>tm;
Queue<pt> que;
Que.push (PT (SN,SM));
Vis[sn][sm]=1;
while (!que.empty ()) {pt cur = Que.front (); Que.pop ();
for (int i=0;i<4;i++) {int newn=cur.x+dirn[i];
int newm=cur.y+dirm[i];
if (newn<1) continue;
if (newn>n) continue;
if (newm<1) continue;
if (newm>m) continue; if (!vis[newn][newm]) {if (mp[newn][newm]== '. ') (NEWN==TN&&NEWM==TM))
{vis[newn][newm]=1;
Que.push (PT (NEWN,NEWM));
}}//Tn==sn&&tm==sm coincident if ({int cnt=0); for (int i=0;i<4;i++) {if (mp[tn+dirn[i]][tm+dirm[i]]== '. ')
cnt++; } if (CNT) {COUT<< "YES" <<endl;
return 0;
}else{cout<< "NO" <<endl;
return 0;
} if (Vis[tn][tm]) {if (mp[tn][tm]== ' X ') {cout<< ' YES ' <<endl;
}else{int cnt=0; for (int i=0;i<4;i++) {if (mp[tn+dirn[i]][tm+dirm[i]]== '. ')
cnt++; } if (cnt>1| | (ABS (SN-TN) +abs (SM-TM) ==1&&cnt==1))
{//Pay attention to the particularly wretched situation cout<< "YES" <<endl;
}else{cout<< "NO" <<endl;
}}}else{cout<< "NO" <<endl;
return 0; }
D Bad Luck Island
An island, there are three kinds of stone scissors cloth people, they will constantly meet randomly, lose the people are gone. Asked the last stone scissors cloth The probability of the remaining is how much.
Probability DP. DP (I,J,K) says there is also a stone on the island, J scissors, k cloth. A DP (I,J,K) can be transferred by a DP (I+1,J,K), a DP (I,J+1,K), and a DP (I,J,K+1). For example, DP (I+1,J,K)->DP (i,j,k), the occurrence of this must be the stone and cloth meet, the stone is gone, according to their number to calculate the probability. It should be noted that the same type of person meeting is not going to happen, so calculate the proportion of the probability of that part of the other situation. See the code specifically.
#include <bits/stdc++.h> using namespace std;
Double dp[110][110][110];
int main () {int a,b,c;
cin>>a>>b>>c;
Dp[a][b][c]=1; for (int i=a;i>=0;i--) {for (int j=b;j>=0;j--) {for (int k=c;k>=0;k--) {if (i==a&&j==b&&k==c
) continue;
Double sum=i+j+k+1;
if (sum<=1) continue;
Double tmp1=0,tmp2=0,tmp3=0;
Double t=0;
TMP1 + = dp[i+1][j][k]*k/sum* (i+1)/(SUM-1);
TMP1 + + dp[i+1][j][k]* (i+1)/sum*k/(sum-1);
Calculate the probability of meeting the same person and share it proportionately, similarly.
t= (i+1)/sum* (i)/(sum-1) + (j-1)/sum* (j)/(Sum-1) + (k-1)/sum* (k)/(SUM-1);
if (t<1.0) tmp1/= (1.0-T);
TMP2 + = dp[i][j+1][k]*i/sum* (j+1)/(SUM-1);
TMP2 + + dp[i][j+1][k]* (j+1)/sum*i/(sum-1);
t= (i-1)/sum* (i)/(sum-1) + (j+1)/sum* (j)/(Sum-1) + (k-1)/sum* (k)/(SUM-1);
if (t<1.0) tmp2/= (1.0-T);
Tmp3 + = dp[i][j][k+1]*j/sum* (k+1)/(SUM-1);
Tmp3 + + dp[i][j][k+1]* (k+1)/sum*j/(sum-1);
t= (i-1)/sum* (i)/(sum-1) + (j-1)/sum* (j)/(Sum-1) + (k+1)/sum* (k)/(SUM-1); if (t<1.0) tmp3/= (1.0-T);
Dp[i][j][k]=tmp1+tmp2+tmp3;
}} double ansa=0;
Double ansb=0;
Double ansc=0;
for (int i=1;i<=a;i++) {ansa+=dp[i][0][0];
for (int i=1;i<=b;i++) {ansb+=dp[0][i][0];
for (int i=1;i<=c;i++) {ansc+=dp[0][0][i];
printf ("%.10lf%.10lf%.10lf\n", ANSA,ANSB,ANSC);
return 0; }