题目:abc126_cDice and Coin
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
- Throw the die. The current score is the result of the die.
- As long as the score is between 1 and K−1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
- The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
解析:
题目意思是先丢点数为n的骰子得到一个分数,再丢硬币;当硬币为正时该分数翻倍,只要丢到反面分数就变为0,分数达到K以上就赢,或者变为0就失败;对每个可能丢到的点数,然后再扔硬币赢的概率相加。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int main(){
//ios::sync_with_stdio(0),cin.tie(0);
int n,k;
cin>>n>>k;
double a=1.0/n;
double e=0.5,ans=0;
for(int i=1;i<=n;i++){
double sum=1;
int num=i;
sum*=a;
while (num<k)
{
sum*=e;
num*=2;
}
ans+=sum;
}
printf("%.12lf",ans);
return 0;
}
题目:equeueabc128_d
Your friend gave you a dequeue D as a birthday present.
D is a horizontal cylinder that contains a row of N jewels.
The values of the jewels are V1,V2,...,VN from left to right. There may be jewels with negative values.
In the beginning, you have no jewel in your hands.
You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):
-
Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
-
Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.
-
Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.
-
Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.
Find the maximum possible sum of the values of jewels in your hands after the operations.
解析:
可以选择从左右拿宝石,而且数据范围很小,就枚举每一种可能,如果还有操作次数,就把拿到的宝石为最小的负数减去,然后对每一种情况去Max最后为答案;
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int main(){
ios::sync_with_stdio(0),cin.tie(0);
int n,k;
cin>>n>>k;
ll a[55]={0};
for(int i=1;i<=n;i++){
cin>>a[i];
}
int ma=0;
int ans=0;
for(int i=0;i<=n;i++){
for(int j=n+1;j>0&&i<j;j--){
ma=0;
vector<int>v;
int x=n-j+i+1;
if(x>k) break;
for(int e=1;e<=i;e++) v.push_back(a[e]) ,ma+=a[e];
for(int f=n;f>=j;f--) v.push_back(a[f]),ma+=a[f];
sort(v.begin(),v.end());
int xb=0;
for(int f=0;f<v.size();f++){
if(v[f]<0&&x<k){
ma-=v[f];
x++;
}
else{
break;
}
}
ans=max(ma,ans);
}
}
cout<<ans<<endl;
return 0;
}
题目:Sequence Decomposing
You are given a sequence with N integers: A={A1,A2,⋯,AN}. For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:
- If Ai and Aj (i<j) are painted with the same color, Ai<Aj.
Find the minimum number of colors required to satisfy the condition.
解析:
该题目意思是将a数组的每一个值作为记录数列中最大的结尾(因为ai<aj,i<j)(一个值代表同一个颜色)当每一个结尾都大于等于该值时,就再创建一个值作为开头,最后看a数组的大小为多少,就可以得出最少用多少颜色
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int main(){
//ios::sync_with_stdio(0),cin.tie(0);
int n;
cin>>n;
vector<int>a;
for(int i=0;i<n;i++){
int x;
cin>>x;
x*=-1;//将x变为负数,最大为最小,最小为最大,方便使用二分查找
if(a.empty()){
//cout<<"ddd";
a.push_back(x);
continue;
}
int c=upper_bound(a.begin(),a.end(),x)-a.begin();//二分查找大于等于x的值
if(a.size()==c){
a.push_back(x);
}
else{
a[c]=x;
}
}
cout<<a.size()<<endl;
return 0;
}
题目:Friendships - SMUOJ
解析:
本题要求最短距离为2的k对顶点,菊花图时边数为(n-1)*(n-2)/2;当连接两个不同的点时其实可以发现,没连接一次距离为2的边会减一,这样我们只用连接为菊花图之后再连接多的点,就能达到题目要求。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
int main(){
ios::sync_with_stdio(0),cin.tie(0);
int n,k;
cin>>n>>k;
if(k>(n-1)*(n-2)/2){
cout<<"-1"<<endl;
}
else{
int x=n-1;
int sum=(n-1)*(n-2)/2;
int ans=x+sum-k;
int c=sum-k;
cout<<ans<<endl;
//vector<pii> v;
for(int i=2;i<=n&&c>0;i++){
for(int j=i+1;j<=n&&c>0;j++){
c--;
//v.push_back({i,j});
cout<<i<<" "<<j<<endl;
}
}
for(int i=2;i<=n;i++){
//v.push_back({1,i});
cout<<"1 "<<i<<endl;
}
}
return 0;
}