Submission #960950

#TimeUsernameProblemLanguageResultExecution timeMemory
960950MarwenElarbiAncient Books (IOI17_books)C++17
Compilation error
0 ms0 KiB
#include <bits/stdc++.h> #include "books.h" using namespace std; long long minimum_walk(std::vector<int> p, int s){ int n=p.size(); long long ans=0; bool vis[n]; vector<int> cycles(n); memset(vis,0,sizeof vis); for (int i = 0; i < n; ++i) { ans+=abs(p[i]-i); } int en=n-1; for (int i = n-1; i >= 0; --i) { en=i; if(p[i]!=i) break; } //cout << ans<<endl; for (int i = 0; i < n; ++i) { int cur=i; if(vis[cur]) continue; int mn=1e6+5; int mx=-1; while(vis[cur]==0){ vis[cur]=1; mx=max(mx,cur); mn=min(mn,cur); cur=p[cur]; } cycles[mn]++; cycles[mx]--; } int cur=0; for (int i = 0; i < en; ++i) { cur+=cycles[i]; if(cur==0) ans+=2; } return ans; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, s; assert(2 == scanf("%d %d", &n, &s)); vector<int> p((unsigned) n); for(int i = 0; i < n; i++) assert(1 == scanf("%d", &p[(unsigned) i])); long long res = minimum_walk(p, s); printf("%lld\n", res); return 0; }

Compilation message (stderr)

books.cpp: In function 'int main()':
books.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
books.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/cc0HuvLo.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccCK0Ktq.o:books.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status