Общий форумCan someone help how to make C# code faster? Here is my code: using System; using System.Collections.Generic; class Program { static int countCut(List<int>[] nods, List<int> uncheckedNodes) { int next = 0; int count = 0; do { count++; watchCut(nods, uncheckedNodes, next); if (uncheckedNodes.Count > 0) next = uncheckedNodes[0]; else next = -1; } while (next != -1); return count; } static void watchCut(List<int>[] nods, List<int> uncheckedNodes, int node) { uncheckedNodes.Remove(node); for (int i = 0; i < nods[node].Count; i++) { if (uncheckedNodes.Contains(nods[node][i])) watchCut(nods, uncheckedNodes, nods[node][i]); } } static void Main(string[] args) { string[] inp_NM = Console.ReadLine().Split(' '); int N, M; N = Convert.ToInt32(inp_NM[0]); M = Convert.ToInt32(inp_NM[1]); int[,] threads = new int[M, 2]; List<int>[] nods = new List<int>[N]; for (int i = 0; i < N; i++) nods[i] = new List<int>(); for (int i = 0; i < M; i++) { string[] inp_thread = Console.ReadLine().Split(' '); threads[i, 0] = Convert.ToInt32(inp_thread[0]) - 1; threads[i, 1] = Convert.ToInt32(inp_thread[1]) - 1; nods[threads[i, 0]].Add(threads[i, 1]); nods[threads[i, 1]].Add(threads[i, 0]); } int Q; Q = Convert.ToInt32(Console.ReadLine()); int[] cuts = new int[Q]; string[] inp_tear = Console.ReadLine().Split(' '); for (int i = 0; i < Q; i++) { cuts[i] = Convert.ToInt32(inp_tear[i]) - 1; } for (int i = 0; i < Q; i++) { nods[threads[cuts[i], 0]].Remove(threads[cuts[i], 1]); nods[threads[cuts[i], 1]].Remove(threads[cuts[i], 0]); List<int> uncheckedNodes = new List<int>(); for (int l = 0; l < N; l++) uncheckedNodes.Add(l); Console.Write(countCut(nods, uncheckedNodes)+" "); } } } Edited by author 24.11.2020 08:00 In this problem you should use disjoint-set(система непересекающихся множеств). I had MLE on test 33. I just changed int to short and 1,0 to bool Sorry if this is too obvious Edited by author 22.11.2020 16:48 #include <iostream> #include <string> #include <vector> #include <set> const std::string &solution(const std::vector<std::string> &penguins); int main() { int n; std::cin >> n; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::vector<std::string> penguins; penguins.reserve(n); for (int i = 0; i < n; ++i) { std::string penguin; std::getline(std::cin, penguin); penguins.emplace_back(std::move(penguin)); } std::cout << solution(penguins) << std::endl; return 0; } const std::string &solution(const std::vector<std::string> &penguins) { std::set<std::string> penguinSet(penguins.begin(), penguins.end()); auto setIterator = penguinSet.begin(); std::set<std::string>::iterator mostNumerous; int maxCount = 0; while (setIterator != penguinSet.end()) { int count = 0; for (const auto &penguin : penguins) { if (*setIterator == penguin) { count++; } } if (count > maxCount) { maxCount = count; mostNumerous = setIterator; } setIterator++; } return *mostNumerous; } I tested locally and it works... Problem 1394 "Ships. Version 2" is the same problem as 1115 but has full testset. Problem 1115 has only first 10 tests. All latest submissions on 1115 were moved to new problem. You can resubmit you solutions to 1115 also if you want. Problem 1115 was rejudged with 10 tests. But some submissions got WA. So change the text 1115, because two problems now have the same limits for N, M and interval length. Or just delete the problem 1115. If you can write a solution that works within 5-10 seconds for any test I make, I wolud increase TL. Wouldn't you like to share with us? I use two algorithms inside one program: First I try to arrange ships using heuristics with randomization. If it fails for many times (say, 1000 times) I use bruteforce with optimizations (just like many people) I found a big numbers of different bruteforces and heuristics for this problem. I have tests for all that methods. You need just one good combination! My program works well on all tests, I've already checked more than 30 billions random tests. A little hint: my bruteforce gets TLE 22, heuristics gets TLE 17. Good luck! the idea is great! I have thought for too many strange ideas like netflow.....but not this one! i gonna try to solve it right now. Where is your good solution at now? The present-day test is too tough for your algorithm? If this the edge, it may be worth keeping the time limit so that one top solution has always been AC. It will be reasonable to increase the TL to 3s. It is required to inspect all the current solutions thoroughly to decide whether all of them have some "tweaking" for specific tests or not. If any of them is universal (i.e. actually not depends on any seed or magic number), then time limit can be even capped on that. If not, then it is reasonable to increase TL somehow. I can bet that some of accepted solutions will not pass for some permutation of actual tests and that is not ok when them will require to be adjusted in order to pass! Edited by author 21.11.2020 11:53 I sort input before use. What do you mean? Edited by author 16.12.2020 00:35 Don't use double or you will suck hard. My formulas are definitely correct (because I got AC), but with double and framework functions like Math.Sqrt I was getting very big inaccuracies (about FEW degrees for angles between tangent and shots). Then I switched to BigInteger and got AC immediately. Edited by author 19.11.2020 04:59 #include <iostream> using namespace std; int main() { long long i, j, iDel; long long biggestV = 9999999, biggestDel = 9999999; cin >> i >> j; if (i == 1) { cout << 1; return 0; } else if (i == j) { cout << i; return 0; } else if (i + 1 == j) { cout << (j % 2 != 0 ? j : i); return 0; } for (long long iV = j % 2 != 0 ? j : j-1; iV >= i; iV -= 2) { for (iDel = j / 3 + 1; iDel >= 1; iDel--) { if (iV%iDel == 0) { if (iDel < biggestDel) { biggestDel = iDel; biggestV = iV; } break; } } if (iDel == 1) { cout << iV; return 0; } } cout << biggestV; } In the main cycle you doesn't compare trivialities, you search the smallest biggest divisor in the weird range instead. Could you please show the mathematical proof of your main cycle? Edited by author 31.03.2020 14:11 Edited by author 31.03.2020 14:12 Hi.Did you solve this problem? ---- ll l,r; cin >> l >> r; if(l==1){ cout << 1; return 0; } if(l == r){ cout << l; return 0; } ll n = 1e6+2; vector<bool> prime(n+1,1); prime[0]=prime[1] = 0; for( ll i =2; i*i <= n; i++) { if(prime[i]) { for(ll j = i*i; j <= n; j+=i) prime[j]=0; } } for(ll i = r; i >= l; i--){ if(prime[i]) { cout << i; return 0; } } double mx =1e11; ll ans = 0; for(ll i = r; i >= l; i--){ if(i % 2 == 0) continue; double suma = 1; for(ll k = 2; k*k <= i; k++){ if( i % k == 0 ){ suma += k; if(i/k != k){ suma += (i/k); } } } double q=(suma)/(i); if( q < mx){ mx = q; ans = i; } } cout << ans; return 0; ---- I used the sieve of Eratosthenes to find prime numbers. If I have not found prime numbers on the interval [l; r], then I iterate over all numbers with [l; r]. I go through the divisors (sqrt (i)). I calculate the minimum. What's wrong? WA 11. Google translator, sorry. if you wanna get 20 lines solutions, write me on dalydudan@gmail.com #include <iostream> using namespace std; int main(){ int a,b = 0,c = 0,d = 0; int ocenki[20]; cin » a; for (int i = 1; i <= a; i++) cin » ocenki[i]; for (int i = 1; i <= a; i++){ if (ocenki[i] == 5){ b++; } else if (ocenki[i] == 4){ c++; } else if (ocenki[i] == 3){ d++; } } if (d > 0){ cout « "None" « endl; } else if (b == a){ cout « "Named" « endl; } else if (c > b){ cout « "Common" « endl; } else if (c = b){ cout « "High" « endl; } return 0; } #include <iostream> using namespace std; int main() { int m[10]{}, n; cin >> n; int sum = 0; for (int i = 0; i < n; i++) { cin >> m[i]; sum += m[i]; } double q; q = static_cast<double>(sum) / n; if (q == 4) { cout << "Common"; } if (q > 4.5 && q < 5) { cout << "High"; } if (q == 5) { cout << "Named"; } if (q == 3) { cout << "None"; } } Hi, I'm not sure why I'm failing on test 1. Any ideas? import sys def main(): ans = [] for line in sys.stdin: nums = line.split() ans.extend([int(num) ** 0.5 for num in nums if num]) for root in ans[::-1]: print("%.4f" % root) Hey, you wrote your code in function. Just paste your code out of function Anybody know how to decrease mod operations to get more faster solution? I've implemented segment tree, where any node used mod operation to be calculated. I got 0.75s on G++, 0.41s on Clang and 0.25s Visual C++. Maybe I should check if value is overflowed (than mod), but will it be faster? Can you please give me a test for my code? For WA#2 you have to check if Alice/Bob are waiting exact amount di. I tried case when di=0, and noticed my code waits 1 time, instead of waiting for 0. How big in compilation time limit? I am getting this error for problem #2107. It is compiling in 1 second on my PC. I don't understand, there is nothing very complicated in my code, just one method with IEnumerable<T> as return value and yield return inside. Ok, I managed to workaround this. I compiled my yield return code. Decompiled it. And copied generated state machine code into my submission. xatosi qayerda bilasizmi? var m,n:1..50; begin read(m,n); if odd(m*n)then write('[second]:=]') else write('[:=[first]'); end.
var a,n,m:word; begin a:=m*n-1; if a mod 2=0 then writeln('[second]=:]') else writeln('[:=[first]');readln; readln;end. #include <stdio.h> void main() { int n,m; scanf("%d %d",&m,&n); printf("%s",(m*n%2)?"[second]=:]":"[:=[first]" ); } men paskallni yaxshi tushunmiyman C++ da kod quyidagicha yozilgan: #include<iostream> using namespace std; int main() { int n,m,s1=0,s2=0,i=0; cin>>n>>m; while(i==0) { if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s1+=n*m; } else { s1+=1; s2+=n*m-1; n=0;m=0;i=1; }
//------------------------------------------------ if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s2+=n*m; } else { s2+=1; s1+=n*m-1; n=0;m=0;i=1; }
} if(s1>=s2)cout<<"[:=[first]"; else cout<<"[second]=:]"; return 0; } //javada ,in java import java.util.Scanner; public class ONETHOUSANDTWO { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int s1 = 0, s2 = 0, i = 0; if ((m * n - 1) % 2 == 0) { System.out.println(); } while(i==0) { if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s1+=n*m; } else { s1+=1; s2+=n*m-1; n=0;m=0;i=1; } //------------------------------------------------ if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s2+=n*m; } else { s2+=1; s1+=n*m-1; n=0;m=0;i=1; } } if (s1>=s2) System.out.println("[:=[first]"); else System.out.println("[second]=:]"); } } men paskallni yaxshi tushunmiyman C++ da kod quyidagicha yozilgan: #include<iostream> using namespace std; int main() { int n,m,s1=0,s2=0,i=0; cin>>n>>m; while(i==0) { if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s1+=n*m; } else { s1+=1; s2+=n*m-1; n=0;m=0;i=1; }
//------------------------------------------------ if(n%2==0||m%2==0) { if(n%2==0&&m%2==0) { if(n>m)n=n/2; else m=m/2; } else if(n%2==0)n=n/2; else m=m/2; s2+=n*m; } else { s2+=1; s1+=n*m-1; n=0;m=0;i=1; }
} if(s1>=s2)cout<<"[:=[first]"; else cout<<"[second]=:]"; return 0; } Lets say there are 5 Steaks and capacity of pan is 4. Step 1: Cook first 4 one side for 1 minute. Step 2: Replace 1 one sided cooked steak with completely uncooked (remaining) one and cook for next 1 minute. Step 3: Now the chef has 3 completely cooked and 2 Half cooked. Cook the remaining 2 half cooked for another one minute. so 1 minute at each step, hence 3 minutes total minimal time. However, story doesn't end here :-). Lets take another example Now, Lets say there are 7 Steaks and capacity of pan is 4. Step 1: Cook first 4 one side for 1 minute. Step 2: Replace 3 one sided cooked steak with completely uncooked (remaining) three and cook for next 1 minute. Step 3: Now the chef has 1 completely cooked and 6 Half cooked. Cook 4 steaks from half cooked six for another one minute. Step 4: Last cook final 2 half cooked one for another one minute So total minimum time take is 4 minute. 1 minute at each step. Please keep in mind the scenarios where 0 steaks or steaks less then cooking capacity of pan. Edited by author 08.06.2017 20:19 Edited by author 08.06.2017 20:20 the scenario with n == 0 or k == 0 won't happen as in the statements it says 1 <= n, k <= 1000 First sample != test 1 :( |
|