Gym - 100703G-G-Game of numbers-暴力
来源:互联网 发布:知乎回答不了问题 编辑:程序博客网 时间:2024/05/18 10:15
给出n个数。。。初始值为零 对每一个数赋一个正或负号。使得,每次操作的值在【0,k】之间
输出最长的符号长度 及其符号
对每一个数 判断其正负数+前一个数(遍历)是否合法,如果合法且正 a[i]+x 标记为1,负标记为-1;
最后找到最后一个数,往前一直推即可得答案
#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <queue>#include <map>#include <set>#include <vector>using namespace std; int a[1005][1005]; int c[1005];char ans[1005];int main(){ int n,k; scanf("%d%d",&n,&k);int i,j;int tmp;for (i=1;i<=n;i++){scanf("%d",&tmp);c[i]=tmp; }a[1][c[1]]=1;for (i=2;i<=n;i++){int cnt=0;for (j=0;j<=1000;j++){if (a[i-1][j]){if (j+c[i]<=k){a[i][j+c[i]]=1;cnt=1;}if (j-c[i]>=0){a[i][j-c[i]]=-1;cnt=1;}}}if (cnt==0)break;}int mark=i-1; //最长长度i--;printf("%d\n",mark);for (j=0;j<=1000;j++){if (a[mark][j]==1){ans[mark]='+';break;}elseif (a[mark][j]==-1){ans[mark]='-';break;}}i=j;if (ans[mark]=='+')i-=c[mark];elsei+=c[mark]; for (j=mark-1;j>=1;j--){if (a[j][i]==1)ans[j]='+';elseans[j]='-';if (a[j][i]==1)i-=c[j];elseif (a[j][i]==-1)i+=c[j];}for (i=1;i<=mark;i++){printf("%c",ans[i]);}cout<<endl;return 0;}
0 0
- Gym - 100703G-G-Game of numbers-暴力
- GYM 101128 G.Game of Cards(博弈论)
- GYM 100827 G.Number Game(博弈论)
- Gym 101028G-The Tower of Evil
- GYM Amman 17 G. Super Subarray 暴力,最小公倍数.
- [博弈论] Codeforces Gym 101190 NEERC 16 G. Game on Graph
- codeforces gym 100548G
- Gym 100269G-Garage
- CodeForces Gym 100735G
- CodeForces Gym 100735G
- Gym 100971G Repair
- Gym 100685G
- Codeforces-GYM-100923G
- Gym 101097G Party
- codeforces [Gym-100814G]
- Gym 101161 G
- Gym 101246.G
- Gym 100825 G
- MySql - SQL Error (1130): Host IP is not allowed to connect to this MySQL server
- 编程工具
- 企业招聘面试十大之怪现状
- GdiplusFlat(7)双缓冲绘图和DC透明复制
- 局域网组建的互联设备详解
- Gym - 100703G-G-Game of numbers-暴力
- Tomcat 生产服务器性能优化
- HTML5的画布:Stroke(笔触)和Fill(填充)
- 【暑期基础2】U HDU 2046 骨牌铺方格 (递推)
- Android UI学习之ListView(使用ArrayAdapter和SimpleAdapter)
- java.lang.UnsupportedClassVersionError异常
- 最大子序列和问题求解 algorithm 1 O(N^3)
- Codeforces Round #Pi (Div. 2) Problem C
- FragmentCustomAnimations分析