UVALive - 7501 思维 +二分
2017-10-09 23:35
489 查看
传送门
题意:给定一个n个结点的环,编号0~n-1,每个点有一定的权值,从点0出发沿编号走,到达某一个节点则把目前总权值加上这个节点的权值,如果结果小于0则变成0。现在给你最多可以走的步数P和最大需要到达的权值大小G,问你需要的最小的初始权值为多少,能在P步内能够产生的最大权值大于等于G
初始权值越大,经过同样步数之后得到的权值越大,考虑二分,但是步数太大无法模拟
如果走第一圈的时候在某一点上x变成0,等价于初始权值为0,接着从x开始,每走一圈的变化值是相同的
否则每走一圈的变化值都相同
题意:给定一个n个结点的环,编号0~n-1,每个点有一定的权值,从点0出发沿编号走,到达某一个节点则把目前总权值加上这个节点的权值,如果结果小于0则变成0。现在给你最多可以走的步数P和最大需要到达的权值大小G,问你需要的最小的初始权值为多少,能在P步内能够产生的最大权值大于等于G
初始权值越大,经过同样步数之后得到的权值越大,考虑二分,但是步数太大无法模拟
如果走第一圈的时候在某一点上x变成0,等价于初始权值为0,接着从x开始,每走一圈的变化值是相同的
否则每走一圈的变化值都相同
//china no.1 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <vector> #include <iostream> #include <string> #include <map> #include <stack> #include <cstring> #include <queue> #include <list> #include <stdio.h> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <functional> #include <stdlib.h> #include <time.h> #include <bitset> using namespace std; #define pi acos(-1) #define s_1(x) scanf("%d",&x) #define s_2(x,y) scanf("%d%d",&x,&y) #define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define s_4(x,y,z,X) scanf("%d%d%d%d",&x,&y,&z,&X) #define S_1(x) scan_d(x) #define S_2(x,y) scan_d(x),scan_d(y) #define S_3(x,y,z) scan_d(x),scan_d(y),scan_d(z) #define PI acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x,y) memset(x,y,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0); #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define fOR(n,x,i) for(int i=n;i>=x;i--) #define fOr(n,x,i) for(int i=n;i>x;i--) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) #define bug printf("***********\n"); #define db double #define ll long long #define mp make_pair #define pb push_back typedef long long LL; typedef pair <int, int> ii; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,1,-1,-1,1}; const int dy[]={0,1,0,-1,-1,1,-1,1}; const int maxn=2e5+10; const int maxx=4e5+10; const double EPS=1e-8; const double eps=1e-8; const int mod=1e9+7; template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} template <class T> inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;} while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;} inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false; while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;} else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}} if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}} if(IsN) num=-num;return true;} void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');} void print(LL a){ Out(a),puts("");} //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); //cerr << "run time is " << clock() << endl; int n; LL g,p; LL w[maxn]; bool check(LL x) { LL ans=x; if(p<=n) { for(int i=1;i<=p;i++) x=max(0LL,x+w[i]),ans=max(ans,x); if(ans>=g) return true; return false; } if(p<=2*n) { for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); for(int i=1;i<=p-n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); if(ans>=g) return true; return false; } if(p<=3*n) { for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); for(int i=1;i<=p-n-n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); if(ans>=g) return true; return false; } for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),ans=max(ans,x); LL x0=x; LL res=ans; for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),res=max(res,x); LL c=p/n,r=p%n,ret=max(res,x),x1=x; c-=3; for(int i=1;i<=n;i++) x=max(0LL,x+w[i]),ret=max(ret,x); for(int i=1;i<=r;i++) x=max(0LL,x+w[i]),ret=max(ret,x); if(x1>x0) { if(c>=(g-ret+x1-x0-1)/(x1-x0)) return true; else return false; } else { if(ret>=g) return true; else return false; } return false; } void solve() { S_3(n,g,p); FOR(1,n,i) S_1(w[i]); LL l=0,r=g; W(l<=r) { LL mid=(l+r)>>1; if(check(mid)) r=mid-1; else l=mid+1; } print(l); } int main() { //freopen( "in.txt" , "r" , stdin ); //freopen( "data.txt" , "w" , stdout ); int t=1; //init(); s_1(t); for(int cas=1;cas<=t;cas++) { printf("Case #%d: ",cas); solve(); } }
相关文章推荐
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive - 7501 思维 +二分
- UVALive 5873 (几何+思维)
- UVALive 7339 Owllen【思维】
- 思维 UVALive 3708 Graveyard
- UVALive 7267 Mysterious Antiques in Sackler Museum 思维题 拼接矩形
- Graveyard UVALive - 3708(思维)
- UVALive-7222 National Disaster【思维+排序+模拟】
- UVALive-7220 Dungeon Trap【最短路Bfs+思维枚举】好题~