比赛链接:Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics)
A:Alexey and Train
题意:从 0 站台出发到达第
n
n
n 号站台的时间,
a
i
a_i
ai 表示火车到达第
i
i
i 站的时间,
b
i
b_i
bi 表示火车离开第
i
i
i 个站台的时间。
t
i
t_i
ti 表示第
i
−
1
i-1
i−1 号站台到达
i
i
i 号站台的时间。
题解:第 i − 1 i-1 i−1 号站台到 i i i 号站台时间 r e s res res += a [ i ] − b [ i − 1 ] + t [ i ] a[i] - b[i-1] + t[i] a[i]−b[i−1]+t[i],比较一下 b [ i ] b[i] b[i]与累计的 r e s + c e i l ( ( b [ i ] − a [ i ] ) / 2.0 ) res + ceil((b[i]-a[i])/2.0) res+ceil((b[i]−a[i])/2.0)的大小, b [ i ] b[i] b[i] 大, r e s = b [ i ] res = b[i] res=b[i],否则 r e s + = c e i l ( ( b [ i ] − a [ i ] ) / 2.0 ) res += ceil((b[i]-a[i])/2.0) res+=ceil((b[i]−a[i])/2.0)。直接按照题意写即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 107;
int t, n, a[maxn], b[maxn], r[maxn];
int main()
{
scanf("%d",&t);
while (t--)
{
scanf(" %d",&n);
for (int i = 1; i <= n; i++) scanf("%d %d", &a[i], &b[i]);
for (int i = 1; i <= n; i++) scanf("%d", &r[i]);
ll res = 0;
for (int i = 1; i <= n; i++)
{
res += r[i] + a[i] - b[i - 1];
if (i < n && res + ceil((b[i] - a[i]) / 2.0) <= b[i]) res = b[i];
else if(i < n) res += ceil((b[i] - a[i]) / 2.0);
}
printf("%lld\n", res);
}
return 0;
}
B:Napoleon Cake
题意:有
n
n
n 层面包,每层面包放奶油,奶油可以向下浸透,0 表示当前面包是干的,1 表示当前面包是湿的,问这
n
n
n 层面包每层的状态是什么样。
直接xjb搞,后面覆盖前面即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int t, n, a[maxn],vis[maxn];
int main()
{
scanf("%d",&t);
while (t--)
{
memset(vis, 0, sizeof vis);
scanf(" %d",&n);
for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
for(int i = n; i > 0; i--)
{
if(a[i])
{
if(a[i] > a[i-1]) a[i-1] = a[i] - 1;
vis[i] = 1;
}
}
for(int i = 1; i <= n; i++)
if(i == 1) printf("%d",vis[i]);
else printf(" %d",vis[i]);
puts("")
}
return 0;
}
C:Going Home
题意: 给你
n
n
n 个数,你从中找出 4 个数
x
x
x ,
y
y
y,
z
z
z,
w
w
w,使得
a
x
+
a
y
=
=
a
z
+
a
w
a_x + a_y == a_z + a_w
ax+ay==az+aw,如果存在输出
Y
E
S
YES
YES,并且输出
x
x
x,
y
y
y,
z
z
z,
w
w
w, 否则输出
N
O
NO
NO。
题解: 抽屉原理:把多于
n
n
n 个的物体放到
n
n
n 个抽屉里,则至少有一个抽屉里的东西不少于两件。
a
a
a +
b
b
b =
c
c
c +
d
d
d
=
=
>
==>
==> 转化成
a
−
c
a - c
a−c =
d
−
b
d - b
d−b,题目中
a
i
a_i
ai 范围小,所以无论怎么构造数据,有近 3000 个左右的数必然存在,所以只需要在 3000 个数来找出 4 个数就行了,所以
O
(
n
2
)
O(n^2)
O(n2) 的时间复杂度能过。
抽屉原理百度百科
#include <bits/stdc++.h>
#define PII pair<int,int>
using namespace std;
struct node{
int num,pos;
bool operator < (const node &t)const{
if(num != t.num) return num < t.num;
return pos < t.pos;
}
};
int main()
{
int n;
scanf("%d",&n);
vector<node>a(n);
for(int i = 0; i < n; i++) scanf("%d",&a[i].num), a[i].pos = i + 1;
sort(a.begin(), a.end());
map<int,PII >mp;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
{
int pre = a[i].num - a[j].num;
if(mp.count(pre))
{
int l1 = mp[pre].first;
int l2 = mp[pre].second;
int r1 = a[j].pos;
int r2 = a[i].pos;
if(l1 == r1|| l1 == r2|| l2 == r1|| l2 == r2) continue;
puts("YES");
printf("%d %d %d %d\n",r1,l2,l1,r2);
goto good;
}
else
mp[pre]={a[j].pos, a[i].pos};
}
}
puts("NO");
good:;
return 0;
}
贴一个xjb搞的代码
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pf push_front
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define NMSL cout << "NMSL" << endl;
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define in128 __int128_t
using namespace std;
const int N = 2e5+10;
const int INF = numeric_limits<int>::max() / 4;
const double EPS = 1e-3;
const long double PI = 3.14159265358979323846;
int n;
struct node
{
int num,pos;
} a[N];
int cx[N];
bool cmp(node l1,node l2)
{
return l1.num<l2.num;
}
void work()
{
for(int x=0; x<n; ++x)
{
for(int y=n-1; y>x; --y)
{
for(int z=0; z<n; ++z)
{
if(z==x||z==y)
continue;
for(int w=n-1; w>z; --w)
{
if(w==x||w==y)
continue;
if(a[x].num+a[y].num==a[z].num+a[w].num)
{
cout<<"YES"<<endl;
cout<<a[x].pos<<' '<<a[y].pos<<' '<<a[z].pos<<' '<<a[w].pos<<endl;
return ;
}
}
}
}
}
cout<<"NO"<<endl;
}
signed main()
{
fio
cin>>n;
for(int i=0; i<n; ++i)
{
cin>>a[i].num;
a[i].pos=i+1;
}
sort(a,a+n,cmp);
if(n<=100)
{
work();
return 0;
}
for(int i=1; i<n; ++i)
{
int pre=a[i].num-a[i-1].num;
if(cx[pre]==0)
cx[pre]=i;
else if(cx[pre]==i-1)
continue;
else
{
cout<<"YES"<<endl;
cout<<a[cx[pre]-1].pos<<' '<<a[i].pos<<' '<<a[cx[pre]].pos<<' '<<a[i-1].pos<<endl;
return 0;
}
}
cout<<"NO"<<endl;
return 0;
}