Analysis
- Let f[x] denote the minimum steps needed to jump to x.
- f[y]=min(f[x]+1) if x + A[x]>=y
- O(n) algorithm can be found.
- if the f[y]=f[x]+1 (x<y and x+A[x]>=y), then any position k ( x<k<y ) won't give f[y] a smaller value
#include<iostream> #include<algorithm> #include<vector> #include<cstdlib> using namespace std; struct node{ int a,b; node(int aa,int bb):a(aa),b(bb){} }; bool cmp(node x,node y){ return x.a<=y.a; } int main(){ vector<node>v; v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,3)); v.push_back(node(1,4)); v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,3)); v.push_back(node(1,4)); v.push_back(node(1,2)); v.push_back(node(1,2)); v.push_back(node(1,3)); v.push_back(node(1,4)); v.push_back(node(1,3)); v.push_back(node(1,4)); v.push_back(node(1,3)); v.push_back(node(1,4)); v.push_back(node(1,5)); v.push_back(node(2,3)); v.push_back(node(2,3)); v.push_back(node(3,4)); sort(v.begin(),v.end(),cmp); for(int i=0;i<v.size();i++) cout<<v[i].a<<" "<<v[i].b<<endl; return 0; }It seems good right? However it will lead to segmentation fault.
i=i+1;
#include<iostream> #include<string> using namespace std; int main(){ int i=-1; unsigned int u=100; if(i<u) cout<<"-1 < 100"; else cout<<"-1 > 100"; return 0; }The output is "-1 > 100".
#include<iostream> #include<cmath> using namespace std; int main(){ cout<<"2^32="<<(int)pow(2,32)<<endl; cout<<"Data type conversion"<<endl; cout<<"This is an illustation of how int convert with unsigned int"<<endl; //unsigned int is directly represented by binary representation //int is stored in two's complement. Thus if a int has n bits, then the bigget one is 2^(n-1)-1, however the smallest one is 2*(n-1) because the positve part uses one to represent the +0 thus the negative part donnot need to represent the -0. unsigned int u; int i; cout<<"11111...11111"<<endl; u=(unsigned int)pow(2,32)-1; i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; cout<<"10000...00000"<<endl; u=(unsigned int)pow(2,31); i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; cout<<"00000...00000"<<endl; u=(unsigned int)0; i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; return 0; }
#include<iostream> #include<cmath> using namespace std; int main(){ cout<<"2^32="<<(int)pow(2,32)<<endl; cout<<"Data type conversion"<<endl; cout<<"This is an illustation of how int convert with unsigned int"<<endl; //unsigned int is directly represented by binary representation //int is stored in two's complement. Thus if a int has n bits, then the bigget one is 2^(n-1)-1, however the smallest one is 2*(n-1) because the positve part uses one to represent the +0 thus the negative part donnot need to represent the -0. unsigned int u; int i; cout<<"11111...11111"<<endl; u=(unsigned int)pow(2,32)-1; i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; cout<<"10000...00000"<<endl; u=(unsigned int)pow(2,31); i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; cout<<"00000...00000"<<endl; u=(unsigned int)0; i=u; cout<<"u="<<u<<endl<<"i="<<i<<endl; return 0; }
vector<int> v; for(int i=-10;i<v.size();i++) cout<<i;
#include<iostream> #include<climits> using namespace std; int main(){ int i=INT_MAX; cout<<i<<endl<<i+1<<endl; i=INT_MIN; cout<<i<<endl<<i-1<<endl; unsigned int u=UINT_MAX; cout<<u<<endl<<u+1<<endl; return 0; }