就是解个方程,也没什么说的
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[4],x,b,c;
for(int i = 0;i < 4; i++)
cin >> a[i];
sort(a,a+4);
c = a[3] - a[0];
x = a[1] - c;
b = a[2] - c;
cout << x << " " << b << " " << c << endl;
return 0;
}
就是找到一个最小的正整数D,使得集合里的所有元素,加上或者减去这个D变成一个数,最后集合里的数全部变成一样的,所有的数只能更改一遍,所以只要找最大值和最小值,变化一下看是否满足条件就OK了。
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,x[10010];
set<int>s;
cin >> n;
for(int i = 0;i < n; i++)
{
cin >> x[i];
s.insert(x[i]);
}
sort(x,x+n);
int cha = x[n-1] - x[0];
int p = x[n-1],q = x[0];
if(s.size() == 2)
{
if(cha & 1)
cout << cha;
else
cout << cha / 2;
return 0;
}
if(cha & 1 || s.size() > 3)
{
cout << "-1";
return 0;
}
cha /= 2;
s.clear();
for(int i = 0;i < n; i++)
{
if(x[i] == p)
{
x[i] -= cha;
s.insert(x[i]);
}
else if(x[i] == q)
{
x[i] += cha;
s.insert(x[i]);
}
else
s.insert(x[i]);
}
if(s.size() == 1)
{
cout << cha;
return 0;
}
else
{
cout << "-1";
return 0;
}
return 0;
}
就按照题意模拟,枚举从周一到周末的每天出发,然后找最大值就可以了。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
int x[maxn];
int main()
{
#ifndef ONLINE_JUDGE
// freopen("in","r",stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
/*int n,k;
cin >> n >> k;
for(int i = 1;i <= n; i++)
return 0;*/
ios_base::sync_with_stdio(false);
ll n,m,k;
cin >> n >> m >> k;
ll ans = min(n / 3,min(m / 2,k / 2));
n -= 3 * ans;
m -= 2 * ans;
k -= 2 * ans;
ans *= 7;
ll maxn = 0;
for(int i = 1;i <= 7;i++){
int t1 = n,t2 = m,t3 = k;
ll pt = 0;
for(int j = i;j <= i + 7; j++){
if(j == 1 || j == 4 || j == 7 || j == 8 || j == 11 || j == 14)
{
if(t1>0)
{
t1--;
pt++;
}
else
break;
}
else if(j==2||j==6||j==9||j==13)
{
if(t2>0)
{
t2--;
pt++;
}
else
break;
}
else if(j==3||j==5||j==10||j==12)
{
if(t3>0)
{
t3--;
pt++;
}
else
break;
}
}
maxn =max(maxn,pt);
}
cout<<ans+maxn<<endl;
}
就是一个贪心题,是有光就优先用电池,没光就优先用accumulator。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
int x[maxn],y[maxn],vis[maxn];
int a[200010];
typedef long long ll;
int main()
{
ios_base::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
// freopen("in","r",stdin);
#endif
cin.tie(0);
cout.tie(0);
ll n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
cin >> a[i];
int t1 = k;
for (int i = 0; i < n; i++){
if (m <= 0 && t1 <= 0) {
cout << i << endl;
exit(0);
}
if (a[i] == 0){
if (t1 > 0)
t1--;
else{
if (m > 0) m--;
if (m <= 0 && t1 <= 0) {
cout << i + 1 << endl;
exit(0);
}
}
}
else{
if (m > 0) {
if (t1 < k) m--, t1++;
else if (t1 == k) t1--;
}
else {
if (t1 > 0) t1--;
if (m <= 0 && t1 <= 0){
cout << i + 1 << endl;
exit(0);
}
}
}
}
cout << n << endl;
}
用一个链表的思想去暴力,纯暴力一定会超时
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
int x[maxn],y[maxn],vis[maxn],lef[maxn],rig[maxn],flag;
void Del(int index)
{
flag == 0 ? vis[index] = 1 : vis[index] = 2;
lef[rig[index]] = lef[index];
rig[lef[index]] = rig[index];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in","r",stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,k;
cin >> n >> k;
for(int i = 1;i <= n; i++)
{
cin >> x[i];
y[x[i]] = i;
lef[i] = i - 1;
rig[i] = i + 1;
}
for(int i = n;i >= 1; i--)
{
int m = y[i];
if(vis[m])
continue;
Del(m);
m = lef[m];
for(int j = 0;j < k && m >= 1; j++)
{
Del(m);
m = lef[m];
}
m = rig[m];
for(int j = 0;j < k && m <= n; j++)
{
Del(m);
m = rig[m];
}
flag = !flag;
}
for(int i = 1;i <= n; i++)
cout << vis[i];
return 0;
}
标签:int,1154,++,cin,552,--,maxn,Div,t1 From: https://blog.51cto.com/u_16131191/6356130