突如其來的div3,賽后打了一下。
A - Floor Number
數學題答案是1+?n?2x?1+\lceil \frac{n-2}{x} \rceil1+?xn?2??
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std
;
int main()
{IO
;int T
=1;cin
>>T
;while(T
--){int n
,x
;cin
>>n
>>x
;if(n
<=2) cout
<<1<<'\n';elsecout
<<1+(n
-2+x
-1)/x
<<'\n';}return 0;
}
B - Symmetric Matrix
只要存在2×2小矩形是對稱的并且邊長是偶數就一定能夠拼成大矩形,否則不行。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std
;
int main()
{IO
;int T
=1;cin
>>T
;while(T
--){bool ok
=0;int n
,m
;cin
>>n
>>m
;while(n
--){int a
,b
,c
,d
;cin
>>a
>>b
>>c
>>d
;if(b
==c
) ok
=1;}if(m
%2) ok
=0;if(ok
) cout
<<"YES\n";else cout
<<"NO\n";}return 0;
}
C - Increase and Copy
稍微轉化一下,很明顯如果加1,只需要每次把a1a_1a1?,并且操作相同次數,先加1再復制結果一定更優。
此題有一個樣例提示了我:當n=1000000000n=1000000000n=1000000000,答案是632446324463244,說明移動步數非常少。由此我們可以枚舉+1的操作數然后就不難解決了。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std
;
const int N
=300010;
int main()
{IO
;int T
=1;cin
>>T
;while(T
--){int n
;cin
>>n
;int res
=0x3f3f3f3f;for(int i
=0;i
<=70000;i
++){int now
=1;now
+=i
;if(now
>=n
){res
=min(res
,i
);break;}elseres
=min(res
,i
+(n
-now
+now
-1)/now
);}cout
<<res
<<'\n';}return 0;
}
E - Rock, Paper, Scissors
不難發現只需要找到互不相交,子串和為0的數量。
考慮前綴和數組sis_isi?,如果si=sjs_i=s_jsi?=sj?,說明ai+1+ai+2+?+aj=0a_{i+1}+a_{i+2}+\dots+a_j=0ai+1?+ai+2?+?+aj?=0
然后用個map記錄一下即可。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<map>
#include<iostream>
#include<algorithm>
using namespace std
;
typedef long long ll
;
const int N
=200010;
ll a
[N
],s
[N
];
int n
;
map
<ll
,int> mp
;
int main()
{IO
;int T
=1;while(T
--){cin
>>n
;mp
.clear();for(int i
=1;i
<=n
;i
++){cin
>>a
[i
];s
[i
]=s
[i
-1]+a
[i
];}int now
=0;int res
=0;mp
[0]=0;for(int i
=1;i
<=n
;i
++){if(mp
.count(s
[i
])){if(now
-1<=mp
[s
[i
]]){res
++;now
=i
;}}mp
[s
[i
]]=i
;}cout
<<res
<<'\n';}return 0;
}
D - Non-zero Segments
別問,問就是暴力
出牌順序就6種,消順序就8種,直接枚舉即可。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std
;
int n
;
int a
,b
,c
;
int e
,f
,g
;
int main()
{IO
;int T
=1;while(T
--){cin
>>n
;cin
>>a
>>b
>>c
;cin
>>e
>>f
>>g
;int cnt0
=0;for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now1
=min(a
,x
+z
);if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now1
=min(a
,x
+z
);int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}int now1
=min(a
,x
+z
);if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now1
=min(a
,x
+z
);if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now1
=min(a
,x
+z
);int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}for(int i
=0;i
<1<<3;i
++){int x
=e
,y
=f
,z
=g
;int now3
=min(c
,y
+z
);if(i
>>2&1){z
-=y
-max(0,y
-now3
);y
=max(0,y
-now3
);}else{y
-=z
-max(0,z
-now3
);z
=max(0,z
-now3
);}int now2
=min(b
,y
+x
);if(i
>>1&1){x
-=y
-max(0,y
-now2
);y
=max(0,y
-now2
);}else{y
-=x
-max(0,x
-now2
);x
=max(0,x
-now2
);}int now1
=min(a
,x
+z
);if(i
&1) {z
-=x
-max(0,x
-now1
);x
=max(0,x
-now1
);}else{x
-=z
-max(0,z
-now1
);z
=max(0,z
-now1
);}cnt0
=max(cnt0
,now1
+now2
+now3
);}int cnt1
=min(n
,min(a
,f
)+min(b
,g
)+min(c
,e
));cout
<<n
-cnt0
<<' '<<cnt1
<<'\n';}return 0;
}
F - Number of Subsequences
首先不考慮問號,我們求子序列abc的個數可以用動態規劃設計狀態
狀態表示:
f(i,0)f_{(i,0)}f(i,0)?表示考慮前iii個字符,子序列aaa的數目
f(i,1)f_{(i,1)}f(i,1)?表示考慮前iii個字符,子序列ababab的數目
f(i,2)f_{(i,2)}f(i,2)?表示考慮前iii個字符,子序列abcabcabc的數目
狀態轉移:
如果當前字符是aaa,那么f(i,0)=f(i?1,0)+1f_{(i,0)}=f_{(i-1,0)}+1f(i,0)?=f(i?1,0)?+1
如果當前字符是bbb,那么f(i,1)=f(i?1,1)+f(i,0)f_{(i,1)}=f_{(i-1,1)}+f_{(i,0)}f(i,1)?=f(i?1,1)?+f(i,0)?
如果當前字符是ccc,那么f(i,2)=f(i?1,2)+f(i,1)f_{(i,2)}=f_{(i-1,2)}+f_{(i,1)}f(i,2)?=f(i?1,2)?+f(i,1)?
此題目非常dt的地方就是因為問號的3種情況,導致有些轉移不是那么簡單
一個問號的3種情況會使得原來的f(i,0/1/2)f_{(i,0/1/2)}f(i,0/1/2)?翻3倍,而對于f(i,0)f_{(i,0)}f(i,0)?的轉移會使得+1+1+1變成+pk(k是之前出現?的數目)+p^k(k是之前出現?的數目)+pk(k是之前出現?的數目)
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<iostream>
#include<algorithm>
using namespace std
;
typedef long long ll
;
const int N
=200010;
const ll mod
=1e9+7;
char s
[N
];
ll f
[N
][3];
int main()
{IO
;int T
=1;while(T
--){int n
;cin
>>n
;cin
>>s
+1;ll p
=1;for(int i
=1;i
<=n
;i
++){if(s
[i
]=='?'){f
[i
][0]=(3ll*f
[i
-1][0]+p
)%mod
;f
[i
][1]=(3ll*f
[i
-1][1]+f
[i
-1][0])%mod
;f
[i
][2]=(3ll*f
[i
-1][2]+f
[i
-1][1])%mod
;p
=p
*3%mod
;}else if(s
[i
]=='a') {f
[i
][0]=(f
[i
-1][0]+p
)%mod
;f
[i
][1]=f
[i
-1][1];f
[i
][2]=f
[i
-1][2];}else if(s
[i
]=='b'){f
[i
][0]=f
[i
-1][0];f
[i
][1]=(f
[i
-1][1]+f
[i
-1][0])%mod
;f
[i
][2]=f
[i
-1][2];}else {f
[i
][0]=f
[i
-1][0];f
[i
][1]=f
[i
-1][1]; f
[i
][2]=(f
[i
-1][2]+f
[i
-1][1])%mod
;}}cout
<<f
[n
][2]<<'\n';}return 0;
}
要加油哦~
總結
以上是生活随笔為你收集整理的Codeforces Round #674 (Div. 3)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。