Skip to content

Commit d48544e

Browse files
committed
add a few CF problems.
1 parent b9c6ee4 commit d48544e

File tree

23 files changed

+4962
-0
lines changed

23 files changed

+4962
-0
lines changed

cf/112/C.cpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <cstdio>
4+
5+
using namespace std;
6+
int K;
7+
string S;
8+
9+
const int MAXN = 1000001;
10+
int pos[MAXN];
11+
12+
void work(){
13+
int idx = 0;
14+
int S_sz = S.size();
15+
/*
16+
for(int i=0; i<S_sz; i++){
17+
if(S[i]=='1'){
18+
pos[idx++] = i;
19+
}
20+
}
21+
if(idx<K){
22+
cout<<0<<endl;
23+
return ;
24+
}
25+
*/
26+
int front = 0;
27+
int end = 0;
28+
int cur = 0;
29+
long long int ans = 0;
30+
cout<<"S_sz = "<<S_sz<<endl;
31+
while(front<S_sz){
32+
cout<<"front = "<<front<<" "<<"end = "<<end<<endl;
33+
if(cur<K){
34+
if(end<S_sz){
35+
end++;
36+
if(end<S_sz && S[end]=='1'){
37+
cur++;
38+
if(cur==K){
39+
ans++;
40+
}
41+
}
42+
}else if(end>=S_sz){
43+
cout<<"haha i break"<<endl;
44+
break;
45+
}
46+
}else if(cur==K){
47+
if(S[front]=='1'){
48+
cur-=1;
49+
}else{
50+
ans+=1;
51+
}
52+
front++;
53+
}
54+
}
55+
cout<<ans<<endl;
56+
57+
58+
}
59+
60+
int main(){
61+
cin>>K;
62+
cin>>S;
63+
work();
64+
65+
66+
return 0;
67+
}
68+

cf/112/C2.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <cstdio>
4+
5+
using namespace std;
6+
int K;
7+
string S;
8+
9+
const int MAXN = 1000001;
10+
int pos[MAXN];
11+
long long int dp[MAXN];
12+
long long int sum[MAXN];
13+
14+
15+
void calc(){
16+
sum[0] = 0;
17+
sum[1] = 1;
18+
int S_sz = S.size();
19+
for(int i=1; i<MAXN; i++){
20+
sum[i] = i+sum[i-1];
21+
}
22+
dp[0] = 0;
23+
dp[1] = 1;
24+
for(int i=2; i<MAXN; i++){
25+
dp[i] = i+dp[i-1];
26+
}
27+
//cout<<dp[2]<<" "<<dp[3]<<" "<<dp[5]<<" "<<dp[6]<<endl;
28+
29+
}
30+
31+
32+
void work(){
33+
int idx = 0;
34+
calc();
35+
int S_sz = S.size();
36+
for(int i=0; i<S_sz; i++){
37+
if(S[i]=='1'){
38+
pos[idx++] = i;
39+
}
40+
}
41+
if(idx<K){
42+
cout<<0<<endl;
43+
return ;
44+
}
45+
long long int ans = 0;
46+
int end = K-1, front = 0;
47+
48+
if(K==0){
49+
if(idx==0){
50+
cout<<dp[S_sz]<<endl;
51+
}else{
52+
ans = dp[pos[0]];
53+
for(int i=1; i<idx; i++){
54+
ans += dp[pos[i]-pos[i-1] - 1];
55+
}
56+
ans += dp[S_sz-pos[idx-1]-1];
57+
cout<<ans<<endl;
58+
}
59+
return ;
60+
}
61+
62+
63+
64+
while(1){
65+
if(end>=idx)break;
66+
long long int left, right;
67+
if(end==idx-1){
68+
right = S_sz - pos[end] -1 + 1;
69+
}else{
70+
right = pos[end+1] - pos[end] -1 + 1;
71+
}
72+
if(front == 0){
73+
left = pos[0] + 1;
74+
}else{
75+
left = pos[front]-pos[front-1]-1 + 1;
76+
}
77+
//cout<<"front = "<<front<<" end = "<<end<<" left right = "<<left<<" "<<right<<endl;
78+
ans += left * right;
79+
front++, end++;
80+
}
81+
cout<<ans<<endl;
82+
}
83+
84+
int main(){
85+
cin>>K;
86+
cin>>S;
87+
work();
88+
89+
90+
return 0;
91+
}
92+

cf/112/a.out

14 KB
Binary file not shown.

cf/135/a.out

7.64 KB
Binary file not shown.

cf/142/C2.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
#include <cmath>
5+
#include <string.h>
6+
7+
using namespace std;
8+
const int INF = 99999999;
9+
const int MAXM = 1001;
10+
const int MAXN = 10001;
11+
char g[MAXM][MAXN];
12+
int min_dis[MAXM][MAXN];
13+
int N,M;
14+
15+
void work(){
16+
//from left to right
17+
for(int i=0; i<M; i++)for(int j=0; j<N; j++)min_dis[i][j] = INF;
18+
19+
for(int i=0; i<M; i++){
20+
int idx = -1;
21+
int reverse_idx = -1;
22+
for(int j=0; j<N; j++){
23+
if(g[i][j]=='1'){
24+
idx = j;
25+
}
26+
if(idx==-1)continue;
27+
min_dis[i][j] = j-idx;
28+
}
29+
for(int j=N-1; j>=0; j--){
30+
if(g[i][j]=='1'){
31+
reverse_idx = j;
32+
}
33+
if(reverse_idx == -1)continue;
34+
min_dis[i][j] = min(min_dis[i][j], reverse_idx - j);
35+
}
36+
}
37+
/*
38+
for(int i=0; i<M; i++){
39+
for(int j=0; j<N; j++){
40+
cout<<min_dis[i][j]<<" ";
41+
}
42+
cout<<endl;
43+
}
44+
*/
45+
int ans = INF;
46+
for(int j=0; j<N; j++){
47+
int tmp = 0;
48+
for(int i=0; i<M; i++){
49+
tmp += min_dis[i][j];
50+
}
51+
ans = min(ans, tmp);
52+
53+
54+
}
55+
if(ans==INF){
56+
cout<<-1<<endl;
57+
}
58+
else{
59+
cout<<ans<<endl;
60+
}
61+
62+
63+
}
64+
65+
int main(){
66+
cin>>M>>N;
67+
for(int i=0; i<M; i++){
68+
cin>>g[i];
69+
}
70+
work();
71+
return 0;
72+
}
73+

cf/142/a.out

-6.09 KB
Binary file not shown.

cf/145/C.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <fstream>
4+
5+
using namespace std;
6+
7+
const int MAXN = 100001;
8+
int N;
9+
int my_data[MAXN];
10+
int neg[MAXN];
11+
int pos[MAXN];
12+
const int INF = 999999999;
13+
14+
int main(){
15+
//scanf("%d",&N);
16+
17+
ifstream myfile;
18+
myfile.open("input.txt");
19+
20+
myfile>>N;
21+
//cin>>N;
22+
for(int i=0; i<N; i++){
23+
//scanf("%d",&my_data[i]);
24+
//cin>>my_data[i];
25+
myfile>>my_data[i];
26+
if(i>0){
27+
if(my_data[i]>0 ){
28+
pos[i] = pos[i-1] + 1;
29+
neg[i] = neg[i-1];
30+
}
31+
else if(my_data[i]<0){
32+
neg[i] = neg[i-1] + 1;
33+
pos[i] = pos[i-1];
34+
}else{
35+
pos[i] = pos[i-1];
36+
neg[i] = neg[i-1];
37+
}
38+
}else{
39+
if(my_data[i]>0){
40+
pos[i] = 1;
41+
}
42+
else if(my_data[i]<0){
43+
neg[i] = 1;
44+
}else{
45+
pos[i] = 0;
46+
neg[i] = 0;
47+
}
48+
}
49+
}
50+
51+
/*
52+
for(int i=0; i<N; i++){
53+
cout<<"at "<<i<<" "<<pos[i]<<" "<<neg[i]<<endl;
54+
}
55+
*/
56+
57+
58+
int ans = INF;
59+
60+
61+
for(int i=1; i<N; i++){
62+
int pre_neg = neg[i-1];
63+
int post_pos = pos[N-1] - pos[i-1];
64+
//cout<<"i = "<<i<<" "<<pre_neg<<" "<<post_pos<<endl;
65+
//cout<<"change "<<i-pre_neg<<" pos to neg "<<endl;
66+
//cout<<"change "<<N-i-post_pos<<" neg to pos"<<endl;
67+
int tmp_ans = (i-pre_neg) + (N-i-post_pos);
68+
ans = min(tmp_ans, ans);
69+
}
70+
ofstream out;
71+
out.open("output.txt");
72+
out<<ans<<endl;
73+
74+
75+
//cout<<"ha"<<endl;
76+
return 0;
77+
}
78+

cf/145/a.out

14.7 KB
Binary file not shown.

cf/145/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 3 -5

cf/145/output.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2

cf/152/B.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
import math
3+
4+
def digit(num):
5+
cnt = 0
6+
while(num):
7+
cnt += 1
8+
num/=10
9+
return cnt
10+
11+
def work():
12+
n = int(raw_input())
13+
print type(pow(10,100))
14+
low = (pow(10, n-1))/210
15+
high = (pow(10,n) -1 )/210
16+
17+
18+
19+
20+
work()
21+

cf/154/B.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
const int MAXN = 100001;
8+
const int INF = 900000000;
9+
int N;
10+
11+
int d[MAXN];
12+
13+
void work(){
14+
/*
15+
for(int i=0; i<N; i++){
16+
cout<<data[i]<<" ";
17+
}cout<<endl;
18+
*/
19+
int ans = -INF;
20+
for(int i=0; i<N-1; i++){
21+
int low = i+1;
22+
int high = N-1;
23+
int tmp_ans = -1;
24+
int mid;
25+
//cout<<"at "<<i<<endl;
26+
while(low<=high){
27+
//cout<<"low high = "<<low<<" "<<high<<endl;
28+
mid = (low+high)/2;
29+
if(d[mid]*1.0/d[i]<=2){
30+
low = mid+1;
31+
tmp_ans = mid;
32+
}else{
33+
high = mid-1;
34+
}
35+
}
36+
if(tmp_ans!=-1){
37+
ans = max(tmp_ans - i+1, ans);
38+
}
39+
}
40+
//cout<<"ans = "<<ans<<endl;
41+
cout<<N-ans<<endl;
42+
43+
}
44+
45+
int main(){
46+
cin>>N;
47+
for(int i=0; i<N; i++){
48+
cin>>d[i];
49+
}
50+
sort(d, d+N);
51+
work();
52+
53+
return 0;
54+
}
55+

cf/154/a.out

17.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)