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?

More than 5 years have passed since last update.

はじめてのyukicoder vol.7

Last updated at Posted at 2019-06-18

久々にyukicoderです。

5問ほど解きました。

No.21

21.f90
implicit none
integer(8) n,k,i
integer(8),allocatable :: A(:)

read(*,*) n
read(*,*) k

allocate (A(n))
do i=1,n
   read(*,*) A(i)
end do

write(*,*) MAXVAL(A)-MINVAL(A)
end

最初問題の意味が分からなくて、全く解き方が分からなかった件について

サンプルを見てもどうやってその組み合わせ作ったん?とか首を傾げてました

で、条件を書き出す

1.塊を作る上で、何も入ってないというのはダメ

2.平均が最小のものと最大のものを比べて、差が最大となるものを出力

考えるべきは2つ目なんですよね

平均が最小のもの=与えられた数字の最小の値が単体でいる

同様に最大のもの=与えられた数字の最大の値が単体でいる

単純にその引き算の値を出力するだけです

1の条件は?となっても、残りで指定されている塊の数を作ればいいのですし、

そもそも出力しろ、とは言われてないんですよね・・・

テクニックが-とか一切関係のない、

ひらめきが物を言う個人的に良問だと思いました。

No.24

24.f90
implicit none
integer(8) n,i,j
character a*3
integer(8),allocatable :: x(:,:)
integer(8),allocatable :: y(:)
read(*,*) n
allocate (x(1:n,4))
allocate (y(0:9))
y(0:9)=4
do i=1,n
   read(*,*) (x(i,j),j=1,4),a
   if (trim(a)=="YES") then
       y(x(i,1))=y(x(i,1))+1
       y(x(i,2))=y(x(i,2))+1
       y(x(i,3))=y(x(i,3))+1
       y(x(i,4))=y(x(i,4))+1
   else
       y(x(i,1))=y(x(i,1))-1
       y(x(i,2))=y(x(i,2))-1
       y(x(i,3))=y(x(i,3))-1
       y(x(i,4))=y(x(i,4))-1
   end if
end do

write(*,*) maxloc(y)-1
end

最初はPythonで解こうとしていたんですがあえなく撃沈

Yesが二つ以上あったら比較しておんなじ奴が一つならそのまま出力とか、色々やってたんですが、

こざかしいことをやめて結局数え上げで終わらせました

最初は数学的に理論等が確立しているのでそれを使おうとしたんですが、

その理論を理解できなかったという罠

誤り訂正符号の考え方を用いるみたいなんですが、私にはちんぷんかんぷんでしたネ

誰かおせーて

プログラミングに数学的な知識はそこまで必要じゃないということを思い知る問題でした

高校数学はーとか物理がーとか言う人いますけど、

Pythonの豊富すぎるモジュールを見ると別にいらないのでは?と思います

衝突が-とか言ってもモジュールの組み合わせで簡単に再現できるみたいですし・・・

まぁ精神衛生上の問題で、知っておくに越したことはないですけどネ!

No.26

26.f90
implicit none
integer(8) a,b,c,d,i
read(*,*) a
read(*,*) b
do i=1,b
    read(*,*) c,d
    if (c==a) then
        a=d
    else if (d==a) then
        a=c
    end if
end do
write(*,*) a
end

注意点は交換する、ということですね

何と何を交換するのか、を考えましょうという話

なお私は気付かなくてWAを貰いました

No.29

29.f90
implicit none
integer(8) i,n,m,j,a,b
integer(8),allocatable :: x(:,:)
integer(8),allocatable :: y(:)
read(*,*) n
allocate (x(1:n+1,1:3))
allocate (y(1:10))
x=0
y=0
a=0
b=0
do i=1,n
    read(*,*) (x(i,m),m=1,3)
    do j=1,3
        y(x(i,j))=y(x(i,j))+1
        if (y(x(i,j))==2) then
            a=a+1
            y(x(i,j))=y(x(i,j))-2
        end if
    end do
end do

do i=1,11
    if (y(i) == 1) then
        b=b+1
    end if
end do

b=b/4

write(*,*) a+b
end
# 入力
3
1 2 3
5 4 1
1 9 2

# 出力
3

この意味全く分からなかったやーつwwwww、私です

「任意のアイテム」を4つ ≠ 連番4つ って気づいてませんでした

30分思考停止して意味が分かりました、はい

そして、始めてyukicoderでTLEに引っかかりました

miss.f90
implicit none
integer(8) i,n,m,j,a,b
integer(8),allocatable :: x(:,:)
integer(8),allocatable :: y(:)
read(*,*) n
allocate (x(1:n+1,1:3))
allocate (y(1:10))
x=0
y=0
a=0
b=0
do i=1,n
    read(*,*) (x(i,m),m=1,3)
    do j=1,3
        y(x(i,j))=y(x(i,j))+1
    end do
end do

do i=1,11
    if (y(i) >= 2) then
        do while (y(i) > 1)
            a = a+1
            y(i)=y(i)-2
        end do
    end if
end do

do i=1,11
    if (y(i) >=1 ) then
        b=b+1
    end if
end do

b=b/4

write(*,*) a+b
end

[do--if--while] ハッピーセットかよ いけませぬ、いけませぬ (戒め)

Fortranが最速なのは数値の計算だからそれ以外の分野は腕次第ってはっきり分かんだね

慢心ダメ、絶対

No.156

156.f90
implicit none
integer(8) i,l,n,m,e,f
integer(8),allocatable :: c(:)
integer(8),allocatable :: d(:)
read(*,*) n,m
allocate (c(n))
allocate (d(1:n))
c=0
d=0
e=0
f=0
read(*,*) (C(l),l=1,n)
do i=1,n
    d(i)=maxval(c)
    c(maxloc(c))=0
end do

do while (m>e)
    e=e+d(n)
    n=n-1
    f=f+1
end do

if (e==m) then
    write(*,*) f
else
    write(*,*) f-1
end if
end

未だにソートのプログラム構造が分からない件について

ソートのやり方が多すぎて余計にわけわかめ

誰かわかりやすく教えてクレメンス、もちろん Fortran90で

Fortranの組み込み関数を利用した、大きいもん順に配列を再成形するという力業で草

最後

現在No.182に取り組んでいるんですけど、TLEでむりぽ

182.f90
implicit none
integer(8) i,N,j,d
integer(8),allocatable :: A(:)
integer(8),allocatable :: B(:)
integer(8),allocatable :: C(:)
read(*,*) N
allocate (A(1:N))
allocate (B(1:N))
allocate (C(1:N))
A=0
B=0
C=0
d=0
read(*,*) (A(i),i=1,N)
do i=1,N
    C(i)=maxval(A)
    A(maxloc(A))=0
end do
deallocate A
do i=1,N-1
    if (C(i)==C(i+1)) then
        B(i)=B(i)-1
        B(i+1)=B(i+1)-1
    end if
end do
do i=1,N
    if (B(i)==0) then
        d=d+1
    end if
end do
write(*,*) d
end

それしかできんのかこの猿ゥ!

ソート使えてもTLEになりそう・・・なりそうじゃない?

[deallocate] とか小賢しいことしても無駄だってはっきり分かんだね

メモリの使い過ぎが原因ではないもの・・・

というより 512MB も使えるので、効率よくメモリーを使えれば時間は短縮できるのでは?

配列をいっぱい使えばメモリーをいっぱい使う、という小学生みたいなことしかわからないんですよね・・・

ちょっとどうにかします

以上!

0
0
0

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?