Codeforces Round #451 (Div. 2) 划水报告

8 篇文章 0 订阅
5 篇文章 0 订阅

A.Rounding
题意:给出一个数x,求出和x差值绝对值最小的,%10==0的数
贪心向上取整向下取整即可

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int A[200000+10];
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n;
    scanf("%d",&n);
    if(n%10==0)
        printf("%d\n",n);
    else if(n%10<=5)
        printf("%d\n",(n/10)*10);
    else printf("%d\n",(n/10+1)*10);
return 0;
}

B. Proper Nutrition
给出a,b,n
ax+by==n 的非负整数解
裸的扩展欧几里得

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int A[200000+10];
typedef long long ll;
inline void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
    if(!b){
        d=a;
        x=1;
        y=0;
        return ;
    }
    else {
        exgcd(b,a%b,d,y,x);
        y-=(a/b)*x;
    }
}
inline ll ab(ll x){
    return x<0?-x:x;
}
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    ll n,a,b;
    scanf("%lld %lld %lld",&n,&a,&b);
    ll d,x,y;
    exgcd(a,b,d,x,y);
    //printf("%d\n",n%d);
    if(n%d!=0){
        puts("NO");
        return 0;
    }
    ll aa=a/d,bb=b/d;
    x*=(n/d),y*=(n/d);
    ll p=(x%bb+bb)%bb;
    ll tl=ab(x-p)/bb;
    if(x>p)
        y+=tl*aa;
    else y-=tl*aa;
    if(y>=0)
        printf("YES\n%lld %lld\n",p,y);
    else puts("NO");
return 0;
}

C. Phone Numbers
给出几段人名以及对应的几个字符串
将人名对应的字符串去重(若a为b的后缀a也算重复)输出
set判重,暴力判后缀

By Ostmbh, contest: Codeforces Round #451 (Div. 2), problem: (C) Phone Numbers, Accepted, #
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
using namespace std;
map<string,int>m;
struct T{
    string nme;
    string B[310];
    int vis[310];
    int cnt;
    int tt;
    T(){
        cnt=0;
        memset(vis,0,sizeof(vis));
    }
}A[30];
int tot=0;
inline bool judge(int x,int y,int z){
    int ly=A[x].B[y].length(),lz=A[x].B[z].length();
    if(lz>ly)
        return false;
    int i=1;
    while(i<=lz&&A[x].B[y][ly-i]==A[x].B[z][lz-i])
        i++;
    if(i==lz+1)
        return true;
    return false;
}
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n;
    scanf("%d",&n);
    int x;
    string s;
    for(int i=1;i<=n;i++){
        cin>>s;
        if(!m[s])
            m[s]=++tot;
        int u=m[s];
        A[u].nme=s;
        scanf("%d",&x);
        for(int j=1;j<=x;j++)
            cin>>A[u].B[++A[u].cnt];
    }
    //printf("%d\n",tot);
    for(int i=1;i<=tot;i++){
        //printf("%d\n",A[i].cnt);
        for(int j=1;j<=A[i].cnt;j++)
            for(int k=1;k<=A[i].cnt;k++){
                if(j==k)
                    continue;
                if(judge(i,j,k)&&A[i].B[j]!=A[i].B[k])
                    A[i].vis[k]=1;
                else if(A[i].B[j]==A[i].B[k]){
                    if(!A[i].vis[j]&&!A[i].vis[k])
                        A[i].vis[k]=1;
                }
            }
        for(int j=1;j<=A[i].cnt;j++)
            if(!A[i].vis[j])
                A[i].tt++;
    }
    printf("%d\n",tot);
    for(int i=1;i<=tot;i++){
        cout<<A[i].nme<<' '<<A[i].tt<<' ';
        for(int j=1;j<=A[i].cnt;j++)
            if(!A[i].vis[j])
                cout<<A[i].B[j]<<' ';
        cout<<endl;
    }
return 0;
}

D. Alarm Clock
给出一串序列
问最少去除多少个数是的数轴上任意[x,x+m]区间内包括的序列中的数少于k个
排序+单调队列维护
在前面留数一定比在后面留优

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int A[200000+10];
queue<int>q;
int main(){
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n,m,k;
    scanf("%d %d %d",&n,&m,&k);
    k--;
    if(k<=0){
        printf("%d\n",n);
        return 0;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",&A[i]);
    sort(A+1,A+n+1);
    int now=2;
    q.push(1);
    int cnt=1;
    int ans=0;
    while(now<=n){
        int x=q.front();
        //printf("%d\n",x);
        while(now<=n){
            //printf("%d\n",A[now]);
            if(A[now]>=A[x]+m){
                q.push(now);
                cnt++;
                now++;
                break;
            }
            else {
                if(cnt<k){
                    q.push(now);
                    cnt++;
                }
                else ans++;
                now++;
            }
        }
        cnt--;
        q.pop();
    }
    printf("%d\n",ans);
return 0;
}

E. Squares and not squares
给出一个长度为偶数的序列
每次可以把一个数x改为x-1
求最少改多少次可以把这个序列变成一个恰好有一半是完全平方数
另一半不是
贪心 先求一边完全平方数数量 求出要补什么
然后把补得代价排序贪心取

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
typedef long long ll;
ll A[200000+10];
ll dl[200000+10];
ll bl[200000+10];
ll res[200000+10];
inline ll get_up(ll x){
    if(x==0)
        return 2;
    return 1;
}
inline ll get_sq(ll x){
    ll l=0,r=x;
    while(l+1<r){
        ll mid=(l+r)>>1;
        if(mid*mid>=x)
            r=mid;
        else l=mid;
    }
    if(r*r<=x)
        return r;
    return l;
}   
inline ll get_down(ll x){
    if(x==0)
        return dl[0];
    if(x==1)
        return dl[0];
    return 1;
}
int main(){
    freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n;
    scanf("%d",&n);
    int nw=0;
    memset(bl,127,sizeof(bl));
    memset(dl,127,sizeof(dl));
    //printf("%lld\n",bl[0]);
    for(int i=1;i<=n;i++){
        scanf("%lld",&A[i]);
        ll td=get_sq(A[i]);
        if(td*td==A[i]){
            nw++;
            bl[i]=min(get_up(td),get_down(td));
        }
        else {
            dl[i]=min(A[i]-td*td,(td+1)*(td+1)-A[i]);//变成完全平方数的代
        }
    }
    int u=n-nw;
    if(u==nw){
        puts("0");
        return 0;
    }
    long long ans=0;
    if(u>nw){
        int res=(u-nw)/2;
        sort(dl+1,dl+n+1);
        for(int i=1;i<=res;i++)
            ans+=dl[i];
    }
    else {
        int res=(nw-u)/2;
        //printf("%d\n",res);
        sort(bl+1,bl+n+1);
        for(int i=1;i<=res;i++)
            ans+=bl[i];
    }
    cout<<ans<<endl;
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值