CPS-J 9-24

· · 个人记录

T1七段密钥(seven)

#include<bits/stdc++.h>
using namespace std;
int main(){
    freopen("seven.in","r",stdin);
    freopen("seven.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long x;
    stack<int> ans;
    cin>>x;
    for(int j=1;j<=7;j++){
        int mod=x%256;
        ans.push(mod);
        x/=256;
    }
    while(!ans.empty()){
        cout<<ans.top()<<' ';
        ans.pop();
    }
    return 0;
}

一次AC

T2巡检记录(log)

死因:没开long long 见祖宗

#include<bits/stdc++.h>
using namespace std;
int main(){
    int len;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("log.in","r",stdin);
    freopen("log.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,d;
    cin>>n>>d;
    if(n==1){
        cout<<1;
        return 0; 
    }
    long long ans=0;
    int a[200010];
    memset(a,0x3f3f3f,sizeof(a));
    for(int i=1;i<=n;i++)   cin>>a[i];
    int l=1,r=1;
    while(l<n){
        if(a[r]-a[r+1]==d) r++;
        else{
            len=r-l+1;
            ans+=((len+1)*len/2);
            r++;
            l=r;
            if(r==n and l==n) ans+=1;
        }
    }
    cout<<ans;
    return 0;
}

AC代码

#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
signed main(){
    int len;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("log.in","r",stdin);
    freopen("log.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,d;
    cin>>n>>d;
    if(n==1){
        cout<<1;
        return 0; 
    }
    int ans=0;
    int a[200010];
    memset(a,0x3f3f3f,sizeof(a));
    for(int i=1;i<=n;i++)   cin>>a[i];
    int l=1,r=1;
    while(l<n){
        if(a[r]-a[r+1]==d) r++;
        else{
            len=r-l+1;
            ans+=((len+1)*len/2);
            r++;
            l=r;
            if(r==n and l==n) ans+=1;
        }
    }
    cout<<ans;
    return 0;
}

T3加速清空(clean)

死因:没判断时间复杂度导致TLE WA:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[500005];
int n,x,y;
bool check(int p){
    int ans=0;
    for(int i=1;i<=n;i++){
        if(a[i]-p*x>0)ans+=((a[i]-p*x)%y==0)?(a[i]-p*x)/y:(a[i]-p*x)/y+1;
    }
    if(ans>p) return false;
    return true;
}
signed main(){
    freopen("clean.in","r",stdin);
    freopen("clean.out","w",stdout);
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int t=1;t<=n;t++){
        if(check(t)) {
            cout<<t;
            return 0;
        }
    }
}

AC:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,x,y,a[500005];
bool check(int p){
    int s=0;
    for(int i=1;i<=n;i++){
        int r=a[i]-p*x;
        if(r>0)s+=(r+y-1)/y;
        if(s>p)return 0;
    }
    return s<=p;
}
signed main(){
    freopen("clean.in","r",stdin);
    freopen("clean.out","w",stdout);
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++)cin>>a[i];
    int l=0,r=2e9,ans=r;
    while(l<=r){
        int m=l+(r-l)/2;
        if(check(m))ans=m,r=m-1;
        else l=m+1;
    }
    cout<<ans;
}

T4样品分装(sample)

死因:没推出转移方程,骗分15' WA不贴; AC:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,a[300005],dp[300005];
signed main(){
    freopen("sample.in","r",stdin);
    freopen("sample.out","w",stdout);
    cin>>n>>k;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    memset(dp,0x3f,sizeof dp);
    dp[0]=0;
    int mn=1e18;
    for(int i=k;i<=n;i++){
        mn=min(mn,dp[i-k]-a[i-k+1]);
        dp[i]=a[i]+mn;
    }
    cout<<dp[n];
}