头像

gongcharlie


访客:2290

离线:55分钟前


新鲜事 原文

五分钟打完二分图祭 https://www.acwing.com/problem/content/submission/code_detail/2017430/


新鲜事 原文

这周四的leetcode题好水,两小时全A了


新鲜事 原文

gongcharlie
1个月前
期末测试圆满结束!o(* ̄▽ ̄*)ブ


新鲜事 原文

gongcharlie
2个月前
class Solution { public: int f[75][75][75]; int cherryPickup(vector<vector<int>>& g) { int n=g.size(),m=g[0].size(); for (int i=0;i<n;++i) { for (int j=0;j<m;++j) { for (int k=0;k<m;++k) { f[i][j][k]=-1; } } } f[0][0][m-1]=g[0][0]+g[0][m-1]; for(int i=0;i<n-1;++i) { for (int j=0;j<m;++j) { for (int k=0;k<m;++k) { int x=f[i][j][k]; if(x<0) continue; for(int l=max(j-1,0);l<min(m,j+2);++l) { for(int r=max(k-1,0);r<min(m,k+2);++r) { int now=g[i+1][l]+g[i+1][r]; if(l==r) now-=g[i][l]; f[i+1][l][r] = max(f[i+1][l][r],x+now); } } } } } int ans=0; for (int i=0;i<m;i++) { for(int j=0;j<m;j++) { ans = max(ans,f[n-1][i][j]); } } return ans; } }; 题目:https://leetcode-cn.com/problems/cherry-pickup-ii/ 求帮助!



gongcharlie
2个月前

什么鬼.PNG
代码:


int a[100000];
class Solution {
public:
    bool canBeEqual(vector<int>& target, vector<int>& arr) {

        for(int i=0;i<target.size();i++){
            a[target[i]]++;
        }
        for(int i=0;i<arr.size();i++){
            a[arr[i]]--;
        }
        for(int i=1;i<=1001;i++){
            if(a[i]!=0) return false;
        }
        return true;
    }
};

题目:https://leetcode-cn.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/submissions/




gongcharlie
3个月前

你告诉我“基于时间的分治算法”这个我连听说都没听说过的算法是一个简单题?!?!
就例如这个: https://www.acwing.com/problem/content/269/



活动打卡代码 AcWing 181. 回转游戏

gongcharlie
4个月前
//这里填你的代码^^#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define debug cout<<"ok"<<endl;
#define ll long long
using namespace std;
int op[10][10]={
    {0,2,6,11,15,20,22},
    {1,3,8,12,17,21,23},
    {10,9,8,7,6,5,4},
    {19,18,17,16,15,14,13},
    {23,21,17,12,8,3,1},
    {22,20,15,11,6,2,0},
    {13,14,15,16,17,18,19},
    {4,5,6,7,8,9,10}
};
int opposite[10]={5,4,7,6,1,0,3,2};
int center[10]={6,7,8,11,12,15,16,17};
int path[100],q[25];
int f(){
    int sum[4]={0};
    int maxx=0;
    for(int i=0;i<8;i++)sum[q[center[i]]]++;
    for(int i=1;i<=3;i++)maxx=max(maxx,sum[i]);
    return 8-maxx;
}
void operate(int x){
    int t=q[op[x][0]];
    for(int i=0;i<6;i++)
        q[op[x][i]]=q[op[x][i+1]];
    q[op[x][6]]=t;
}
bool dfs(int depth,int max_depth,int last){
    if(depth+f()>max_depth)return false;
    if(!f())return true;
    for(int i=0;i<8;i++){
        if(opposite[i]!=last){
            operate(i);
            path[depth]=i;
            if(dfs(depth+1,max_depth,i))
                return true;
            operate(opposite[i]);
        }
    } 
    return false;
}
int main()
{
    while(cin>>q[0]&&q[0]){
        for(int i=1;i<24;i++)
            scanf("%d",&q[i]);
        int depth=0;
        while(!dfs(0,depth,-1))
            depth++;
        if(!depth)
            printf("No moves needed");
        else{
            for(int i=0;i<depth;i++)
                printf("%c",(path[i]+'A'));
        }
        printf("\n%d\n",q[6]);
    }
    return 0;
}
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


活动打卡代码 AcWing 180. 排书

gongcharlie
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define debug cout<<"ok"<<endl;
#define ll long long
using namespace std;

const int N=15;
int q[N];
int w[5][N];
int T,n;

int f(){
    int cnt=0;
    for(int i=0;i+1<n;i++){
        if(q[i+1]!=q[i]+1) cnt++;
    }
    return (cnt+2)/3;
}

bool dfs(int u,int depth){
    if(u+f()>depth) return false;
    if(!f())return true;
    for(int len=1;len<n;len++){
        for(int l=0;l+len-1<n;l++){
            int r=l+len-1;
            for(int k=r+1;k<n;k++){
                memcpy(w[u],q,sizeof(q));
                int y=l;
                for(int x=r+1;x<=k;x++,y++) q[y]=w[u][x];
                for(int x=l;x<=r;x++,y++) q[y]=w[u][x];
                if(dfs(u+1,depth)) return true;
                for(int i=0;i<N;i++){
                    q[i]=w[u][i];
                } 
            }
        }
    }
    return false;
}

int main(){
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>q[i];
        }
        int depth=0;
        while(depth<5&&!dfs(0,depth)){
            depth++;
        }
        if(depth>=5) cout<<"5 or more\n";
        else cout<<depth<<endl;
    }
    return 0;
}



活动打卡代码 AcWing 168. 生日蛋糕

gongcharlie
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define debug cout<<"ok"<<endl;
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;

int n,m,S=INF;
int v=0,s=0;
int r[21]={INF},h[21]={INF};
int mins[22],minv[22];

void dfs(int i){
    if(i==m+1){
        if(v==n)
            S=min(S,s);
        return;
    }
    if(v+minv[m-i+1]>n||s+mins[m-i+1]>=S)return;
    if(i!=1&&s+2*(n-v)/r[i-1]>S)return;
    for(r[i]=min(r[i-1]-1,int(sqrt(double(n-v))));r[i]>=m-i+1;r[i]--){
        for(h[i]=min(h[i-1]-1,(n-v)/(r[i]*r[i]));h[i]>=m-i+1;h[i]--){
            if(i==1)
                s=r[i]*r[i];
            s+=2*r[i]*h[i];
            v+=r[i]*r[i]*h[i];
            dfs(i+1);
            s-=2*r[i]*h[i];
            v-=r[i]*r[i]*h[i];
        }
    }
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        mins[i]=mins[i-1]+1*i*i;
        minv[i]=minv[i-1]+i*i*i;
    }
    dfs(1);
    if(S!=INF)
        printf("%d",S);
    else
        printf("0");
    return 0; 
}


活动打卡代码 AcWing 167. 木棒

gongcharlie
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define debug cout<<"ok"<<endl;
#define ll long long
using namespace std;
const int N=100;
int a[N],n,m,max_val,sum,i,len,cnt,vis[N],true_n;
int cmp(int a,int b){
    return a>b;
}
int dfs(int cnm,int cab,int last){
    if(cnm>cnt) return true;
    if(cab==len) return dfs(cnm+1,0,1);
    int fail=0;
    for(int i=last;i<=true_n;i++){
        if(!vis[i]&&a[i]!=fail&&cab+a[i]<=len){
            vis[i]=1;
            if(dfs(cnm,cab+a[i],i+1)){
                return true;
            }
            fail=a[i];
            vis[i]=0;
            if(cab==0||cab+a[i]==len){
                return false;
            }
        }
    } 
    return false;
}
int main()
{
    while(cin>>n&&n){
        int x;
        max_val=0,sum=0;true_n=0;
        for(i=1;i<=n;i++){
            cin>>x;
            if (x>50)
                continue;
            true_n++;
            a[true_n]=x;
            max_val=max(max_val,a[true_n]);
            sum+=a[true_n];
        }
        sort(a+1,a+1+true_n,cmp);
        for(len=max_val;len<=sum;len++)
        {
            if (sum%len)
                continue;
            cnt=sum/len;
            memset(vis,0,sizeof(vis));
            if (dfs(1,0,1))
                break;
        }
        cout<<len<<endl;
    }
    return 0;
}