通过数:2
250:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class Quorum {
public:
int count(vector <int>, int);
};
int Quorum::count(vector <int> arr, int k) {
sort(arr.begin(), arr.end());
int res = 0;
for(int i = 0 ; i < min(k, (int)arr.size()) ; i++)
res += arr[i];
return res;
}
<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
500:
加一个vector用来回溯就好
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class Quacking {
public:
int quack(string);
};
string str = "quack";
int Quacking::quack(string s) {
int n = s.size();
int res = 0;
int vis[5000];
for(int i = 0 ; i < n ; i++) {
// printf("i = %d\n", i);
if(vis[i] == 1) continue;
else if(s[i] != ‘q‘) return -1;
int num = 0;
vector<int>rev;
for(int j = i ; j < n ; j++) {
if(vis[j] == 1) continue;
if(s[j] == str[num]) {
num++, vis[j] = 1, rev.push_back(j);//, printf("%d ", j);
}
if(num == 5) {
rev.clear();
num = 0;
}
}
// puts("");
for(int j = 0 ; j < (int)rev.size() ; j++) vis[rev[j]] = 0;
res++;
}
for(int i = 0 ; i < n ; i++) if(vis[i] == 0) return -1;
return res;
}
<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
1000:
打死想不到是概率DP
转换后发现原式表示的是f(p,k)表示前k-1天均未完成,第k天完成的概率
那么,每天完成的概率就是1/p
Dp[i][j]表示到达(i,j)状态时,第一个任务消耗时间严格小于第一个任务的概率
初始化dp[i][0] = 0, dp[0][i!=0] = 1
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class Queueing {
public:
double probFirst(int, int, int, int);
};
const int MAXN = 1000 + 5;
double dp[MAXN][MAXN];
double Queueing::probFirst(int len1, int len2, int p1, int p2) {
double q1 = 1.0 / (1.0 * p1);
double q2 = 1.0 / (1.0 * p2);
for(int i = 0 ; i <= len1 ; i++) {
for(int j = 0 ; j <= len2 ; j++) {
if(j == 0) dp[i][j] = 0;
else if(i == 0) dp[i][j] = 1;
else {
double temp1 = (q1) * (q2) * dp[i - 1][j - 1];
double temp2 = (1 - q1) * q2 * dp[i][j - 1];
double temp3 = (q1) * (1 - q2) * dp[i - 1][j];
dp[i][j] = (temp1 + temp2 + temp3) / (1.0 - (1 - q1) * (1 - q2));
}
}
}
return dp[len1][len2];
}
<%:testing-code%>
//Powered by [KawigiEdit] 2.0!
时间: 2024-11-06 09:37:08