Creating a Character(cf A.)二分
You play your favourite game yet another time. You chose the character you didn't play before. It has str
points of strength and int points of intelligence. Also, at start, the character has exp free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by 1 or raise intelligence by 1
).
Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).
Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.
The first line contains the single integer T
(1≤T) — the number of queries. Next T
lines contain descriptions of queries — one per line.
This line contains three integers str
, int and exp (1≤str,int≤108, 0≤exp≤108
) — the initial strength and intelligence of the character and the number of free points, respectively.
Print T
integers — one per query. For each query print the number of different character builds you can create.
4 5 3 4 2 1 0 3 5 5 4 10 6
3 1 2 0
二分写得不是很好,不知道怎么控制最后结果刚好停留在最大的分配给int的exp这个结果上,又不死循环。然后就大概写了一下,区间长度小于等于1就退出,然后特判了一下 l 和 r。
//#pragma GCC optimize(2) #pragma comment(linker, "/STACK:10240000,10240000") #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<queue> #include<map> #include<set> #include<stack> #include<list> #include<ctime> #include<ctype.h> #include<stdlib.h> #include<bitset> #include<algorithm> #include<assert.h> #include<numeric> //accumulate #define endl "\n" #define fi first #define se second #define forn(i,s,t) for(int i=(s);i<(t);++i) #define mem(a,b) memset(a,b,sizeof(a)) #define rush() int MYTESTNUM;cin>>MYTESTNUM;while(MYTESTNUM--) #define debug(x) printf("%d\n",x) #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define mp make_pair #define pb push_back #define sc(x) scanf("%d",&x) #define sc2(x,y) scanf("%d%d",&x,&y) #define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define pf(x) printf("%d\n",x) #define pf2(x,y) printf("%d %d\n",x,y) #define pf3(x,y,z) printf("%d %d %d\n",x,y,z) #define ll long long #define ull unsigned long long #define dd double #define pfs puts("*****") #define pfk(x) printf("%d ",(x)) #define kpf(x) printf(" %d",(x)) #define pfdd(x) printf("%.5f\n",(x)); #define pfhh printf("\n") using namespace std; const ll P=1e9; ll mul(ll a, ll b){ll ans = 0;for(;b;a=a*2%P,b>>=1) if(b&1) ans=(ans+a)%P;return ans;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} const int maxn=550; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } int a,b,c; inline bool pd(int x){ return a+(c-x)>b+x; } int main() { rush() { cin>>a>>b>>c; if(a+c<=b) cout<<0<<endl; else{ int l=0,r=c; while(r-l>1){ int mid = (l+r)/2; if(pd(mid)) l=mid; else r=mid-1; } int ans; if( pd(r) ) ans=r; else ans=l; ans++; cout<<ans<<endl; } } return 0; }