zoukankan      html  css  js  c++  java
  • Codeforces Round #250 (Div. 2) A B C

    C 贪心 写的时候突然发现这么easy。全部的绳子都要拆掉,并且绳子的个数固定,所以仅仅要每次拆绳子。仅仅要找绳子两端v小的就可以,O(n)  //代码里面有无用的冗余

    //#pragma comment(linker, "/STACK:102400000,102400000")
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <map>
    #include <set>
    #include <queue>
    using namespace std;
    
    #define ls(rt) rt*2
    #define rs(rt) rt*2+1
    #define ll long long
    #define ull unsigned long long
    #define rep(i,s,e) for(int i=s;i<e;i++)
    #define repe(i,s,e) for(int i=s;i<=e;i++)
    #define CL(a,b) memset(a,b,sizeof(a))
    #define IN(s) freopen(s,"r",stdin)
    #define OUT(s) freopen(s,"w",stdout)
    const ll ll_INF = ((ull)(-1))>>1;
    const double EPS = 1e-8;
    const double pi = acos(-1.0);
    const int INF = 100000000;
    
    const int MAXN = 2000*2+100;
    vector<int>g[MAXN];
    int v[MAXN],id[MAXN];
    int n,m;
    
    int main()
    {
        //IN("C.txt");
        while(~scanf("%d%d",&n,&m))
        {
            int u,t;
            for(int i=0;i<=n;i++)
                g[i].clear();
            for(int i=1;i<=n;i++)
                scanf("%d",&v[i]),id[i]=i;
            //sort(id+1,id)
            ll ans=0;
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&u,&t);
                g[u].push_back(t);
                g[t].push_back(u);
                if(v[t]>v[u])ans+=v[u];
                else ans+=v[t];
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    

    B---胡蒙的,至今不解为啥按lowbit从大到小一定能够找出sum的组合

    //#pragma comment(linker, "/STACK:102400000,102400000")
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <map>
    #include <set>
    #include <queue>
    using namespace std;
    
    #define ls(rt) rt*2
    #define rs(rt) rt*2+1
    #define ll long long
    #define ull unsigned long long
    #define rep(i,s,e) for(int i=s;i<e;i++)
    #define repe(i,s,e) for(int i=s;i<=e;i++)
    #define CL(a,b) memset(a,b,sizeof(a))
    #define IN(s) freopen(s,"r",stdin)
    #define OUT(s) freopen(s,"w",stdout)
    const ll ll_INF = ((ull)(-1))>>1;
    const double EPS = 1e-8;
    const double pi = acos(-1.0);
    const int INF = 100000000;
    
    const int MAXN = 1e5+100;
    
    int vis[MAXN],a[MAXN];
    int sum,up;
    
    inline int lowbit(int x)
    {
        return x&(-x);
    }
    
    vector<int>ans;
    
    bool cmp(int ca,int cb)
    {
        return lowbit(ca)>lowbit(cb);
    }
    
    int main()
    {
        //IN("B.txt");
        while(~scanf("%d%d",&sum,&up))
        {
            ans.clear();
            for(int i=0;i<=up;i++)
                a[i]=i;
            sort(a+1,a+1+up,cmp);
            for(int i=1;i<=up;i++)
             {
                 int tmp=lowbit(a[i]);
                // printf("i=%d %d
    ",i,a[i]);
                 /////
                 if(sum>=tmp)sum-=tmp,ans.push_back(a[i]);
                 if(sum==0)break;
             }
             if(sum!=0)puts("-1");
             else
             {
    
                 printf("%d
    ",ans.size());
                 if(ans.size())printf("%d",ans[0]);
                 for(int i=1;i<ans.size();i++)
                    printf(" %d",ans[i]);
                 putchar('
    ');
             }
        }
        return 0;
    }
    

    A  纯属联系string类的substr函数了 只是用String数组写更好些,我的代码冗余严重

    //#pragma comment(linker, "/STACK:102400000,102400000")
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <map>
    #include <set>
    #include <queue>
    using namespace std;
    
    #define ls(rt) rt*2
    #define rs(rt) rt*2+1
    #define ll long long
    #define ull unsigned long long
    #define rep(i,s,e) for(int i=s;i<e;i++)
    #define repe(i,s,e) for(int i=s;i<=e;i++)
    #define CL(a,b) memset(a,b,sizeof(a))
    #define IN(s) freopen(s,"r",stdin)
    #define OUT(s) freopen(s,"w",stdout)
    const ll ll_INF = ((ull)(-1))>>1;
    const double EPS = 1e-8;
    const double pi = acos(-1.0);
    const int INF = 100000000;
    
    string a,b,c,d;
    
    int checka()
    {
        int len=a.size()*2;
        if(len <= b.size() && len <=c.size() && len<=d.size())return 1;
        if(a.size()>=b.size()*2 && a.size()>=d.size()*2 && a.size()>=c.size()*2)return 1;
        return 0;
    }
    int checkb()
    {
        int len=b.size()*2;
        if(len <= a.size() && len <=c.size() && len<=d.size())return 1;
        if(b.size()>=a.size()*2 && b.size()>=d.size()*2 && b.size()>=c.size()*2)return 1;
        return 0;
    }
    
    int checkc()
    {
        int len=c.size()*2;
        if(len <= a.size() && len <=c.size() && len<=d.size())return 1;
        if(c.size()>=a.size()*2 && c.size()>=d.size()*2 && c.size()>=b.size()*2)return 1;
        return 0;
    }
    
    int checkd()
    {
        int len=d.size()*2;
        if(len <= a.size() && len <=c.size() && len<=b.size())return 1;
        if(d.size()>=a.size()*2 && d.size()>=c.size()*2 && d.size()>=b.size()*2)return 1;
        return 0;
    }
    
    int main()
    {
        //IN("A.txt");
        while(cin >> a >> b >> c >> d)
        {
            a=a.substr(2,a.size()-2);
            b=b.substr(2,b.size()-2);
            c=c.substr(2,c.size()-2);
            d=d.substr(2,d.size()-2);
    
            int flag=0;
            char ans;
            if(checka()){ans='A';flag++;}
            if(checkb()){ans='B';flag++;}
            if(checkc()){ans='C';flag++;}
            if(checkd()){ans='D';flag++;}
            if(flag == 1){printf("%c
    ",ans);continue;}
            puts("C");
        }
        return 0;
    }
    


  • 相关阅读:
    nginx+keepalived高可用 (主从+双主)
    element ui中table动态列切换时,表格样式变形
    vue中的Swiper使用slideTo提示no function
    element的upload手动submit前动态设置上传请求地址
    使用svg让页面自适应浏览器大小,整体等比缩放
    vue子组件为父组件属性写值
    C#使用SharpZipLib解压多文件的zip压缩文件数据流,保存到本地
    javascript使用正则表达式,从字符串提取内容,多数组解析
    sql server 分组排序
    IE浏览器下错误,不能执行已释放script的代码
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6911727.html
Copyright © 2011-2022 走看看