Common Board#include <stdio.h> int main(){ long int n,sum; scanf("%ld", &n); if(n == 0){ printf("%ld\n",1); return 0; } if(n >= 1){ sum = n*(n+1)/2; }else{ n = n*(-1); sum = n*(n+1)/2; sum--; sum = sum * (-1); } printf("%ld\n", sum); return 0; } Meet-in-the-middle is algorithm which allows us to solve O(2^n) complexity problems for O(2^n/2) time. In this problem you can use meet-in-the-middle: divide the peal into two peals b = {a1 a2 a3 . . . an/2}, c = {an/2 + 1, a2, . . . an}. Then calculate all subvalues of b and store it in s1, same for c and store in s2.Then sort s1 and s2. Now you must calculate Min(pealsum - 2 * (s1j + s2k)).This algo takes O((2^(n /2))^2) time instead of O(2^n), but O((2^(n /2))^2) = O(2^n). Notice that s2 is sorted then we can use binray_search then we have O(2^(n/2)log(2^n/2))=O(2^(n/2) *(n/2)). Edited by author 17.10.2011 20:50 Hello, i am learning about meet in the meedle, please explain why Min(pealsum - 2 * (s1j + s2k)) thanks (Sorry for my poor english) I also haven't understood Min(pealsum - 2 * (s1j + s2k)). I'd appreciate if someone explain it. But, you can calculate s1 and s2 as sum of all possible combinations: +/-a1 +/-a2... ++/-an and +/-b1 +/-b2... ++/-bn. Then find min(s1j + s2k). Here `meet in the middle' to reduce complexity, sort s2 and use binary search for lookup. Does it make sense? How come that my code passed all tests except this one? What ability is tested there? 6 4 4 ans King: 8 Knight: 8 Bishop: 9 Rook: 10 Queen: 19 1987 1947 1952 ans King: 8 Knight: 8 Bishop: 2056 Rook: 3972 Queen: 6028 17 17 17 ans King: 3 Knight: 2 Bishop: 16 Rook: 32 Queen: 48 If your complexity is O(N^3) and your solution only gets AC in C/C++ then maybe you should print the matrix of where is best to drop the first egg. If your complexity is O(N^3) you can use the fact that when number of eggs >= log(N) , answer is log(N) . O(N^2*log(N)) . If your complexity is O(N^3) you can use the fact that when number of eggs >= log(N) , answer is log(N) . O(N^2*log(N)) . Keivan, that's such a good observation! Is that the intended solution though? #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <iomanip> #include <set> #include <map> #include <queue> #include <deque> using namespace std; map <char,int> a; map < string , bool > tamga; map < string , string > tamga2; pair <int, string > mas[109]; string ans,p,pat,s,s1,word[10]={"oqz","ij","abc","def","gh","kl","mn","prs", "tuv","wxy"}; int main() { int n,i,j; for(i=0;10>i;i++) { for(j=0;word[i].length()>j;j++) a[word[i][j]]=i+1; } while(true) { cin>>s1; if(s1.find("-1")==0)break; cin>>n; tamga.clear(); tamga2.clear(); for(i=0;n>i;i++) { p=""; cin>>s; for(j=0;s.length()>j;j++) p+=((a[s[j]]-1)+'0'); tamga[p]=true; tamga2[p]=s; }
n=s1.length(); for(i=0;n>i;i++) { pat=""; if( i==0 || mas[i-1].first) for(j=i;n>j;j++) { pat+=s1[j];
if(tamga[pat] && i==0){ mas[j]=make_pair( 1,tamga2[pat] );continue;} if(i==0)continue;
if( tamga[pat] && ( mas[j].first==0 || mas[j].first>mas[i-1].first+1) ) mas[j]=make_pair( mas[i-1].first+1 ,tamga2[pat] );
}
} if(mas[n-1].first==0 ){cout<<"No solution."<<endl;continue;} int l=mas[n-1].first;
ans=mas[n-1].second; mas[n-1]=make_pair(0,""); l--; for(i=n-2;i>=0;i--) { //cout<<mas[i].first<<" "<<mas[i].second<<endl; if(mas[i].first==l && l>0){ans=mas[i].second+" "+ans,l--;} mas[i]=make_pair(0,""); } cout<<ans<<endl; ans="";
} //system("pause"); return 0; } Edited by author 12.11.2013 12:41 Well, you'd better explain your solution, perhaps it's wrong... Try also this test: 2285064252258219 5 long false cat tail black The output should be "No solution.". returned "No solution" ?:( there is a '.' after the word "solution" I have derived an expression which is giving correct results for the input given on the problem page as well as the ones below : 2 1000000 1 1 1000000000 1 1 500 15811 I got this set from another thread on this problem, however I am still getting WA on test 2. I am rounding off the result in Python : round(abs(n)) and checking for the case when this will round to 0, which will happen for this case : 1 2 1 In which case I output 1. What's special about test 2 ? Can I get a similar test or an idea about the special conditions which have to be checked ? #include<iostream> #include<string> int res; int Fr(short int nm, short int q) { if((nm-q) == 0) return res; if((nm-q)>0) { res *= nm-q; Fr(nm-q,q); } else return res; } int main() { short int n=0; char c; int k=0; std::string s; std::string ss=""; std::getline(std::cin,s); for(int i=0;i<s.length();i++) { if(isdigit(s[i])) ss += s[i]; if(s[i] == '!') k++; } int w=1; int temp; for(int i=0;i<ss.length();i++) { for(int j=0;j<(ss.length()-i-1);j++) w *= 10; if (ss[i] >= '0' && ss[i] <= '9') temp = ss[i] - '0'; n += temp*w; w=1; }
res = n; printf("%d",Fr(res,k)); } AC again with nearly nothing change People, please, help me. I got WA 11. Can anybody give me test? Here is my solution: #include <iostream> #include <map> #include <queue> #include <vector> using namespace std;
int minimum(int arr[300][300], map < int, string > Num, map < string, int >L, int cur) {
int min_ = 100499;
for (int i = 0; i < cur; ++i)
if (arr[cur][i] && L[Num[i]] < min_)
min_ = L[Num[i]];
return min_; }
void Level_up(map < string, int >Nam, map < int, string > Num, map < string, int >&Lev, int sz, int arr[300][300]) {
for (int i = 0; i < sz; ++i) {
int level = Lev[Num[i]];
if (!level && Num[i] != "Isenbaev") // Если имя ещё не встречалось
Lev[Num[i]] = level = minimum(arr, Num, Lev, sz) + 1;
for (int j = 0; j < sz; ++j)
if (i != j && arr[i][j])
if ((Num[j] != "Isenbaev" && Lev[Num[j]] == 0) || Lev[Num[j]] > level + 1)
Lev[Num[j]] = level + 1;
} }
int main() {
int N;
string f;
string s;
string t;
string P = "Isenbaev";
map < string, int >Name;
map < int, string > Number;
int count = 1;
Name[P] = count++;
Number[0] = P;
int arr[300][300];
map < string, int >Level;
bool flag = false;
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> f >> s >> t;
if (f == P || s == P || t == P) flag = true;
if (!Name[f])
Name[f] = count++;
if (!Name[s])
Name[s] = count++;
if (!Name[t])
Name[t] = count++;
Number[Name[f] - 1] = f;
Number[Name[s] - 1] = s;
Number[Name[t] - 1] = t;
arr[Name[f] - 1][Name[s] - 1] = arr[Name[s] - 1][Name[f] - 1] = arr[Name[f] - 1][Name[t] - 1] = arr[Name[t] - 1][Name[f] - 1] = arr[Name[s] - 1][Name[t] - 1] = arr[Name[t] - 1][Name[s] - 1] = 1;
}
Level[P] = 0;
Level_up(Name, Number, Level, count - 1, arr);
for (map < string, int >::iterator it = Level.begin(); it != Level.end(); ++it) {
if (flag || !flag && it->first != P) {
cout << it->first << " ";
if (it->first != P)
if (it->second < count)
cout << it->second << endl;
else cout << "undefined" << endl;
else cout << it->second << endl;
}
}
return 0; } Sorry, for the my code and my English. Edited by author 24.08.2014 03:07 Admins, do you check timus_support@acm.timus.ru from time to time? My solution to this problem just lacks 1/3 of a solution (actually, the most difficult third of it) - and got AC! I sent the code recognizing that important part of the solution is missing and just wanted to see how far it will reach - but got AC instead. Pity that such a good problem has such a weak testset. I can mail my tests to you, but it seems nobody checks this mailbox... Edited by author 16.08.2014 13:34 up up Edited by author 04.08.2014 02:20 up up Tests have been added. 16 authors have lost AC. This is my code. I got WA 1 on G++ and G++11, but AC on Visual C++ 2010. Why? #include <iostream> const int M = 1000*1000+1; char a[M], b[M]; int n; using namespace std; void read() { ios_base::sync_with_stdio(0); cin >> n; for(int i = n-1; i >= 0; --i) { cin >> a[i] >> b[i]; a[i] -= 48; b[i] -= 48; } } void solve() { for(int i = 0; i < n; ++i) { a[i] += b[i]; if(a[i] > 9) { a[i + 1]++; a[i] -= 10; } } } void print() { for(int i = n-1; i >= 0; --i) { cout << (int)a[i]; } } int main() { read(); solve(); print(); } #include <stack> #include <cmath> #include <iostream> int main() { unsigned long long int n; std::stack<unsigned long int> numbers; while (!std::cin.eof()) numbers.push(n); while (!numbers.empty()) { std::cout << sqrt( double( numbers.top() ) ) << " "; numbers.pop(); } return 0; } Probably the stack this code builds is too large, but I can't see how to optimise it, given the sample numbers. I don't think your first while loop ever terminates. Admins, can you show me test#3,WA,don't understand why? 1332. Джинн-бомбардировки Edited by author 20.08.2014 18:37 Edited by author 20.08.2014 18:37 Edited by author 20.08.2014 18:38 Edited by author 20.08.2014 18:38 Edited by author 20.08.2014 18:38 Edited by author 20.08.2014 18:39 Edited by author 24.08.2014 16:33 The example is not clear enough. Does anyone provide more testcases/examples for this problem? Can you give some tests, please? Think about the parallel case for even number of vertices. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ReverseRoot { class Program { static void Main(string[] args) { string line; string input = ""; double sqrt; while (!String.IsNullOrWhiteSpace(line = Console.ReadLine())) { input += " "; input += line;//but here it seems to be an infinite loop } if (Encoding.UTF8.GetBytes(input).Length / 1024 > 256) { Console.WriteLine("Sorry! Long Input Stream"); } else { string[] arr = input.Split(' '); for (int i = arr.Length - 1; i >= 0; i--) { if (!String.IsNullOrWhiteSpace(arr[i])) { sqrt = Math.Sqrt(Int64.Parse(arr[i])); if (sqrt == 0) Console.WriteLine(sqrt.ToString("0.0000")); else Console.WriteLine(sqrt.ToString("#.####")); } } } } } } I have 9 times WA#9 and all becouse I don't use double... If you have WA#9, use double in computing triliality, and get AC =) Good Luck! |
|