简单动态规划题。
View Code
1 #include <stdio.h>
2 #include <string.h>
3 #define N 500002
4 #define MAX 10000000
5 char vis[MAX];
6 int c[N];
7 int main()
8 {
9 int n,i,tmp;
10 c[0]=0,vis[0]=1;
11 for(i=1; i<N; i++)
12 {
13 tmp=c[i-1]-i;
14 if(tmp>0&&!vis[tmp]) vis[tmp]=1,c[i]=tmp;
15 else c[i]=c[i-1]+i,vis[c[i]]=1;
16 }
17 while(scanf("%d",&n)&&n!=-1) printf("%d\n",c[n]);
18 return 0;
19 }