A:给你一个整数n,写出一个n-1的序列(1~n-1每个数出现一次),使得 a[i+1] % n = 2a[i] % n或者 a[i+1] % n = 3a[i] % n
写题时直接暴力,先考虑2倍再考虑3倍,但是不明白证明
如果打表的话,你会发现,从1开始,一直翻2倍的会形成一个环,同理,一直翻3倍也会形成一个环。
所以,优先翻2倍,发现2倍的已经使用过(回到环的起点),需要翻3倍进入到3倍的环当中。
一直到2倍3倍的环都走过为止。(若n-1没有达到,环便不能走了,输出-1)
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int maxn = 1e6 + 50;
int m[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
{
m[i] = 0;
}
if(n == 2)
{
cout << 1 << endl;
}
else
{
vector<int> ans;
int num = 1;
ans.push_back(1);
m[1] = 1;
int last = 1;
int flag = 0;
while(num < n - 1)
{
int a1 = 2*last % n;
int a2 = 3*last % n;
if(m[a1] == 0)
{
last = a1;
ans.push_back(a1);
m[a1]++;
num++;
}
else if(m[a2] == 0)
{
last = a2;
ans.push_back(a2);
m[a2]++;
num++;
}
else
{
flag = 1;
cout << -1 << endl;
break;
}
}
if(!flag)
{
for(int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
cout << endl;
}
}
}
return 0;
}
E:模拟将右侧的方块推到左侧,求最小的最大高度。
二分答案,然后在check函数下一点功夫。
如何判断当前枚举的最大高度x可以实现呢?
所有的方块都是从右往左推,所以,我计算左侧最多还可以加多少个方块即可。
如果左侧可以加的方块,小于右边需要移走的数目,那么就会多出方块无处可放,错误。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 50;
int a[maxn];
int n;
int check(int x)
{
long long sum = 0;
for(int i = 1; i <= n; i++)
{//从左往右看,这样记录前面最多还能放多少个方块
sum += a[i] - x;
//记录第i列最多还能放多少个
if(sum > 0) return 0;
//有多余的方块 ,就错误
}
return 1;
}
int solve(int l, int r)
{
while(l < r)
{
int mid = (l+r)/2;
if(check(mid))
{
r = mid;
}
else
{
l = mid + 1;
}
}
return l;
}
//二分的时候,先判断mid是否成立,成立就更新r,可以确保最后l和r取到最小值
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
cin >> n;
int maxh = 0;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
maxh = max(maxh,a[i]);
}
int ans = solve(1,maxh);
cout << ans << endl;
}
return 0;
}
D:酒馆战棋,题意不讲了,玩个炉石的都知道,不玩的不用写了。
一开始贪心地策略错了,我们应该尽可能用鱼人去换掉对方的鱼人。
所以,一开始用a3去换掉对方的b3或者b4。
用得到的植物去消耗b1,b2的圣盾。
之后再用a1去换掉对方没有圣盾的鱼人。
即:总体的策略:我方顺序3124,打对方的3412.
值得注意的是,如果我方的a3,a4存在,则可以白吃对方的植物。
a1,a2吃会浪费圣盾。
#include <iostream>
#include <algorithm>
using namespace std;
int a1,a2,a3,a4;
int b1,b2,b3,b4;
int zhiwua,zhiwub;
void solve()
{
if(b3) b3--,zhiwub++;
else if(b4) b4--;
else if(b1) b1--,b3++;
else if(b2) b2--,b4++;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
cin >> a1 >> a2 >> a3 >> a4;
cin >> b1 >> b2 >> b3 >> b4;
zhiwua = 0;
zhiwub = 0;
while((a1+a2+a3+a4) && (b1+b2+b3+b4))
{//双方都还有随从
if(a3 + a4) zhiwub = 0;//免费吃
while(zhiwua && (b1+b2))
{
if(b1)
{
b1--;
b3++;
}
else
{
b2--;
b4++;
}
zhiwua--;
}//消耗植物换圣盾
if(a3)
{
a3--;
zhiwua++;
solve();
}
else if(a1)
{
a1--;
a3++;
solve();
}
else if(a2)
{
a2--;
a4++;
solve();
}
else if(a4)
{
a4--;
solve();
}
// cout << a1 << " " << a2 << " " << a3 << " " << a4 << endl;
// cout << b1 << " " << b2 << " " << b3 << " " << b4 << endl;
// cout << zhiwua << " " << zhiwub << endl;
}
if((b1+b2+b3+b4) || ((a1+a2+a3+a4) == 0 && zhiwub >= zhiwua))
{
cout << "No" << endl;
}
else cout << "Yes" << endl;
}
return 0;
}
I:有n支队伍,每两支队伍需要比赛一次,一天只可以有一场比赛。对于每支队伍而言,在他队伍所参与的第一场比赛时到达,在他队伍所参与的最后一场比赛后离开。问,如何安排比赛顺序,使得所有队伍住宿的总天数最小。
一开始打算直接 先给1比赛完,再给2比赛,再给3比赛…,后来发现这样总天数会多。
最少的天数,无疑就是早来的早走,能晚来的尽晚来(没必要早早来了,徒增天数)
使得对于每一天来说,住宿的队伍都最少。
我们假设1~n队伍,按照序号的顺序到达.总天数n*(n-1)/2
于是可以得到:
1.有n支队伍的日子,至少有一天。即:最早来的队伍,与最晚来的队伍比赛之后就走了。
2.至少有n-1支队伍的日子,至少有4天。
证明:假设n = 6,我们不从一开始去推比赛顺序,肯定中间有一场比赛1,6。6最后到达,到达后和1比赛,1就离开了。
之后,我们想要寻找2离开的情况。
那就是:1,5 1,6 2,5 2,6 (顺序不唯一)2与6比完之后离开。 1,5是5刚到达。
得到至少有4天,至少有n-1 = 5支队伍。
3.在第一天和最后一天,肯定只有两只队伍,所以,至少有3的支队伍,有n *(n - 1 )/ 2 - 2天。
总结: 我们让先来的一部分队伍互相比赛,之后,让先走的队伍和最后来的队伍比赛,及时地削减队伍的数目,最后让后面来的队伍互相比赛即可
备注:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i = 2; i <= n/2; i++)
{
for(int j = 1; j < i; j++)
{
cout << i << " " << j << endl;
}
}
//首先是内部比赛
for(int i = n/2 + 1; i <= n; i++)
{
for(int j = 1; j <= n-i; j++)
{
cout << i << " " << j << endl;
}
}
//输出:1,4 2,4 1,5
for(int i = 1; i <= n/2; i++)
{
for(int j = n-i+1; j <= n; j++)
{
cout << i << " " << j << endl;
}
}
//输出中间部分:1,6 2,5 2,6 3,4 3,5 3,6.......
for(int i = n/2+1; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
cout << i << " " << j << endl;
}
}
//最后输出后半部分的相互比赛
}
return 0;
}