zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典训练指南-做题详细记录(更新中)

    第一章

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 using namespace std;
     5 typedef long long ll;
     6 //吐槽:WA一次。原因是不会输出 Loowater is doomed。由于原代码没有判断骑士是否已经杀了一个头。鉴于题目里提到n>=1.所以一旦有骑士杀了龙,那么就让骑士 b[i]=-1,这样他就相当于被标记过了。本来还想开个bool vis数组记录一下。
     7 bool dragon(int x,int y)
     8 {
     9     return x>y;
    10 }
    11 bool ability(int x,int y)
    12 {
    13     return x<y;
    14 }
    15 
    16 int main()
    17 {
    18     int n,m,a[20000+5],b[20000+5],cnt;    ll x;
    19     while(scanf("%d%d",&n,&m)==2)
    20     {
    21         if(m==n&&n==0)
    22             break;
    23         x=cnt=0;
    24         bool flag=false;
    25         for(int i=0;i<n;++i)
    26             scanf("%d",&a[i]);
    27         for(int i=0;i<m;++i)
    28             scanf("%d",&b[i]);
    29         sort(a,a+n,dragon);
    30         sort(b,b+m,ability);
    31         for(int i=0;i<n;++i)//head
    32         {
    33             int t=cnt;
    34             for(int j=0;j<m;j++)//person
    35             {
    36                 if(a[i]<=b[j])
    37                 {
    38                     cnt++;
    39                     x+=b[j];
    40                     b[j]=-1;
    41                     break;
    42                 }
    43             
    44             }
    45             if(cnt!=t+1)
    46                 {flag=true;break;}
    47         }
    48         if(flag||cnt!=n)
    49             printf("Loowater is doomed!
    ");
    50         else{
    51             printf("%lld
    ",x);
    52         }
    53     }
    54 }
    UVA11292:The Dreagon of Loowater
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 using namespace std;
     5 typedef long long ll;
     6 
     7 struct per{
     8     int b;
     9     int j;
    10     bool operator < (const per& x) const{return j>x.j;}//第一次尝试不用cmp函数 
    11 }p[1000+7];
    12 //
    13 //bool cmp(per x,per y)
    14 //{
    15 //    return x.j>y.j;
    16 //}
    17 int main()
    18 {
    19     int n,cas=0,s,ans;
    20     while(scanf("%d",&n)==1&&n)
    21     {
    22         ans=s=0;
    23         cas++;
    24         for(int i=0;i<n;++i)
    25             scanf("%d%d",&p[i].b,&p[i].j);
    26         sort(p,p+n);                    //        sort(p,p+n,cmp);
    27         for(int i=0;i<n;++i){
    28             s+=p[i].b;
    29             ans=max(ans,s+p[i].j);    
    30         }
    31         printf("Case %d: %d
    ",cas,ans);
    32     }
    33     return 0;
    34 }
    UVA11729:Commando War

  • 相关阅读:
    php的四种算法
    laravel框架安装过程中遇到的问题
    json_decode转码无效
    php通过mysqli链接mysql数据库
    jq函数绑定与解绑
    redis的运行机制
    数据库设计的三范式
    MYSQL数据库索引
    PHP超全局变量数组
    vue的settings.json格式化配置
  • 原文地址:https://www.cnblogs.com/greenaway07/p/11219147.html
Copyright © 2011-2022 走看看