头像

defineシツ0x7fffffff




离线:10小时前


最近来访(1)
用户头像
ljlhandsome

活动打卡代码 LeetCode 1282. 用户分组

class Solution {
public:
    vector<int>seq[510],tmp;
    vector<vector<int>>res;
    vector<vector<int>> groupThePeople(vector<int>& g) {
        for(int i=0;i<(int)g.size();++i){
            seq[g[i]].push_back(i);
            if(seq[g[i]].size()==g[i])
            {
                for(int j=0;j<g[i];++j)tmp.push_back(seq[g[i]][j]);
                seq[g[i]].resize(0),res.push_back(tmp),tmp.resize(0);
            }
        }
        return res;
    }
};


活动打卡代码 AcWing 1866. 围栏刷漆

#include <cstring>
#include <algorithm>

using namespace std;
int main(){
    int a, b, c, d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    if (b < c or a > d) printf("%d", b - a + d - c);
    else printf("%d", max(b, d) - min(a, c));
    return 0;
}



两次深搜,可能有点多余,但是过了。

class Solution {
public:
int depp=-1,sum=0,res=0;
    void dfs(TreeNode*node,int dep){//找最深深度->比对深度累积答案->返回值
        if(!node->left and !node->right)return (void)(depp=max(depp,dep));
        if(node->left)dfs(node->left,dep+1);
        if(node->right)dfs(node->right,dep+1);
    }
    int Dfs(TreeNode* node,int dep)
    {
        if(!node->left and !node->right)
        {
            if(depp==dep)return node->val;
            else return 0;
        }
        int ans=0;
        if(node->left)ans+=Dfs(node->left,dep+1);
        if(node->right)ans+=Dfs(node->right,dep+1);
        return ans;
    }
    int deepestLeavesSum(TreeNode* root){
       dfs(root,1);
       res=Dfs(root,1);return res;
    }
};


活动打卡代码 LeetCode 1656. 设计有序流

class OrderedStream {
public:
vector<string>sp;
vector<string>z;
int ptr=1;//注意是从1开始
    OrderedStream(int n) {sp.resize(n+1);}//可以开大一点,防止越界

    vector<string> insert(int id, string val) {
        sp[id]=val;//先插入值
        z.resize(0);
        if(sp[ptr]=="")return z;
        else
        {
            while(ptr<sp.size() and sp[ptr]!="")z.push_back(sp[ptr++]);
            return z;
        }
    }
};


活动打卡代码 AcWing 4263. 走路回家

#pragma GCC optimize (2)
#pragma G++ optimize (2)//不加会有点慢

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<bitset>
using namespace std;
int t,n,k;char tmp;
bitset<55>g[55];
int dx[]={0,1},dy[]={1,0};
inline int read()
{
    int res=0,c,f=1;c=getchar();
    while(!isdigit(c))(c=='-')&&(f=-1),c=getchar();
    while(isdigit(c))res=res*10+c-48,c=getchar();
    return res*f;
}
int play(int x,int y,int tim,int las){
    //1,1->n,n
    if(x==n and y==n)return 1;
    if(tim>k||(tim==k and x!=n and y!=n))return 0;//剪掉啊剪掉啊
    int res=0;
    for(int i=0;i<2;++i){//需要记录上一次的
        int xx=dx[i]+x,yy=dy[i]+y;
        if(xx<1||xx>n||yy<1||yy>n||g[xx][yy]||tim+(las!=i)>k)continue;
        res+=play(xx,yy,tim+(las!=i),i);
    }
    return res;
}
int main()
{
    t=read();
    while(t--){
        n=read(),k=read();
        for(int i=1;i<=n;++i,getchar())//这个找了也蛮久的
        for(int j=1;j<=n;++j)tmp=getchar(),g[i][j]=tmp=='H';
        printf("%d\n",play(1,1,-1,-1));
    }
    return 0;
}


活动打卡代码 AcWing 4262. 空调

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+10;
int diff[N],del[N];
int p[N],t[N],maxn,minn;
int main()
{
    int n;cin>>n;
    for(int i=1;i<=n;++i)scanf("%d",p+i);
    for(int i=1;i<=n;++i){
    scanf("%d",t+i),del[i]=p[i]-t[i];
    diff[i]=del[i]-del[i-1];
    diff[i]>=0?(maxn+=diff[i]):(minn-=diff[i]);
    }
    printf("%d",max(maxn,minn));
    return 0;
}


活动打卡代码 AcWing 4261. 孤独的照片

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
int cd[27],c1[27],c2[27];
int main()
{
    string tmp;
    int n;scanf("%d",&n);
    while(n--)
    {
        cin>>tmp;
        for(auto c:tmp)c1[c-'a'+1]++;
        cin>>tmp;
        for(auto c:tmp)c2[c-'a'+1]++;
        for(int i=1;i<=26;++i)cd[i]+=max(c1[i],c2[i]),c1[i]=c2[i]=0;
    }
    for(int i=1;i<=26;++i)printf("%d\n",cd[i]);
    return 0;
}



解法

运用到了 multimap 和 pair。

C++ 代码

#include<cstdio>
#include<algorithm>
#include<map>
#define m(x,y) make_pair(x,y)//减少打字量
using namespace std;
using pii = pair<int, int>;
multimap<pii, int>g;//存下所有点
int res[5];//0-4有5个数
int main(){
    int n, x, y; scanf("%d", &n);//输入坐标数
    for (int i = 1; i <= n; ++i)
        scanf("%d%d", &x, &y), g.insert(m(m(x, y), 1));//将坐标加入集合之中
    for (auto c = g.begin(); c != g.end(); c++) {//离线遍历集合
        auto coor = c->first; int x = coor.first, y = coor.second;//取出坐标
        auto f1 = g.find(m(x + 1, y)), f2 = g.find(m(x - 1, y)),
             f3 = g.find(m(x, 1 + y)), f4 = g.find(m(x, y - 1));//不容易错的写法
        if (f1 != g.end() and f2 != g.end() and f3 != g.end() and f4 != g.end())
        {//当前点的上下左右均在集合之中
            int tmp = 0;//为了算出桶数组的标号
            f1 = g.find(m(x + 1, y + 1)), f2 = g.find(m(x + 1, y - 1)),
            f3 = g.find(m(x - 1, y + 1)), f4 = g.find(m(x - 1, y - 1));
            if (f1 != g.end())tmp++;if (f2 != g.end())tmp++;
            if (f3 != g.end())tmp++;if (f4 != g.end())tmp++;
            res[tmp]++;}
    }
    for (int i = 0; i <= 4; ++i, puts(""))printf("%d", res[i]);//输出答案
    return 0;}




lambda表达式 与map

真的很简单,统计之后排一下序就好了

C++ 代码

#include<iostream>
#include<algorithm>
#include<map>

using namespace std;
typedef struct het{int num,tims;}hey;
int main()
{
    int n;cin>>n;
    map<int,int>sp;
    int s;
    for(int i=0;i<n;++i){cin>>s;sp[s]+=1;}
    int N=sp.size()+1;

    hey *ss=new hey[N];
    int cnt=0;
    for(map<int,int>::iterator it=sp.begin();it!=sp.end();++it)
        ss[++cnt].num=(*it).first,ss[cnt].tims=(*it).second;

    sort(ss+1,ss+N,[](hey a,hey b){//排序的依据
        if(a.tims==b.tims)return a.num<b.num;
        return a.tims>b.tims;
    });//lambda表达式长的话可以分行,不过写完要记得放分号

    for(int i=1;i<N;++i)
        cout<<ss[i].num<<" "<<ss[i].tims<<endl;
    return 0;
}