Codeforces Round #570 (Div. 3)

这次div3的题挺有水平的。至少我这么觉得。
(后面三题都没做出来哭了。

F. Topforces Strikes Back

题意:从序列中找出最多三个互不整除的数,使之和最大。

题解:
假设我们枚举到第 i i i个数为最大数,我们选取不能整除的最大数,再对第二个数选取不能被整除的最大数。排序即可实现。
正确性呢?
对于选择的 a 、 b 、 c a、b、c abc,如果对于 c c c,我们选择 b ‘ &lt; b b`&lt;b b<b,因为 b b b是最大数,是否会出现 a ‘ + b ‘ &gt; a + b a`+b`&gt;a+b a+b>a+b,前题条件,如果出现, a &gt; a ‘ a&gt;a` a>a,但是如果出现这个的话,选择完 b b b的 时候应该选择 a ‘ a` a才是,因为都是不被整除,而且更大。

代码很简单的。但是我写的另一种处理方法,每次用 s e t set set存放可以拿的数,每次选择都删去其因数。
同时要预处理因数,又长又慢。好代码都在 c o d e f o r c e s codeforces codeforces上,直接去看即可,大部分都是三重循环找,及时 b r e a k break break
复杂度玄学。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

int T,n;
int A[200050];
vector<int>vc[200050];
vector<int>del;
set<int>st;

void init(){
    for(int i=2;i<=200000;i++)
        for(int j=i;j<=200000;j+=i){
            vc[j].push_back(i);
        }
}

void recover(int index){
    st.insert(A[index]);
    while(!del.empty()){
        st.insert(del.back());
        del.pop_back();
    }
}

int main(){
    cin>>T;
    init();
    FOR(time,1,T){
        scanf("%d",&n);
        FOR(i,1,n)scanf("%d",&A[i]);
        sort(A+1,A+1+n);
        n=unique(A+1,A+1+n)-(A+1);
        int res=-1;
        del.clear();st.clear();
        FOR(i,1,n){
            //cout<<i<<":"<<endl;

            int tmp=A[i];
            res=max(res,tmp);
            for(auto it:vc[A[i]]){
                if(st.find(it)!=st.end()){
                    del.push_back(it);
                    st.erase(it);
                }
            }
            //for(auto x:vc[A[i]])cout<<x<<" ";puts("_____1");
            if(st.empty()){
                recover(i);
                continue;
            }
            int p1=*(--st.end());tmp+=p1;
            res=max(res,tmp);
            for(auto it:vc[p1]){
                if(st.find(it)!=st.end()){
                    del.push_back(it);
                    st.erase(it);
                }
            }
            //for(auto x:vc[A[i]])cout<<x<<" ";puts("_____2");
            if(st.empty()){
                recover(i);
                continue;
            }
            int p2=*(--st.end());tmp+=p2;
            res=max(res,tmp);
            recover(i);
        }
        printf("%d\n",res);
    }
}

H - Subsequences (hard version)

题意:从字符串中挑选 k k k个尽可能长的子串,选择 d p dp dp来解决。

题解: d p [ i ] [ j ] dp[i][j] dp[i][j]表示考虑到第 i i i位长度为 j j j的子串个数。
d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + d p [ i − 1 ] [ j − 1 ] dp[i][j]=dp[i-1][j]+dp[i-1][j-1] dp[i][j]=dp[i1][j]+dp[i1][j1]这样会计上重复。

如何去重呢?比较讲究。
我们每次记录最近一次该字符出现的位置。减去之前那部分的以该字符为结尾的串个数。

为什么可行呢?
考虑 a a a出现在 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4位置, x x x为未知字符。
x x x x x 1 x x x 2 x x x 3 x x x x 4 x x x x x x x xxx1xxx2xxx3xxxx4xxxxx xxxxx1xxx2xxx3xxxx4xxxxx
我们容易发现,如果每次都把前面的删去。
1 1 1:含 1 1 1前面字符并以 a a a结尾的串。
2 2 2:含 2 2 2前面字符并以 a a a结尾的串+含 1 1 1前面字符并以 a a a为结尾的串个数,同时加上之前已经组合好的长度满足的串。
如果此时我们直接减去,那么此时答案符合。对于下次又会多加一份已经组合过的串。
但是这么减去另一个好处就是,每次都只会多加一份组合过的串,所以方便删除。如果不及时减去,那么会反复叠加难以计算。

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

int n;
ll k;
int last[2000];
ll dp[202][202];
string str;

int main(){
    cin>>n>>k>>str;
    memset(last,0,sizeof(last));
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;//空字符串时为1
    FOR(i,1,n){
        dp[i][0]=1;//考虑到第i位的时候也需要加上空字符串,因为转移方程没有
        FOR(j,1,i){
            dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
            if(last[str[i-1]])dp[i][j]-=dp[last[str[i-1]]-1][j-1];
        }
        last[str[i-1]]=i;
    }
    ll ans=0;
    for(int i=n;i>=0;i--){
        ll tmp=min(dp[n][i],k);
        ans+=tmp*(n-i);
        k-=tmp;
        if(!k)break;
    }
    if(k)puts("-1");
    else printf("%lld\n",ans);
}

G - Candy Box (hard version)

题意:选择多个数,记录相同的数的个数,对于每个选择的数,相同个数是不同的,同时在选择数的个数最大的时候,保证这些数的另一个值总和也是最大(另一个值是 1 / 0 1/0 1/0

题解:预处理出所有数字的个数,肯定优先取最多个数的。或者我们考虑最多个数为 n n n,反向遍历,每次把个数满足放进优先队列中,然后选取另一个值最大的,加上贡献。
每次加进去的,肯定对于后面依然可以。所以这样是最优的。

`#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
using namespace std;

int T,n;
int mark[200050];
map<int,int>M1,M2;
vector<pair<int,int> >vc;
priority_queue<int>pq;

int main(){
    cin>>T;
    FOR(time,1,T){
        M1.clear();M2.clear();vc.clear();
        pq=priority_queue<int>();
        scanf("%d",&n);
        FOR(i,1,n)mark[i]=0;
        FOR(i,1,n){
            int x,y;
            scanf("%d%d",&x,&y);
            M1[x]++,M2[x]+=y;
        }
        for(auto it:M1)vc.push_back(make_pair(it.second,M2[it.first]));
        sort(vc.begin(),vc.end());
        int ansl=0,ansr=0;
        for(int i=n;i>=1;i--){
            while(!vc.empty()&&vc.back().first>=i){
                pq.push(vc.back().second);
                vc.pop_back();
            }
            if(pq.empty())continue;
            ansl+=i,ansr+=min(i,pq.top());
            pq.pop();
        }
        printf("%d %d\n",ansl,ansr);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值