Common BoardI've learned A LOT of optimizations tricks for BFS with this problem. I think that's the fastest BFS I've ever done LOL. A little hint if you are unsure what to do: Try to brute force the solution and optimize it. Eventually, you will get AC. And thanks to the author, for creating this amazing problem. I use Disjoint-set who can help me? Facing the same. Try changing a linear search you might have into smth that can b done in constant time. That made it much faster for me. (hint: you probably don't need anything special, just change your implementation a bit) After so many calculation and math I have solved the problem..... Here we can have two worst cases... Case 1: having all the right shoes first.so here needed time is 2*b and we have now all the left shoes remaining...so total time is 2*b+40... Case 2: we may have 39 right shoes so time needed here is (39*2=78)...then we have only one right foot left but we may encounter all the left shoes and here needed time is 40+2*(a-40)....> 40 for the first 40 shoes and 2*(a-40) is for the remaining shoes as they needed to be thrown away...then we have the only one right foot left and it need 1 second... so total time = 78+40+2*(a-40)+1 = 119+2*a-80 = 2*a-39 ans=max(Case 1,Case 2) Edited by author 22.02.2020 03:07 But it's given that both a,b>=40.....so how 39 right shoes can be there? mistake in case 2: 119+2*a-80 = 2*a+39 everything else is correct Edited by author 11.01.2021 22:56 int f; cin >> f; int obs = 255; /* 5 hours = 300 minutes all time = 300 minutes - 45 minutes(for f tasks) */ int r = 12 - f; // tasks after first hour int ans = 45 * r; // time for solution if (ans <= obs) cout << "YES" << endl; else cout << "NO" << endl; Edited by author 15.05.2020 17:20 Edited by author 15.05.2020 17:20 if (5 + f >= 12) cout << "YES"; else cout << "NO"; cuz (60 * 4) // 45 = 5 he can solve 5 tasks in last 4 hours Edited by author 11.01.2021 13:20 Edited by author 11.01.2021 13:20 #+-+-+-+-+-+ +-+-+-+-+-+# Edited by author 09.01.2021 01:54 Ok, stupid mistake again. Basically, the last line will be always \n So if you did something like this to read the input: std::string s; while(!std::cin.tie(0)){ std::cin >> s; } The Program will read an empty string. So you need to read as: while(!std::cin.tie(0)){ std::cin >> s; if(!s.size())break; } I got WA #6, So I thought my diagonal code was wrong! Lol. Edited by author 09.01.2021 02:47 Edited by author 15.01.2021 03:31 Edited by author 15.01.2021 03:32 Edited by author 15.01.2021 03:32 Btw the correct answer is: 11 11 My AC solution (6438921) does not work on this test 2 111111110000 6 1 9 2 3 3 4 5 11 6 7 7 8 Please add it. Moreover, I think there could be found much more tricky tests than the one above. Please try to do that and add them too (I am too lazy to help you with that :) ). You may look into my submission 6438944 where this case is handled (presumably as well as all other tricky cases). I got AC with random shuffle, the tests are weak for sure. Could you send test against your solution to sp@urfu.ru ? :) I don't know the problem use std::io; use std::io::BufReader; use std::io::BufRead; fn minimal_product_digit(n: i64) -> i64{ if n == 0 {return 10}; if n < 10 { return n};
let f : Vec<i64> = vec![2,3,5,7]; let mut aux = n; let mut ret : Vec<i64> = Vec::new(); for i in f{ if aux % i== 0 { loop{ aux/=i; ret.push(i); if aux % i != 0 { break; } } } }; if ret.is_empty() || aux > 1 { return -1; }
match ret.iter().fold(0,|acc,elem| if *elem == 3{acc + 1} else {acc}) { 1 => { let index = ret .iter().position(|&x| x == 3).unwrap(); if index > 0{ ret.remove(index); let k = ret.get_mut(index - 1).unwrap(); *k = 6; ret.sort();
} } _ => () } let mut v : Vec<i64> = Vec::new(); for i in ret{ match i { 2 | 3=> match v.last_mut(){ None => v.push(i), Some(last) if i ==3 && *last == 2 => v.push(i), Some(last) => { let valor = *last; if (valor * i) < 10{ *last = valor*i } else{ v.push(i); } } } _ => v.push(i) } } v.sort(); v.iter().fold(0, |acc, elem| acc * 10 + elem) } fn main() { let mut line = String::new(); let mut stdin = BufReader::new(io::stdin()); stdin.read_line(&mut line).unwrap(); let lim = line.trim().parse::<i64>().unwrap(); println!("{:?}",minimal_product_digit(lim)); } When n > 5 I guess, you can place one or more circles to free place within polygon? I assume that answer for 5 and 6 is same Edited by author 06.01.2021 17:11 One doesn't have to execute their actions followed by the other. For instance: Sandro can execute 7 consecutive actions and after that Zagamius could execute 20. So basically, their turns don't need to be consecutive. Misinterpretation will lead to WA#4 or WA#5. Edited by author 05.01.2021 02:08 It is not said in the condition that the points have different coordinates. Can points have the same coordinates? There are no duplicate points in tests. in some cases, there are more than 1 current user Count numbers X and Y of quiet days for vacations given by intervals [1;r] and [1;l-1]. The answer to the problem would be ANS=X-Y. How to count X and Y? In the first step, we eliminate every 2th. That is, CNT_0=r and and CNT_1=r//2 In the second step, CNT_2=CNT_1//3. And so on. Until CNT > 0. Last non-zero CNT is the answer. Yes, but you should prove that the number of steps is short enough. Here is some sketch of the proof. After deleting all 2nd numbers we are with $\frac{n}{2}$ numbers, after erasing all 3rd numbers we are with $\frac{2}{3}\cdot \frac{n}{2} = \frac{n}{3}$ numbers, after erasing all 4th numbers we are with $\frac{3}{4}\cdot \frac{n}{3} = \frac{n}{4}$ numbers and so on, after erasing every $k$-th number we are left with $\frac{n}{k}$ numbers, and we stop when $\frac{n}{k} < (k+1)\implies k \approx \sqrt{n}$, so after $O(\sqrt{n})$ steps the algorithm stops. Edited by author 02.01.2021 20:07 Edited by author 02.01.2021 20:07 Edited by author 02.01.2021 20:09 The 7th test case is: 2 5 1 And my program outputs 348. I've tried to brute-force, and I got the same result. Did I misunderstand the problem? What should be the output for this test case? Ok, the answer is 780, but I don't know why. Ok, I'm stupid. Here's a function that checks if a matrix respects the Lunar Condition. bool lunar_check() { for(int i = 0; i != N-1;++i) { int count = 0; for(int j = 0; j != M;++j) { count += (matrix[j][i]==0&&matrix[j][i+1]==1); } if(count>K) return false; } return true; } I hope that nobody else's have problems reading the statement. ;-; Input: 1 40 1 Output: 1099511627776 we can use the netflow algorithm(+ operator) to solve this problem. #include <cstdio> #include <cstring> #include <algorithm> #define MAXN 5 using namespace std; const int INF=1<<30; int n,m,flow[MAXN][MAXN],pre[MAXN],cur[MAXN],gap[MAXN],d[MAXN],g[MAXN],l[MAXN][MAXN]; int Maxflow_Isap(int s,int t,int n) { memset(d,-1,sizeof(d)); memset(pre,-1,sizeof(pre)); memset(gap,0,sizeof(gap)); gap[s]=n; int u=pre[s]=s,v,maxflow=0; while (d[s]<n) { v=n+1; for (int i=cur[u];i<=g[u];i++) if (flow[u][l[u][i]] && d[u]==d[l[u][i]]+1) { v=l[u][i];cur[u]=i;break; } if (v<=n) { pre[v]=u;u=v; if (v==t) { int dflow=INF;u=s; for (int i=v;i!=s;i=pre[i]) dflow=min(dflow,flow[pre[i]][i]); maxflow+=dflow; for (int i=v;i!=s;i=pre[i]) { flow[pre[i]][i]-=dflow; flow[i][pre[i]]+=dflow; } } } else{ int mindist=n; for (int i=1;i<=g[u];i++) if (flow[u][l[u][i]]) mindist=min(mindist,d[l[u][i]]); if (!--gap[d[u]]) return maxflow; gap[d[u]=mindist+1]++;cur[u]=1; u=pre[u]; } } return maxflow; } void setsize(int x,int y,int c) { flow[x][y]=c; l[x][++g[x]]=y; l[y][++g[y]]=x; } int main() { scanf("%d%d",&n,&m); setsize(1,2,n); setsize(2,4,n); setsize(1,3,m); setsize(3,4,m); printf("%d\n",Maxflow_Isap(1,4,4)); return 0; } Thank you for this code, I'm curious to know, is there any shorter form of this i can use for a contest? Since the requirements became stronger, and time was reduced from 2 sec down to 1 sec, now it is not possible to resolve the task using bruteforce method, python is very slow for such hard requirements. Even I do empty loops the taken time is already about 1.3 sec. from time import time time_before = time() len_lst = 20 for i in range(1, ((2 ** len_lst) // 2) - 1):
for j in range(len_lst): pass
print(time() - time_before) >>> 1.3192360401153564 I think for Python the time should be rolled back to 2 sec. Of cause if I use bruteforce on "C" it takes about 0.2 sec, but Python is not "C". Finally I could resolve the task on Python using DP for 0.65 sec. This problem is easier than rating should be if you know the trick. Invariant: graph G is decomposable if and only if all the connected components of G has even number of edges. A tree is very easy to be decomposed. Creating a tree equivalent to each connected component will lead to the answer. Hope this help, Mick :) It helped me a lot, thank you! I read a proof of the fact that any connected graph with even number of edges is decomposable, but it was quite complicated and didn't allude at any good algorithm underneath. Your idea is much simpler and easier to proof I don`t understant brute-force , who can give your code to me ? mail: k13795263@yahoo.cn Edited by author 23.08.2008 05:40 I have a brute-force code for cheking answers Python 3.8 def summa(a): ans = 0 for i in range(len(a)): ans += a[i] * a[i - 1] return ans def recursion(n, do_etogo): a = True mn = 10 ** 9 mx = 0 mnansn = [] mxansn = [] for i in range(2, n + 1): if i in do_etogo: continue a = False mnans, mxans, mnansm, mxansm = recursion(n, do_etogo + [i]) if mnans < mn: mn = mnans mnansn = [mnansm] elif mnans == mn: mnansn.append(mnansm) if mxans > mx: mx = mxans mxansn = [mxansm] elif mxans == mx: mxansn.append(mxansm) if a: return summa(do_etogo), summa(do_etogo), do_etogo, do_etogo return mn, mx, mnansn, mxansn n = int(input()) print(recursion(n, [1])) i have got WA on test #5. Help me please. Thanks |
|