ICPC North Central NA Contest 2017
C. Urban Design
思路:用直线的一般式判断两直线相交 通过奇偶性判断是否在同一区域
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 1e4 + 10;
int s, t, x1, y1_, x2, y2, x3, y3, x4, y4, cnt;
int a[N], b[N], c[N];
ll tmp1, tmp2;
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
for (int i = 1; i <= s; i ++ ){
cin >> x1 >> y1_ >> x2 >> y2;
a[i] = y1_ - y2;
b[i] = x2 - x1;
c[i] = x1 * y2 - y1_ * x2;
}
cin >> t;
while (t -- ){
cnt = 0;
cin >> x3 >> y3 >> x4 >> y4;
for (int i = 1; i <= s; i ++ ){
tmp1 = 1ll * x3 * a[i] + 1ll * y3 * b[i] + c[i],
tmp2 = 1ll * x4 * a[i] + 1ll * y4 * b[i] + c[i];
if (tmp1 * tmp2 < 0)
cnt ++ ;
}
if (cnt & 1)
cout << "different\n";
else
cout << "same\n";
}
return 0;
}
E. Is-A? Has-A? Who Knowz-A?
思路:floyd算法(忘了init导致wa1
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 510;
int n, m, tot;
bool Is[N][N], Has[N][N];
string a, b, c;
map<string, int> mp;
void init(){
for (int i = 1; i <= tot; i ++ )
Is[i][i] = true;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i ++ ){
cin >> a >> b >> c;
if (!mp.count(a))
mp[a] = ++ tot ;
if (!mp.count(c))
mp[c] = ++ tot ;
if (b == "is-a")
Is[mp[a]][mp[c]] = true;
else
Has[mp[a]][mp[c]] = true;
}
init();
for (int k = 1; k <= tot; k ++ ){
for (int i = 1; i <= tot; i ++ ){
for (int j = 1; j <= tot; j ++ ){
if (Is[i][k] && Is[k][j])
Is[i][j] = true;
if ((Has[i][k] && Has[k][j]) || (Has[i][k] && Is[k][j]) || (Is[i][k] && Has[k][j]))
Has[i][j] = true;
}
}
}
for (int i = 1; i <= m; i ++ ){
cin >> a >> b >> c;
if (b == "is-a"){
if (Is[mp[a]][mp[c]])
cout << "Query " << i << ": true\n";
else
cout << "Query " << i << ": false\n";
}
else{
if (Has[mp[a]][mp[c]])
cout << "Query " << i << ": true\n";
else
cout << "Query " << i << ": false\n";
}
}
return 0;
}
G. Sheba's Amoebas
思路:dfs
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 110;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int n, m, ans;
char mp[N][N];
void dfs(int x, int y){
mp[x][y] = '.';
for (int i = 0; i < 8; i ++ ){
int tx = x + dx[i],
ty = y + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && mp[tx][ty] == '#')
dfs(tx, ty);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
cin >> mp[i][j];
for (int i = 0; i < n; i ++ ){
for (int j = 0; j < m; j ++ ){
if (mp[i][j] == '#'){
dfs(i, j);
ans ++ ;
}
}
}
cout << ans << "\n";
return 0;
}
H. Zebras and Ocelots
思路:水题 直接用pow会wa(迷惑
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 65;
int n;
ll ans;
ll pow_2[N];
char c[N];
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
pow_2[0] = 1;
for (int i = 1; i < N; i ++ )
pow_2[i] = 2 * pow_2[i - 1];
cin >> n;
for (int i = n; i >= 1; i -- )
cin >> c[i];
for (int i = 1; i <= n; i ++ )
if (c[i] == 'O')
ans += pow_2[i - 1];
cout << ans << "\n";
return 0;
}
I. Racing Around the Alphabet
思路:水题 注意处理带空格的串
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const double pi = acos(-1.0);
const int N = 130;
int n, tmp1, tmp2;
double res, ans;
char s[N];
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
cin.getline(s, 130);
while (n -- ){
cin.getline(s, 130);
// cout << s << "\n";
int len = strlen(s);
ans = 0;
for (int i = 0; i < len - 1; i ++ ){
if (s[i] == ' ')
tmp1 = 0;
else if (s[i] == '\'')
tmp1 = 1;
else
tmp1 = int(s[i] - 'A') + 2;
if (s[i + 1] == ' ')
tmp2 = 0;
else if (s[i + 1] == '\'')
tmp2 = 1;
else
tmp2 = int(s[i + 1] - 'A') + 2;
res = 60 * pi / 28.0 * min(abs(tmp1 - tmp2), 28 - abs(tmp1 - tmp2));
ans += res / 15.0;
}
cout << fixed << setprecision(6) << ans + len << "\n";
}
return 0;
}
J. Lost Map
思路:最小生成树 数组开小了段错误+1
AC代码
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll mult_mod(ll x, ll y, ll mod){
return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}
ll pow_mod(ll a, ll b, ll p){
ll res = 1;
while (b){
if (b & 1)
res = mult_mod(res, a, p);
a = mult_mod(a, a, p);
b >>= 1;
}
return res % p;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b) : a;
}
const int N = 2510;
int n, tot;
int fa[N];
int mp[N][N];
void init(){
for (int i = 1; i <= N; i ++ )
fa[i] = i;
}
int find(int x){
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void join(int x, int y){
if (find(x) != find(y))
fa[find(x)] = find(y);
}
struct Edge{
int u, v, dis;
Edge(){ };
Edge(int _u, int _v, int _dis): u(_u), v(_v), dis(_dis){ };
bool operator <(const Edge &e) const{
return dis < e.dis;
}
}e[N * N];
int main(){
#ifndef ONLINE_JUDGE
freopen("my_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
cin >> n;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
cin >> mp[i][j];
for (int i = 1; i <= n; i ++ ){
for (int j = i + 1; j <= n; j ++ ){
Edge edge(i, j, mp[i][j]);
e[tot ++ ] = edge;
}
}
sort(e, e + tot);
for (int i = 0; i < tot; i ++ ){
if (find(e[i].u) == find(e[i].v))
continue;
join(e[i].u, e[i].v);
cout << e[i].u << " " << e[i].v << "\n";
}
return 0;
}