0%

思路讲解

题解还用了二进制分析?我看不懂,但我大受震撼。(做的时候没看)

我的理解是这样的,首先无关痛痒的操作,一定是交给floor做会往大里走,交给ceil做会往小里走。

然后那如果事关痛痒的操作,那么还是先floor做,再ceil做大,反之就小。

如果要系统的解释还是要依赖二进制。其实答案要么是 x/2n+mx/2^{n+m},要么是其+1。我们来解释一下。

image

我们发现,如果要将一个进位传递过去,会消耗所有路径上的1,因此并不会出现累加的情况。

其他就看官解吧,讲的很清楚。

https://codeforces.com/blog/entry/140702

主要就是要注意,ceil的话opX=1的时候会卡住,需要对这一情况进行预判。

1
2
3
if(opX==1){
break;
}

AC代码

https://codeforces.com/contest/2082/submission/311004371

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Problem: B. Floor or Ceil
// Contest: Codeforces - Codeforces Round 1010 (Div. 2, Unrated)
// URL: https://codeforces.com/contest/2082/problem/B
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll X,N,M,T,A[MAXN];

inline void solve(){
cin>>X>>N>>M;
ll opM=M,opN=N,opX=X;
while(opX>0){
if(opX%2==0){
if(opM>0){
opX/=2;
--opM;
}else if(opN>0){
opX/=2;
--opN;
}else{
break;
}
}else{
if(opM>0){
// #ifdef LOCAL
// cerr << "op前 "<<opX << '\n';
// #endif
if(opX==1 && opN!=0){
opX=0;
break;
}else if(opX==1 && opN==0){
break;
}
opX=ceil(opX*1.0/2);
// #ifdef LOCAL
// cerr << opX << '\n';
// #endif
--opM;
}else if(opN>0){
opX/=2;
--opN;
}else{
break;
}
}
}
cout<<opX<<" ";
opM=M,opN=N,opX=X;
while(opX>0){
if(opX%2==0){
if(opN>0){
opX/=2;
--opN;
}else if(opM>0){
opX/=2;
--opM;
}else{
break;
}
}else{
if(opN>0){
opX/=2;
--opN;
}else if(opM>0){
if(opX==1){
break;
}
opX=ceil(opX*1.0/2);
--opM;
}else{
break;
}
}
}
cout<<opX<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/contest/2082/submission/311004371
*/

心路历程(WA,TLE,MLE……)

思路讲解

https://codeforces.com/contest/2052/submission/297107696

jianly的思路我觉得比官解更好(官解都看不懂是什么意思)

jianly的思路就是逆向思维,先将345612(这是输入进来的)转化为654321,再将654321转化为123456

整个过程我们都记录下来,reverse一下输出即可。

AC代码

https://codeforces.com/contest/2052/submission/311474567

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Problem: CF2052A Adrenaline Rush
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF2052A
// Memory Limit: 1000 MB
// Time Limit: 3000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e3)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,C[MAXN];

inline void solve(){
cin>>N;
FOR(i,1,N){
cin>>C[i];
}
vector<pll> ans;
// 先将345612(这是输入进来的)转化为654321
ROF(i,N,1){
ll pi=0;
FOR(j,1,N){
if(C[j]==i){
pi=j;
break;
}
}
ROF(j,pi-1,1){
if(C[j]<C[j+1]){
// 不仅顺序最后要倒过来,超车顺序也要倒过来
ans.pb({C[j],C[j+1]});
swap(C[j],C[j+1]);
}
}
}
FOR(i,1,N){
FOR(j,i+1,N){
ans.pb({j,i});
}
}
reverse(ans.begin(),ans.end());
cout<<SZ(ans)<<"\n";
FOR(i,0,SZ(ans)-1){
cout<<ans[i].fi<<" "<<ans[i].se<<"\n";
}
// #ifdef LOCAL
// FOR(i,1,N){
// cerr<<C[i]<<" ";
// }
// cerr<<"\n";
// #endif
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*
AC
https://codeforces.com/contest/2052/submission/311474567
*/

心路历程(WA,TLE,MLE……)

思路讲解

二分答案+树状数组

其实这个二分答案还是挺难想的,主要还是因为这个东西具有单调性,所以就可以搞二分答案。二分答案还是能节省很多时间。

AC代码

https://codeforces.com/problemset/submission/1843/310708365

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Problem: CF1843E Tracking Segments
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1843E
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,T,A[MAXN];
arr lr[MAXN];
ll modify[MAXN];
ll fiOcc[MAXN]; // 该值第一次出现的位置
ll tr[MAXN];
bool isHave[MAXN];
inline ll lowbit(ll x){
return x&(-x);
}
inline void add(ll p){
while(p<=N){
tr[p]+=1;
p+=lowbit(p);
}
}
inline ll query(ll l,ll r){
ll lres=0,rres=0;
l-=1;
while(l>0){
lres+=tr[l];
l-=lowbit(l);
}
while(r>0){
rres+=tr[r];
r-=lowbit(r);
}
return rres-lres;
}
inline bool check(ll x){
if(x==N+1) return true;
FOR(i,1,N){
tr[i]=0;
isHave[i]=false;
}
FOR(i,1,x){ // 保证不重复加
if(isHave[modify[i]]) continue;
isHave[modify[i]]=true;
add(modify[i]);
}
FOR(i,1,M){
ll len=lr[i][1]-lr[i][0]+1;
ll res=query(lr[i][0],lr[i][1]);
if(res>len-res){
return true;
}
}
return false;
}
inline void solve(){
cin>>N>>M;
FOR(i,1,M){
cin>>lr[i][0]>>lr[i][1];
}
ll Q;
cin>>Q;
FOR(i,1,Q){
ll x;
cin>>modify[i];
}
ll l=1,r=Q+1;
while(l<r){
ll mid=l+r>>1;
if(check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout<<(l!=Q+1?l:-1)<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/1843/310708365
*/

心路历程(WA,TLE,MLE……)

思路讲解

思路其实比较简单,但没那么容易想到,只需要看队尾连续的一即可。

这乍听起来有点奇怪,但其实是因为a ≥ 1,所以说这个在 j 时刻(这个j是指离结束时刻的距离)的操作无法完成 j-1的这个指标,那么就没人帮他完成了。

AC代码

https://codeforces.com/problemset/submission/2059/310691802

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Problem: CF2059C Customer Service
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF2059C
// Memory Limit: 250 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(3e2)+10,INF=static_cast<ll>(5e18)+3;

ll N,M,T,A[MAXN][MAXN];
// ll cur[MAXN];
// ll sumA[MAXN][MAXN];
ll suffix1[MAXN];

inline void solve(){
cin>>N;
for(int i=1;i<=N;++i){
for(int j=1;j<=N;++j){
cin>>A[i][j];
}
}
FOR(i,1,N){
ll cnt=0;
ROF(j,N,0){
if(A[i][j]!=1){
suffix1[i]=cnt;
break;
}
++cnt;
}
}
sort(suffix1+1,suffix1+1+N);
ll ans=0;
FOR(i,1,N){
if(suffix1[i]>=ans){
++ans;
}
}
// #ifdef LOCAL
// FOR(i,1,N){
// cerr<<suffix1[i]<<" ";
// }
// cerr<<"\n";
// #endif
cout<<ans<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/2059/310691802
*/

心路历程(WA,TLE,MLE……)

https://codeforces.com/problemset/submission/2059/310691236

这个的问题在于

1
2
3
4
1
2
1 1
1 1
1
2
3
4
5
6
7
8
9
10
FOR(i,1,N){
ll cnt=0;
ROF(j,N,0){
if(A[i][j]!=1){
suffix1[i]=cnt;
break;
}
++cnt;
}
}

这种break式样的,做赋值的,一定要想到如果一直满足情况怎么办?

思路讲解

image

这个是动态规划还是很明显的,但是是需要不等式优化的dp,因为裸的dp想到达到这道题目的O(n)时间复杂度还是有点困难的。

具体的来说,我们需要对机子进行排序,以让我们不需要考虑机子的顺序,按照排序顺序即可。

满足以下式子说明a1要排在前面。

a1(a2x+b2)+b1<a2(a1x+b1)+b2a1a2x+a1b2+b1<a1a2x+a2b1+b2a1b2+b1<a2b1+b2a1b2b2<a2b1b1a11b1<a21b2a_1(a_2x+b_2)+b_1 < a_2(a_1x+b_1) + b_2 \\a_1a_2x + a_1b_2 + b_1 < a_1a_2x + a_2b_1 + b_2 \\a_1b_2 + b_1 <a_2b_1 + b_2 \\a_1b_2 - b_2 < a_2b_1 - b_1 \\\cfrac{a_1 - 1}{b_1} < \cfrac{a_2 - 1}{b_2}

很明显,根据推导,该不等式具有传递性,因为a1,b1在一边,a2,b2在一边

然后动态规划部分其实就是从上面一个状态推下来,dp[i][j]表示考虑到第i个物品,选j个物品所能达到的最大值

这就是不考虑顺序(顺序已经是最优的情况)下,选K个物品时动态规划的套路

或者你还是理解不了,背包总理解吧?选K个物品不就是背包容量为K,每个物品重量为1吗?然后背包不也不用考虑顺序或者要求顺序已经最优吗?(其实是笔者自己不理解)

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// http://120.55.170.42/contest/1001/problem/L2-2
#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(5e18)+3;

ll N,K,T;
pll ab[MAXN];
ll dp[MAXN][15];

inline bool cmp(const pll &a,const pll &b){
ll a1=a.fi,b1=a.se,a2=b.fi,b2=b.se;
long double t2=(a2-1)*1.0l/b2,t1=(a1-1)*1.0l/b1;
return t1<t2;
}

inline void solve(){
cin>>N>>K;
for(int i=1;i<=N;++i){
cin>>ab[i].fi>>ab[i].se;
}
sort(ab+1,ab+1+N,cmp);
for(int i=0;i<=K;++i){
dp[0][i]=1;
}
for(int i=0;i<=N;++i){
dp[i][0]=1;
}
// 注意到,其实k是很小的
for(int i=1;i<=N;++i){
ll a=ab[i].fi,b=ab[i].se;
for(int j=1;j<=K;++j){
dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]*a+b);
}
}
cout<<dp[N][K]<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
/*

*/

心路历程(WA,TLE,MLE……)