#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/intmain(){Run();
ll T;sd(T);while(T --){
string s;
cin >> s;
cout <<(s.size())<< endl;}return0;}
B. Jumps
题意
刚开始的时候,一个人在 ox 轴的原点 0 位置,想要到 x 位置,
对于第 i 次操作,假设当前这个人的位置在 y,
第一种操作可以让这个人的向前走 i 步,即 y += i;
第二种操作可以让这个人向后走 1 步,即 y -= 1;
问到 x 最少需要走多少步?
思路
这一题有点难叙述,大致的思路就是,当人的位置 y 小于 x 的时候,一直向前走(假设这个是第 i 步),那么令 y+= i, 如果此时 y 仍然小于 x 继续令 y+=(i+1)…,
一直加 i 直到,y >= x 的时候,这个时候计算出差值:cha = y - x,如果差值 > i+1 的话就继续进行第一种操作,(需要特判 cha = 1 的时候也需要进行第一种操作,因为差 1 的时候,我们需要把 i0 的时候的哪一步从第一种操作变成第二种操作,但是 i==0 是不存的)
到 y >= x && cha <= i + 1 的时候我们需要 cha 给去除掉,这个时候我们只需要让 i=cha-1 的时候的哪一步从 y+= i 变成 y -= 1 操作就行了.
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/intmain(){Run();
ll T;sd(T);while(T --){
ll n;sd(n);
ll sum =0;
ll ans;for(ll i =1;; i ++){
sum += i;if(sum < n)continue;if(sum >= n){
ll cha = sum - n;if(cha ==1)continue;if(cha <= i +1){
ans = i;break;}}}pr("%lld\n", ans);}return0;}
重点来了:Both Alice and Bob play optimally and want to, firstly, maximize their number of wins and, secondly, minimize the number of wins of their opponent. 这个两个人都会先考虑在自己赢最多局的情况下,才会考虑让对手尽可能的多输。
思路
这题分类讨论一下,就好做多了,
首相 x == y, 的时候,如果 Bob 想自己赢得尽可能的多的情况下,那么最好就是 Alice 的前 x-1 个次发球不接,当 Alice 最后一次发球的时候,Bob 再接,那么 Alice 赢 x-1 场, Bob 赢 y 场;
当 x < y 的时候,也是 Alice 的前 x-1 个球不接,Alice 第 x 次发球的时候接球,那么 Alice 赢 x-1 场, Bob 赢 y 场;
当 x > y 的时候的 Alice 先发球,我们考虑,Bob 接不接?Bob 当然不会接,因为 Bob 接的话,反而赢得次数会变少(参考题意中的 5),所以这种情况也是 Alice 的前 x-1 个球不接,Alice 第 x 次发球的时候接球,那么 Alice 赢 x-1 场, Bob 赢 y 场
综上答案就是:x-1, y
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/intmain(){Run();
ll T;sd(T);while(T --){
ll x, y;sc("%lld %lld",&x,&y);printf("%lld %lld\n", x -1, y);}return0;}
D. Sequence and Swaps
题意
给定一个长度 为 n 的序列 a [],和一个有初始值的 x,
思路
这一题应该就是一个明显考察贪心思维的题,其实看懂了非常简单,
我们考虑如果序列原来就是有序的,那就不用排序了,
如果不是有序的那我们就应该从左往右找到序列中第一个比 x 的值大的那个数(设其下标为 j)令 x 与 a [x] 的值进行交换 (考虑为什么从左往右找第一个比 x 值大的那个数? 这个是因为:如果我们确定这个序列是不是有序的,如果我不与 a [j] 这个元素进行交换,那么后面 x 必定需要 j 下标之后的某个比 x 大元素(设其下标为 k)进行交换,当 swap (x, a [k]) 之后, 那么 a [k] 的值必然小于 a [j] 的值,使序列变的更加无序)
之后检查序列是不是有序循环执行步骤 3,否则结束,输出统计的交换次数!!!
代码
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<string>#include<string>#include<map>#include<bitset>#include<vector>voidfre(){system("clear"),freopen("A.txt","r",stdin);freopen("Ans.txt","w",stdout);}voidFre(){system("clear"),freopen("A.txt","r",stdin);}voidRun(int x =0){#ifdef ACM //宏定义免注释 freopenif(! x)fre();elseFre();#endif}#define ios ios::sync_with_stdio(false)#define Pi acos(-1)#define pb push_back#define fi first#define se second#define db double#define ll long long#define ull unsigned long long#define Pir pair<ll, ll>#define m_p make_pair#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)#define memset(a, b, c) memset(a, (int)b, c);#define size() size() * 1LL#define sc scanf#define pr printf#define sd(a) scanf("%lld", &a)#define ss(a) scanf("%s", a)#define inf 0x3f3f3f3f#define INF 0x3f3f3f3f3f3f3f3f#define esp 1e-7#define mod (ll)(1e9 + 7)
using namespace std;/*=========================ACMer===========================*/const ll mxn =1005;
ll ar[mxn];
bool check(ll l, ll r){for_(i, l +1, r){if(ar[i]< ar[i -1])return0;}return1;}intmain(){Run();
ll T;sd(T);while(T --){
ll n, x;sc("%lld %lld",&n,&x);for_(i,1, n)sd(ar[i]);
ar[n +1]= INF;
ll id =1, ans =0;while(id != n +1){if(check(id, n))break;while(ar[id]<= x) id ++;swap(ar[id], x);
ans ++;}if(check(1, n))pr("%lld\n",max(ans,0LL));elsepr("-1\n");}return0;}