Dashboard - The 2021 China Collegiate Programming Contest (Harbin) - Codeforces
d p [ i ] [ j ] 表示组成数字 i ,数列前 j 位的最大贡献。 dp[i][j] 表示组成数字 i , 数列前 j 位的最大贡献。 dp[i][j]表示组成数字i,数列前j位的最大贡献。
d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i − 1 ] [ j ] ) dp[i][j] = max(dp[i][j]~,~dp[i - 1][j]) dp[i][j]=max(dp[i][j] , dp[i−1][j])
d p [ i ] [ j ] = m a x ( d p [ i ] [ j ] , d p [ i ] [ p o s [ i − a [ j ] ] − 1 ] + 2 ) dp[i][j] = max(dp[i][j]~,~dp[i][pos[i-a[j]]-1]+2) dp[i][j]=max(dp[i][j] , dp[i][pos[i−a[j]]−1]+2)
时间复杂度 O ( 200 n ) 时间复杂度O(200n) 时间复杂度O(200n)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int n , a[N] , maxx;
int pos[310] , dp[210][N];
/*
和为 i 前 j 个数的最大贡献
*/
signed main(){
IOS
cin >> n;
for(int i = 1 ; i <= n ; i ++ ) cin >> a[i];
for(int i = 2 ; i <= 200 ; i ++) {
for(int j = 1 ; j <= 100 ; j ++) pos[j] = 0;
for(int j = 1 ; j <= n ; j ++) {
if(a[j] >= i || !pos[i - a[j]]) dp[i][j] = dp[i][j - 1];
else dp[i][j] = max(dp[i][j - 1] , dp[i][pos[i - a[j]] - 1] + 2);
pos[a[j]] = j;
}
}
for(int i = 2 ; i <= 200 ; i ++) maxx = max(maxx , dp[i][n]);
cout << maxx << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n 2 n l o g ( 2 n ) ) 时间复杂度O(n2^nlog(2^n)) 时间复杂度O(n2nlog(2n))
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 30 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , a[N] , b[N] , n , m , st , ed;
string s1 , s2;
signed main(){
IOS
cin >> t;
while(t --){
cin >> s1 >> s2;
n = s1.size();
m = s2.size();
st = ed = 0;
for(int i = 0 ; i < n ; i ++) a[i] = s1[i] - '0' , st = st * 10 + a[i];
for(int i = 0 ; i < m ; i ++) b[i] = s2[i] - '0' , ed = ed * 10 + b[i];
set<pair<string , int>>st1;
for(int i = 1 ; i <= (1ll << n) ; i++){
int now = 0;
string del;
for(int j = 0 ; j < n ; j ++){
if((i >> j) & 1){
del += a[j] + '0';
}else{
now = now * 10 + a[j];
}
}
sort(del.begin() , del.end());
st1.insert({del , now});
}
int ans = 9e18 , els = 0;
for(int i = 1 ; i <= (1ll << m) ; i++){
int now = 0;
string del;
for(int j = 0 ; j < m ; j ++){
if((i >> j) & 1){
del += b[j] + '0';
}else{
now = now * 10 + b[j];
}
}
sort(del.begin() , del.end());
if(!now) continue;
if((__int128) st * now % (__int128) ed) continue;
int pre = (__int128) st * now / ed;
if(st1.find({del , pre}) != st1.end()){
if(pre < ans){
ans = pre;
els = now;
}
}
}
cout << ans << " " << els << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
A i = ( A i − 1 ∗ 2 ) % M ( A 1 = 0 ∣ ∣ A 1 = 1 ) A_i = (A_{i-1}*2)\%M(A_1=0||A_1 = 1) Ai=(Ai−1∗2)%M(A1=0∣∣A1=1)
时间复杂度 O ( n l o g n ) 时间复杂度O(nlogn) 时间复杂度O(nlogn)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n;
int a[N] , res;
bool judge(){
int cnt = 0;
for(int i = i = 1 ; i <= n ; i ++) if(!a[i]) cnt += 1;
if(cnt == n) {
res = 1;
return 1;
}
set<int>ans;
if(a[1] < 0 || a[1] > 1) return 0;
for(int i = 2 ; i <= n ; i ++){
if(a[i] >= a[i - 1]){
if(a[i - 1] * 2 != a[i]) return 0;
}else{
res = a[i - 1] * 2 - a[i];
ans.insert(a[i - 1] * 2 - a[i]);
}
}
if(ans.size() != 1) return 0;
else return 1;
}
signed main(){
IOS
cin >> t;
while(t --){
cin >> n;
for(int i = 1 ; i <= n ; i ++) cin >> a[i];
if(judge()){
cout << res << "\n";
}else{
cout << "-1\n";
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度不太会算,但是跑的飞快 时间复杂度不太会算 , 但是跑的飞快 时间复杂度不太会算,但是跑的飞快
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n;
int a[N] , bit[32];
signed main(){
IOS
cin >> t;
while(t --) {
cin >> n;
for(int i = 0 ; i <= 30 ; i ++) bit[i] = 0;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i];
for(int j = 0 ; j <= 30 ; j ++) {
if(a[i] >> j & 1) bit[j] += 1;
}
}
bool tag = true;
while(tag) {
tag = false;
for(int i = 30 ; i >= 1 ; i --) {
if(bit[i] > bit[i - 1]) {
int need = (bit[i] - bit[i - 1] - 1) / 3 + 1;
bit[i] -= need;
bit[i - 1] += need * 2;
tag = true;
}
}
}
cout << bit[0] << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( n m ) 时间复杂度O(nm) 时间复杂度O(nm)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int a[N][N] , n , m , res;
bool vis[N][N];
signed main(){
IOS
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++) {
for(int j = 1 ; j <= m ; j ++) {
cin >> a[i][j];
}
}
for(int i = 1 ; i <= n ; i ++) {
int minn = 9e18;
for(int j = 1 ; j <= m ; j ++) minn = min(minn , a[i][j]);
for(int j = 1 ; j <= m ; j ++) if(a[i][j] == minn) vis[i][j] = 1;
}
for(int j = 1 ; j <= m ; j ++) {
int minn = 9e18;
for(int i = 1 ; i <= n ; i ++) minn = min(minn , a[i][j]);
for(int i = 1 ; i <= n ; i ++) if(a[i][j] == minn) {
if(vis[i][j]) res += 1;
}
}
cout << res << "\n";
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
算法讨论小群 : 545214567(QQ)