fork download
  1. //ABcDexter
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int i,n,t;
  6. int ans=0;
  7. const int INF=2<<19;
  8.  
  9. int Solve(int n,int i, int x) //greedy approach, MUST FIND DP!
  10. {
  11.  
  12. if(n==x) { //cout<<"reached at x: "<<n<<","<<i<<","<<x<<endl;
  13. return ans=i-1;}
  14.  
  15. else
  16. if(n<-x) return INF;
  17. else if(n>2*x) return INF;
  18.  
  19. else { //cout<<"here: "<<n<<","<<i<<","<<x<<endl;
  20. return ans=min( Solve(n+i,i+1,x), Solve(n-i,i+1,x));
  21. }
  22. }
  23.  
  24. int main()
  25. { //int ans=0;
  26. //cout<<INF;
  27. for(cin>>t;t;t--)
  28. { ans=0;
  29. cin>>n;
  30. cout<<">! For x="<<n<<", ans : "<<Solve(0,0,n)<<endl;
  31. }
  32. }
Time limit exceeded #stdin #stdout 15s 3472KB
stdin
52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
stdout
>! For x=1, ans : 1
>! For x=2, ans : 3
>! For x=3, ans : 2
>! For x=4, ans : 3
>! For x=5, ans : 5
>! For x=6, ans : 3
>! For x=7, ans : 5
>! For x=8, ans : 4
>! For x=9, ans : 5
>! For x=10, ans : 4
>! For x=11, ans : 5
>! For x=12, ans : 7
>! For x=13, ans : 5
>! For x=14, ans : 7
>! For x=15, ans : 5
>! For x=16, ans : 7
>! For x=17, ans : 6
>! For x=18, ans : 7
>! For x=19, ans : 6
>! For x=20, ans : 7
>! For x=21, ans : 6
>! For x=22, ans : 7
>! For x=23, ans : 9
>! For x=24, ans : 7
>! For x=25, ans : 9
>! For x=26, ans : 7