0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC380Cをjavascriptで解いた

Posted at

今日の問題

↑押してください。

自分の回答(javascript)

function Main(input){
    min = 1e18
    max = -1e18
    let abc = "abcdefghijklmnopqrstuvwxyz".split("")
    let ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
    let f = 3
    if(f == 0){
      input = parseInt(input.trim())
    }
    if(f == 1){
      input = input.trim().split("\n").map((a)=>parseInt(a))
    }
    if(f == 2){
      input = input.trim().split("\n").map((a)=>a.split(" ").map((b)=>parseInt(b)))
    }
    //ここより上は定型文です。
    input = input.trim().split("\n")
   let [n,k] = input.shift().split(" ").map((a)=>parseInt(a))
   let s = input.shift()
   a = s.split("0")
   b = s.split("1")
   s = s.split("")
   if(s[0] == "1"){
     m = 1
   }else{
     m = 0
   }
   list1 = []
   list2 = []
   for(let i = 0;i<a.length;i++){
     if(a[i].length != 0){
     list1.push(a[i].length)
     }
   }
   for(let i = 0;i<b.length;i++){
     if(b[i].length != 0){
     list2.push(b[i].length)
     }
   }
   //console.log(list1,list2,a,b)
   
   //console.log(list1,list2,a,b)
   list2.push(0)
   list1.push(0)
   res = []
   list1[k-2] = list1[k-1]+list1[k-2]
   list1[k-1] = 0
   list2[k-1-m] = list2[k-1-m]+list2[k-m]
   list2[k-m] = 0
   for(let i = 0;i<list1.length+list2.length;i++){
     if(m == 1){
       for(let j = 0;j<list1[i];j++){
         res.push(1)
       }
       for(let j = 0;j<list2[i];j++){
         res.push(0)
       }
     }
     if(m == 0){
       for(let j = 0;j<list2[i];j++){
         res.push(0)
       }
       for(let j = 0;j<list1[i];j++){
         res.push(1)
       }
       
     }
   }
   console.log(res.join(""))
   
  }
  Main(require("fs").readFileSync("/dev/stdin", "utf8"));
  

工夫した点

C問題からは愚直にやるとTLEするので注意します。まず1と0がそれぞれ何個連続であるかをlistにします。その後、連結する作業をして、もとの形のリストに戻して出力して完成です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?