zoukankan      html  css  js  c++  java
  • leetcode周赛 243

    A:水题。

    https://leetcode-cn.com/problems/check-if-word-equals-summation-of-two-words/

     1 class Solution {
     2 public:
     3     int tans(string s){
     4         int res=0;
     5         for(int i=0;i<s.size();i++){
     6             res=res*10+s[i]-'a';
     7         }
     8         return res;
     9     }
    10     bool isSumEqual(string a, string b, string tar) {
    11         if(tans(a)+tans(b)==tans(tar)) return true;
    12         return false;
    13     }
    14 };

    B:给定一个数字字符串(可正可负)和一个在1~9之间的数x,问将x插入后,最大值为多少。

    贪心,如果是正数,就希望高位尽可能大,否则就是高位尽可能小。

    假设某正数为abcdef,其中a>b,x>b,那么最好方案必然是axbcdef。(可以证明将x放在其他任何地方,都比这要小)

    负数同理可证。

     1 class Solution {
     2 public:
     3     string maxValue(string n, int x) {
     4         string res="";
     5         int l=0,r=n.size()-1;
     6         if(n[0]=='-'){
     7             l+=1;    
     8         }
     9         if(n[0]!='-'){
    10             for(int i=l;i<=r;i++){
    11                 if(n[i]-'0'<x&&x!=-1)
    12                 {
    13                     res+=x+'0';
    14                     x=-1;
    15                 }
    16                 res+=n[i];
    17             }
    18             if(x!=-1)
    19                 res+=x+'0';
    20         }else if(n[0]=='-'){
    21             res+='-';
    22             for(int i=l;i<=r;i++){
    23                 if(n[i]-'0'>x&&x!=-1)
    24                 {
    25                     res+=x+'0';
    26                     x=-1;
    27                 }
    28                 res+=n[i];
    29             }
    30             if(x!=-1)
    31                 res+=x+'0';
    32         }
    33         return res;
    34     }
    35 };

    C:https://leetcode-cn.com/problems/process-tasks-using-servers/

    可以将服务器分为空闲和忙碌两种状态。

      空闲服务器有的操作:找到权值最小的服务器,删除(进入工作状态),添加(工作完成),故可以用堆来维护

      忙碌服务器有的操作:找到最先完成的服务器,删除(完成任务),添加(进入工作状态),同样可以用堆来维护。

     1 typedef long long LL;
     2 class Solution {
     3 public:
     4     struct node1{//空闲服务器堆中的点
     5         LL id,w,t;
     6         bool operator<(const node1 tmp) const {
     7             if(w==tmp.w) return id<tmp.id;
     8             return w<tmp.w;
     9         }
    10         bool operator>(const node1 tmp) const {
    11             if(w==tmp.w) return id>tmp.id;
    12             return w>tmp.w;
    13         }
    14     };
    15     struct node2{//忙碌服务器堆中的点
    16         LL id,w,t;
    17         bool operator<(const node2 tmp) const {
    18             if(t!=tmp.t) return t<tmp.t;
    19             if(w!=tmp.w) return w<tmp.w;
    20             return id<tmp.id;
    21         }
    22         bool operator>(const node2 tmp) const {
    23             if(t!=tmp.t) return t>tmp.t;
    24             if(w!=tmp.w) return w>tmp.w;
    25             return id>tmp.id;
    26         }
    27     };
    28     priority_queue<node1,vector<node1>,greater<node1>> free;
    29     priority_queue<node2,vector<node2>,greater<node2>> busy;
    30     vector<int> ans;
    31     vector<int> assignTasks(vector<int>& se, vector<int>& ta) {
    32         for(int i=0;i<se.size();i++) free.push({i,se[i],0});
    33         for(int i=0;i<ta.size();i++){
    34             while(busy.size()&&busy.top().t<=i){
    35                 auto tmp=busy.top();
    36                 busy.pop();
    37                 free.push({tmp.id , tmp.w , tmp.t});
    38             }
    39             if(free.size()){
    40                 auto tmp=free.top();
    41                 free.pop();
    42                 ans.push_back(tmp.id);
    43                 busy.push({tmp.id , tmp.w , i+ta[i]});
    44             }else{
    45                 auto tmp=busy.top();
    46                 busy.pop();
    47                 ans.push_back(tmp.id);
    48                 busy.push({tmp.id , tmp.w , tmp.t+ta[i]});
    49             }
    50         }
    51         return ans;
    52     }
    53 };

    D:https://leetcode-cn.com/problems/minimum-skips-to-arrive-at-meeting-on-time/

    模型类似于上一次周赛,必须在整点上车。

    可以将等待时间归到前一段,即f[i][j]表示经过i段路,操作次数为j的最小花费时间。

    存在精度问题(因为需要对double上取整)。

     1 const int N=1010,INF=1e9;
     2 const double eps=1e-8;
     3 double f[N][N];
     4 class Solution {
     5 public:
     6     int minSkips(vector<int>& dist, int speed, int hoursBefore) {
     7         int n=dist.size();
     8         for(int i=1;i<=n;i++){
     9             double t=(double)dist[i-1]/speed;
    10             for(int j=0;j<=i;j++){
    11                 f[i][j]=INF;
    12                 if(j<=i-1) f[i][j]=min(f[i][j],ceil(f[i-1][j]+t -eps ));
    13                 if(j-1>=0) f[i][j]=min(f[i][j],f[i-1][j-1]+t);//j-1<=i-1必然成立
    14             }
    15         }
    16         for(int i=0;i<=n;i++){
    17             if(f[n][i]<=hoursBefore){
    18                 return i;
    19             }
    20         }
    21         return -1;
    22     }
    23 };
  • 相关阅读:
    小贝_mysql 存储过程
    Codeforces Round #253 (Div. 1)-A,B
    rac环境改动spfile后遭遇ora-29250小例
    Linux学习笔记——例说makefile 索引博文
    《信息检索》课程论文撰写指南 及 分享加分说明
    git mirror的创建与使用
    一起talk GDB吧(第二回:GDB单步调试)
    nginx源代码分析--配置信息的继承&amp;合并
    EasyUI基础入门之Droppable(可投掷)
    自己动手写CPU之第七阶段(5)——流水线暂停机制的设计与实现
  • 原文地址:https://www.cnblogs.com/greenofyu/p/14842633.html
Copyright © 2011-2022 走看看