赞
踩
补题链接:https://ac.nowcoder.com/acm/contest/18713
https://codeforces.com/gym/103202
G. The Witchwood
time limit per test2 seconds
memory limit per test1024 megabytes
inputstandard input
outputstandard output
Shenyang’s night fair culture is developed very well. Every time Bob comes to Shenyang, he will definitely go to a night fair called The Witchwood. There are n snack stalls in The Witchwood, the ith of which gives him ai pleasure.
Bob’s stomach allows him to eat k snack stalls at most. So Bob wants to know the maximum pleasure he can get after visiting the night market.
Input
The first line of input contains two integers n (1≤n≤1000) and k (1≤k≤n), indicating the number of snack stalls and the capacity of Bob’s stomach.
The second line of input contains n integers a1,a2,…,an (1≤ai≤109), the ith of which indicates the pleasure of the ith snack stall.
Output
Print one integer denoting the maximum pleasure Bob can get.
Example
inputCopy
5 2
9 8 10 2 4
outputCopy
19
题意:
思路:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e6+10;
LL a[N];
void solve(){
int n, k; cin>>n>>k;
for(int i = 1; i <= n; i++)cin>>a[i];
sort(a+1,a+n+1, [&](int x, int y){
return x>y;
});
LL res = 0;
for(int i = 1; i <= k; i++){
res += a[i];
}
cout<<res;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T=1; //cin>>T;
while(T--){
solve();
}
return 0;
}
F. Kobolds and Catacombs
time limit per test2 seconds
memory limit per test1024 megabytes
inputstandard input
outputstandard output
Kobolds are rat-like, candle-loving cave folk, digging deep beneath the surface for millennia. Today, they gather together in a queue to explore yet another tunnel in their catacombs!
But just before the glorious movement initiates, they have to arrange themselves in non-descending heights. The shortest is always the leader digging small holes, and the followers swelling it.
The kobolds are hyperactive; they like to move here and there. To make the arrangement easier, they decide to group themselves into consecutive groups first, then reorder in each group.
What’s the maximum number of consecutive groups they can be partitioned into, such that after reordering the kobolds in each group in non-descending order, the entire queue is non-descending?
For example, given a queue of kobolds of heights [1, 3, 2, 7, 4], we can group them into three consecutive groups ([1] [3, 2] [7, 4]), such that after reordering each group, the entire queue can be non-descending.
Input
The first line of the input contains a single integer n (1≤n≤106), denoting the number of kobolds.
The second line contains n integers a1,a2,…,an (1≤ai≤109), representing the heights of the kobolds in the queue.
Output
Print a single integer, denoting the maximum number of groups.
Example
inputCopy
5
1 3 2 7 4
outputCopy
3
题意:
思路:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e6+10;
LL a[N], s[N], ss[N];
void solve(){
int n; cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i]; s[i] = s[i-1]+a[i];
}
sort(a+1,a+n+1);
for(int i = 1; i <= n; i++){
ss[i] = ss[i-1]+a[i];
}
LL res = 0;
for(int i = 1; i <= n; i++){
if(s[i]==ss[i])res++;
}
cout<<res;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T=1; //cin>>T;
while(T--){
solve();
}
return 0;
}
Scholomance Academy
Input file: standard input
Output file: standard output
Time limit: 4 seconds
Memory limit: 1024 megabytes
As a student of the Scholomance Academy, you are studying a course called Machine Learning. You are
currently working on your course project: training a binary classifier.
A binary classifier is an algorithm that predicts the classes of instances, which may be positive (+) or
negative (-). A typical binary classifier consists of a scoring function S that gives a score for every instance
and a threshold θ that determines the category. Specifically, if the score of an instance S(x) ≥ θ, then
the instance x is classified as positive; otherwise, it is classified as negative. Clearly, choosing different
thresholds may yield different classifiers.
Of course, a binary classifier may have misclassification: it could either classify a positive instance as
negative (false negative) or classify a negative instance as positive (false positive).
Predicted class
Actual class Positive Negative
Positive True positive (TP) False negative (FN )
Negative False positive (FP) True negative (TN )
Таблица 1: Predicted classes and actual classes.
Given a dataset and a classifier, we may define the true positive rate (TPR) and the false positive rate
(FPR) as follows:
TPR = #TP
#TP + #FN
, FPR = #FP
#TN + #FP
where #TP is the number of true positives in the dataset; #FP, #TN , #FN are defined likewise.
Now you have trained a scoring function, and you want to evaluate the performance of your classifier.
The classifier may exhibit different TPR and FPR if we change the threshold θ. Let TPR(θ), FPR(θ) be
the TPR, FPR when the threshold is θ, define the area under curve (AUC) as
AUC = Z 1
0
max
θ∈R
{TPR(θ)|FPR(θ) ≤ r}dr
where the integrand, called receiver operating characteristic (ROC), means the maximum possible of TPR
given that FPR ≤ r.
Given the actual classes and predicted scores of the instances in a dataset, can you compute the AUC of
your classifier?
For example, consider the third test data. If we set threshold θ = 30, there are 3 true positives, 2 false
positives, 2 true negatives, and 1 false negative; hence, TPR(30) = 0.75 and FPR(30) = 0.5. Also, as θ
varies, we may plot the ROC curve and compute the AUC accordingly, as shown in Figure 1.
Input
The first line contains a single integer n (2 ≤ n ≤ 106
), the number of instances in the dataset. Then
follow n lines, each line containing a character c ∈ {+, -} and an integer s (1 ≤ s ≤ 109
), denoting the
actual class and the predicted score of an instance.
It is guaranteed that there is at least one instance of either class.
Output
Print the AUC of your classifier within an absolute error of no more than 10−9
.
Page 1 of 2
Рис. 1: ROC and AUC of the third sample data.
Examples
standard input standard output
3
题意:
思路:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e6+10;
void solve(){
int n; cin>>n;
vector<double>v1, v2;
for(int i = 1; i <= n; i++){
string op; int x; cin>>op>>x;
if(op=="+")v1.push_back(x);
else v2.push_back(x);
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
double res = 0;
for(int i = 0; i < v1.size(); i++){
res += lower_bound(v2.begin(), v2.end(), v1[i])-v2.begin();
}
res = res/(double)v1.size()/(double)v2.size();
printf("%.10lf\n", res);
}
int main(){
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T=1; //cin>>T;
while(T--){
solve();
}
return 0;
}
D. Journey to Un’Goro
time limit per test2 seconds
memory limit per test1024 megabytes
inputstandard input
outputstandard output
Recently, you’ve taken a trip to Un’Goro.
A small road in Un’Goro has attracted your eyes. The road consists of n steps, each colored either red or blue.
When you go from the ith step to the jth, you count the number of red steps you’ve stepped. You will be satisfied if the number is odd.
“What is the maximum number of pairs (i,j) (1≤i≤j≤n), such that I’ll be satisfied if I walk from the ith step to the jth?” you wonder. Also, how to construct all colorings such that the number of pairs is maximized?
Input
The only line contains an integer n (1≤n≤105), indicating the number of steps of the road.
Output
Output an integer in the first line, denoting the maximum number of pairs that make you satisfied.
Each of the following several lines contains a string with length n that represents a coloring scheme, in lexicographically ascending order. The ith character is the color of the ith step, where r is for red and b for blue.
If there are more than 100 different colorings, just find the lexicographically smallest 100 colorings.
Note that in lexicographical order, b is ordered before r.
Examples
inputCopy
1
outputCopy
1
r
inputCopy
2
outputCopy
2
br
rb
rr
inputCopy
3
outputCopy
4
brb
rbr
rrr
题意:
思路:
首先如何判断一个给定的区间是不是好区间,肯定不能每次O(n^2)跑一遍数数有多少个r。
我们把r记为1,b记为0,对序列做前缀和, 那么好区间就是前缀和作差为奇数的区间。
然后考虑这个前缀和数组的性质, 因为前缀和要么是偶数要么是奇数,我们最后要的是作差为奇数。
而只有偶数减奇数或者奇数减偶数才有可能得到奇数,所以我们不难算出最终整个序列好区间的个数为前缀和数组的奇数个数odd乘上偶数个数even。
我们最后要的是好区间个数尽可能大,同时因为奇数个数+偶数个数=n是固定的,所以根据f(x) = (n-x)*x ,显然x取到n/2附近, 奇数个数和偶数个数相等时,乘积能取到最大值。
所以我们构造的时候, dfs分别维护当前奇数个数和偶数个数,如果>n/2,就剪枝减掉,输出前100个为止。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e6+10;
LL n, mx, cnt = 0; string s;
void dfs(int cur, int n1, int n2, int st){ //奇数,偶数个数,前缀和奇偶性
if(n1 > mx || n2 > mx || cnt==100)return ;
if(cur == n){
cnt++;
cout<<s<<"\n";
return ;
}
s[cur] = 'b';
dfs(cur+1, n1+(st^1), n2+st, st);
st ^= 1;
s[cur] = 'r';
dfs(cur+1, n1+(st^1), n2+st, st);
}
void solve(){
cin>>n;
s = string(n,'r');
mx = (n+2)/2;
cout<<((n+1)/2)*((n+2)/2)<<"\n";
dfs(0, 1, 0, 0);
}
int main(){
// ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T=1; //cin>>T;
while(T--){
solve();
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。