Gym - 101177A Anticlockwise Motion 模拟
2017-11-05 00:06
399 查看
可以看成一个个嵌套的正方形
每个的左下角是 奇数 的平方,,然后模拟找到两个点的坐标
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000 + 7, maxd = (1<<18)-1, mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
ll n, m;
struct node{
int x,y;
};
node pos_n,pos_m;
int judge_edge(int n){
int ans = (int)sqrt(n);
int cnt;
if(ans * ans != n){
if (ans % 2 ==0 ){
cnt =ans +1;
}
else {
cnt = ans +2;
}
}
else {
if (ans %2){
cnt = ans;
}
else{
cnt = ans +1 ;
}
}
return cnt;
}
void find_(int cen,int s){
int res = cen * cen;
if (s >= res - (cen - 1)){
pos_n.x = 1;
pos_n.y = 1 + (res-s);
}
else if (s <res-(cen-1) && s >= res - 2*(cen-1)){
pos_n.x = 1 + ((res - (cen-1) ) -s );
pos_n.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
//cout <<"/*-*-/*-/*-/" <<endl;
int temp = res - 2* (cen-1);
pos_n.x = cen;
pos_n.y = cen - (temp - s );
}
else {
int temp = res - 3* (cen-1);
pos_n.y = 1;
pos_n.x = cen - (temp - s);
}
}
void find_1(int cen,int s){
int res = cen*cen;
if (s >= res - (cen - 1)){
pos_m.x = 1;
pos_m.y = 1 + (res-s);
}
else if (s < res-(cen-1) && s >= res - 2*(cen-1)){
pos_m.x = 1 + ((res - (cen-1) ) -s );
pos_m.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
int temp = res - 2* (cen-1);
pos_m.x = cen;
pos_m.y = cen - (temp - s );
}
else{
int temp = res - 3* (cen-1);
pos_m.y = 1;
pos_m.x = cen - (temp - s);
}
}
int main() {
ios::sync_with_stdio(0);
cin >>n >>m;
if (n > m){
swap(n,m);
}
int cen_n = judge_edge(n);
int cen_m = judge_edge(m);
//cout << cen_n << " "<< cen_m<<endl;
int max_cen = max(cen_n,cen_m);
find_(cen_n,n);
find_1(cen_m,m);
//cout << pos_n.x <<" "<< pos_n.y <<" "<< pos_m.x << " " << pos_m.y << endl;
pos_n.x += (cen_m- cen_n)/2;
pos_n.y += (cen_m- cen_n)/2;
int ress = 0;
ress = abs(pos_m.x-pos_n.x) + abs(pos_m.y-pos_n.y);
cout << ress << endl;
return 0;
}
每个的左下角是 奇数 的平方,,然后模拟找到两个点的坐标
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define PI acos(-1.0)
#define in freopen("in.txt", "r", stdin)
#define out freopen("out.txt", "w", stdout)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000 + 7, maxd = (1<<18)-1, mod = 1e9 + 7;
const int INF = 0x7f7f7f7f;
ll n, m;
struct node{
int x,y;
};
node pos_n,pos_m;
int judge_edge(int n){
int ans = (int)sqrt(n);
int cnt;
if(ans * ans != n){
if (ans % 2 ==0 ){
cnt =ans +1;
}
else {
cnt = ans +2;
}
}
else {
if (ans %2){
cnt = ans;
}
else{
cnt = ans +1 ;
}
}
return cnt;
}
void find_(int cen,int s){
int res = cen * cen;
if (s >= res - (cen - 1)){
pos_n.x = 1;
pos_n.y = 1 + (res-s);
}
else if (s <res-(cen-1) && s >= res - 2*(cen-1)){
pos_n.x = 1 + ((res - (cen-1) ) -s );
pos_n.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
//cout <<"/*-*-/*-/*-/" <<endl;
int temp = res - 2* (cen-1);
pos_n.x = cen;
pos_n.y = cen - (temp - s );
}
else {
int temp = res - 3* (cen-1);
pos_n.y = 1;
pos_n.x = cen - (temp - s);
}
}
void find_1(int cen,int s){
int res = cen*cen;
if (s >= res - (cen - 1)){
pos_m.x = 1;
pos_m.y = 1 + (res-s);
}
else if (s < res-(cen-1) && s >= res - 2*(cen-1)){
pos_m.x = 1 + ((res - (cen-1) ) -s );
pos_m.y = (cen);
}
else if (s < res - 2* (cen - 1) && s >= res - 3* (cen -1)){
int temp = res - 2* (cen-1);
pos_m.x = cen;
pos_m.y = cen - (temp - s );
}
else{
int temp = res - 3* (cen-1);
pos_m.y = 1;
pos_m.x = cen - (temp - s);
}
}
int main() {
ios::sync_with_stdio(0);
cin >>n >>m;
if (n > m){
swap(n,m);
}
int cen_n = judge_edge(n);
int cen_m = judge_edge(m);
//cout << cen_n << " "<< cen_m<<endl;
int max_cen = max(cen_n,cen_m);
find_(cen_n,n);
find_1(cen_m,m);
//cout << pos_n.x <<" "<< pos_n.y <<" "<< pos_m.x << " " << pos_m.y << endl;
pos_n.x += (cen_m- cen_n)/2;
pos_n.y += (cen_m- cen_n)/2;
int ress = 0;
ress = abs(pos_m.x-pos_n.x) + abs(pos_m.y-pos_n.y);
cout << ress << endl;
return 0;
}
相关文章推荐
- codeforces Gym 100338F Spam Filter 垃圾邮件过滤器(模拟,实现)
- 【模拟】NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
- codeforces Gym - 101190J ——Jenga Boom(模拟)
- Gym 101341M Last Man Standing 模拟|贪心
- Gym - 101341M-简单模拟-Last Man Standing
- C - Bored Judge Gym - 101102C 模拟
- Gym-101505D Rotating Display [模拟]
- 使用gym库模拟强化学习环境
- 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)
- Gym - 101196B Foosball Dynasty【模拟】
- Appearance Analysis Gym - 101173A(模拟)
- Gym 100641A Continued Fractions 模拟|签到
- 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)
- codeforces gym 100286 I iSharp (字符串模拟)
- UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)
- Gym - 100543D Wheels 模拟|BFS|签到
- Gym - 101667B Connect3 [模拟]
- Codeforces Gym - 101064A Renzo and the lost artifact [模拟退火]
- Gym 100851E Easy Problemset (水题,模拟)
- Gym 101341G I love Codeforces 模拟