codeforces1457 C. Bouncing Ball

I wrote this inscription for 1h. I didn’t intend to see the code of God Jly after the game and I could only worship %%%

C. Bouncing Ball

Preprocessing from 1 → k 1\to k1k began to jump the number of platforms you need to add, at the pre-k + 1 → n k + 1k+1n These are not difficult to find because every time you jump k grids, you only need to use the prefix and ideas and the results of the previous preprocessing to do the difference.
Then enumerate the number of deleted platforms, and use the preprocessing result to directly ask for the answer.
Time complexityO (n) O(n)O ( n )
There is a dt in how to use the array to store the preprocessing result, we need this size to be an arraycnt[k][n/k]. cnt[i][j]It means thatone step is skippedfrom theibeginning to the back. Obviously the array cannot be opened, here is a dynamic arraystoragejjk
vector

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=100010;

int main()
{
    
    
    //IO;
    int T=1;
    cin>>T;
    while(T--)
    {
    
    
        
        int n,p,k;cin>>n>>p>>k;
        string s;cin>>s;s="0"+s;
        ll x,y; cin>>x>>y;
        
        ll res=1e18;
        vector<vector<int> > cnt(k+1,vector<int>(n/k+1,0));
        for(int i=1;i<=k;i++)
            for(int j=i;j<=n;j+=k)
            {
    
    
                if(j!=i) cnt[i][(j-i)/k]=cnt[i][(j-i)/k-1];
                if(s[j]=='0') cnt[i][(j-i)/k]++;
            }
            
        for(int i=1;i<=n;i++)
        {
    
    
            ll now=y*(i-1);
            int j=i+p-1;
            if(j>n) continue;
            int w=(j-1)%k+1;
            
            int l=(j-w)/k,r=(n-w)/k;
            now+=x*cnt[w][r];
            if(l>0) now-=x*cnt[w][l-1];
            
            res=min(res,now);
        }
        cout<<res<<'\n';
    }
    return 0;
}

The
general idea of ​​the jly code is to calculate the number of platforms that need to be added to jump backward from a certain point from back to front , and then enumerate and delete the number of platforms.

#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=100010;
int main()
{
    
    
    //IO;
    int T=1;
    cin>>T;
    while(T--)
    {
    
    
        int n,p,k;
        cin>>n>>p>>k;
        --p;
        string s;
        cin>>s;
        int x,y;
        cin>>x>>y;
        int ans=1e9;
        vector<int> dp(n);
        for(int i=n-1;i>=0;i--)
        {
    
    
            dp[i]=(s[i]=='0')?x:0;
            if(i+k<n) dp[i]+=dp[i+k];
        }
        for(int i=p;i<n;i++) ans=min(ans,(i-p)*y+dp[i]);
        cout<<ans<<'\n';
    }
    return 0;
}

The thinking is still not good. Other people's 10 minutes A's question, I almost missed it in 1 hour. .
Come on~

Guess you like

Origin blog.csdn.net/Fighting_Peter/article/details/110357592