您的位置:首页 > 其它

Codeforces Round #340 (Div. 2) (617A,617B,617C,617D(Constructive ),617E(莫队算法))

2016-01-27 17:27 417 查看
比赛的时候由于入了一道题的坑,而且一直没爬出来。。。

Elephant

题目连接:

http://codeforces.com/contest/617/problem/A

解题思路:

It's optimal to do the biggest possible step everytime. So elephant should do several steps by distance 5 and one or zero step
by smaller distance. Answer equals to 


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int main(){
int x;
scanf("%d",&x);
if(x%5)
printf("%d\n",x/5+1);
else
printf("%d\n",x/5);
return 0;
}


Chocolate

题目连接:

http://codeforces.com/contest/617/problem/B

解题思路:

We are given array which contains only ones and zeroes. We must divide it on parts with only one 1.

Tricky case: when array contains only zeroes answer equals to 0.

In general. Between two adjacent ones we must have only one separation. So, answer equals to product of values posi - posi - 1 whereposi is
position of i-th one.
弱英语差,没看到这句话:You are asked to calculate the number of ways he can do it. 

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;
int a[110];

int main(){
int n;
while(~scanf("%d",&n)){
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
int i;
for(i = 0; i < n; i++){
if(a[i] == 1)
break;
}
if(i == n){
printf("0\n");
continue;
}
ll cnt = 1,ans = 1;
for(; i < n; i++){
if(a[i] == 1){
ans *= cnt;
cnt = 1;
}
else
cnt++;
}
printf("%lld\n",ans);
}
return 0;
}


Watering Flowers

题目连接:

http://codeforces.com/contest/617/problem/C

解题思路:

First radius equals to zero or distance from first fountain to some flower. Let's iterate over this numbers. Second radius equals
to maximal distance from second fountain to flower which doesn't belong to circle with first radius. Now we should choose variant with minimalr12 + r22.

AC代码(n^2):

#include <iostream>
#include <vector>
#define INF 1e18
using namespace std;

typedef long long ll;

ll square(int x){
return (ll)x * x;
}

int main() {
ios_base::sync_with_stdio(false); cin.tie(0);

int n, x1, y1, x2, y2;
while(cin >> n >> x1 >> y1 >> x2 >> y2){
vector<pair<ll, ll> > dist(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
dist[i].first = square(x - x1) + square(y - y1);
dist[i].second = square(x - x2) + square(y - y2);
}
dist.push_back({0, 0});

ll result = INF;
for(int i = 0; i <= n; i++){
ll r1 = dist[i].first;
ll r2 = 0;
for(int j = 0; j <= n; j++){
if(dist[j].first > r1){
r2 = max(r2, dist[j].second);
}
}
result = min(result, r1 + r2);
}
cout << result << endl;
}
return 0;
}


AC代码(nlogn):

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef long long ll;

ll square(int x) {
return x * (ll) x;
}

int main() {
ios_base::sync_with_stdio(false); cin.tie(0);

int n, x1, y1, x2, y2;
while(cin >> n >> x1 >> y1 >> x2 >> y2){
vector< pair<ll, ll> > dist(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
dist[i].first = square(x - x1) + square(y - y1);
dist[i].second = square(x - x2) + square(y - y2);
}

sort(dist.begin(), dist.end());
vector<ll> maxsuf(n + 1);
for (int i = n - 1; i >= 0; i--) {
maxsuf[i] = max(maxsuf[i + 1], dist[i].second);
}

ll result = min(dist[n - 1].first, maxsuf[0]);
for (int i = 0; i < n; i++) {
ll r1 = dist[i].first;
ll r2 = maxsuf[i + 1];
result = min(result, r1 + r2);
}
cout << result << endl;
}
return 0;
}


Polyline

题目连接:

http://codeforces.com/contest/617/problem/D

解题思路:

Answer equals to one if all coordinates x or y of points are same.

When answer equals to two? Let's iterate over all pairs of points. Let first point in pair is beginning of polyline, second point is end. Only one or two such polylines with answer two exist. If third point is on the polyline it belongs to rectangle with corners
in first two points. We can just check it.

Else answer equals to three. We can build vertical lines which contains the most left and the most right point and horizontal line through third point. If we erase some excess rays we will get polyline.

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int x[3], y[3];

bool is_between(int a, int b, int c) {
return min(a, b) <= c && c <= max(a, b);
}

bool judge(int i, int j, int k) {
return (x[k] == x[i] || x[k] == x[j]) && is_between(y[i], y[j], y[k]) ||
(y[k] == y[i] || y[k] == y[j]) && is_between(x[i], x[j], x[k]);
}

int main(){
for(int i = 0; i < 3; i++)
scanf("%d%d",&x[i],&y[i]);

if(x[0] == x[1] && x[1] == x[2] || y[0] == y[1] && y[1] == y[2]){
puts("1");
}else if(judge(0,1,2) || judge(0,2,1) || judge(1,2,0)){
puts("2");
}else{
puts("3");
}
return 0;
}


XOR and Favorite Number

题目连接:

http://codeforces.com/contest/617/problem/E

解题思路:

We have array a.

Let's calculate array pref (pref[0] = 0, 

).

Xor of subarray a[l...r] equals to 

.

So query (l, r) is counting number of pairs i, j (l - 1 ≤ i < j ≤ r) 

.

Let we know answer for query (l, r) and know for all v cnt[v] —
count of v in a[l - 1...r].
We can update in O(1) answer and cnt if we move left or right border of query on 1. So we can solve problem offline in 

 with
sqrt-decomposion (Mo's algorithm).

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long ll;
const int blocks = 300;

struct Query{
int l, r, id;
bool operator < (const Query& other) const{
return l / blocks < other.l / blocks || (l / blocks == other.l / blocks && r < other.r);
}
};

int n, m, k;
Query q[100000];
ll ans[100000];
int s[100001];
int cnt[1 << 20];
ll cur;

void add(int x){
cur += cnt[x ^ k];
cnt[x]++;
}

void del(int x){
cnt[x]--;
cur -= cnt[x ^ k];
}

int main(){
while(~scanf("%d%d%d",&n,&m,&k)){
for (int i = 1; i <= n; i++){
scanf("%d", &s[i]);
s[i] ^= s[i - 1];
}
for (int i = 0; i < m; ++i){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id = i;
}
sort(q,q+m);
int l = 1, r = 0;
for(int i = 0; i < m; ++i){
while(q[i].l - 1 > l)
del(s[l++]);
while(q[i].l - 1 < l)
add(s[--l]);
while(q[i].r > r)
add(s[++r]);
while(q[i].r < r)
del(s[r--]);
ans[q[i].id] = cur;
}
for (int i = 0; i < m; ++i){
printf("%lld\n", ans[i]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: