Time Limit: 3000MS | Memory Limit: 60000K | |
Total Submissions: 19016 | Accepted: 7963 |
Description
Input
Output
Sample Input
7 10000 -1
Sample Output
20 18658
DP水题
#include <iostream> #include <cstring> using namespace std; const int MAXN = 9999999; int a[MAXN]; bool flag[MAXN]; void DP() { for(int i=1;i<=500000;i++) { if(a[i-1] - i > 0 && flag[a[i-1] - i] == false) { a[i] = a[i-1] - i; flag[a[i-1] - i] = true; } else { a[i] = a[i-1] + i; flag[a[i-1] + i] = true; } } } int main() { memset(flag,false,sizeof(false)); a[0] = 0; DP(); int k; while(cin>>k) { if(k == -1) break; cout<<a[k]<<endl; } }