c_cpp [HackerRank]预订Hackathon 2015
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp [HackerRank]预订Hackathon 2015相关的知识,希望对你有一定的参考价值。
n = input()
hotel_id = []
price = []
fac = []
for i in range(0,n):
hotel_info = raw_input().split()
hotel_id.append(int(hotel_info[0]))
price.append(int(hotel_info[1]))
hotel_fac = set(hotel_info[2:])
fac.append(hotel_fac)
m = input()
for i in range(0,m):
result = []
request = raw_input().split()
budget = int(request[0])
list_fac = set(request[1:])
for j in range(0, n):
if budget >= price[j] and list_fac <= fac[j]:
# create tuple
new_cand = (len(fac[j]), price[j], hotel_id[j])
result.append(new_cand)
#print result
sorted_result = sorted(result, key = lambda x: (-x[0], x[1], x[2]))
output = [x[2] for x in sorted_result]
print ' '.join(str(item) for item in output)
min_tag = int(raw_input())
tag_list = []
city_name_list = []
n = 0
while 1:
try:
info = raw_input()
except EOFError:
break
info = info.split(":")
n += 1;
#print "INPUT = ", info
city_name = info[0]
city_name_list.append(info[0])
city_tag = info[1].split(",")
tag_list.append(set(city_tag))
#print city_name, city_tag
#print city_name_list
#print tag_list
cand_mem = []
cand_tag = []
for i in range(n):
# go through each city
# try adding city to candidate tag_list
new_list_mem = []
new_list_tag = []
for j in range(len(cand_mem)):
# get common tag between candidate and new city
old_tag_set = cand_tag[j].copy()
new_tag_set = tag_list[i].copy()
# if list is the same, add member
# else, create new entry
common_tag_set = old_tag_set.intersection(new_tag_set)
if len(common_tag_set) >= min_tag:
if len(common_tag_set) == len(old_tag_set):
cand_mem[j].append(city_name_list[i])
else:
new_mem_list = cand_mem[j][:]
new_mem_list.append(city_name_list[i])
new_list_mem.append(new_mem_list)
new_list_tag.append(common_tag_set)
#cand_mem.append(new_mem_list)
#cand_tag.append(common_tag_set)
cand_mem.extend(new_list_mem)
cand_tag.extend(new_list_tag)
# add itself to the candidate list
new_list = [city_name_list[i]]
new_tag = tag_list[i].copy()
#print "NEW", new_list, new_tag
if len(new_tag) >= min_tag:
cand_mem.append(new_list)
cand_tag.append(new_tag)
#for i in range(len(cand_mem)):
#print cand_mem[i], cand_tag[i]
#print '--------------------------------'
#for i in range(len(cand_mem)):
# print "CANDIDATE:", cand_mem[i], cand_tag[i]
my_dict = {}
for i in range(len(cand_mem)):
if len(cand_mem[i]) == 1:
continue
cand_mem[i] = list(set(cand_mem[i]))
cand_mem[i].sort()
#correct_key = ','.join([item for item in cand_mem[i]])
mem_frozen = frozenset(list(cand_tag[i]))
if mem_frozen in my_dict:
if len(cand_mem[i]) > len(my_dict[mem_frozen]):
my_dict[mem_frozen] = cand_mem[i]
else:
my_dict[mem_frozen] = cand_mem[i]
result = []
for key, value in my_dict.iteritems():
correct_key = list(key)
correct_key.sort()
correct_part = ','.join([item for item in correct_key])
correct_value = ','.join([item for item in value])
# merge
# (key:correct_part, len(tag))
result.append((len(correct_key), ':'.join([correct_value, correct_part])))
sorted_result = sorted(result, key = lambda x: (-x[0], x[1]))
for item in sorted_result:
print item[1]
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <deque>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <queue>
#include <stack>
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i <= b; i++)
#pragma comment(linker, "/STACK:16777216")
typedef long long int64;
typedef unsigned long long uint64;
using namespace std;
double PI = 3.14159265359;
double torad(double x)
{
return x * PI / 180.0;
}
double distance_between(double lat1, double long1, double lat2, double long2) {
double EARTH_RADIUS = 6371.0;//in km
double point1_lat_in_radians = torad(lat1);
double point2_lat_in_radians = torad(lat2);
double point1_long_in_radians = torad(long1);
double point2_long_in_radians = torad(long2);
double dist = acos( sin( point1_lat_in_radians ) * sin( point2_lat_in_radians ) +
cos( point1_lat_in_radians ) * cos( point2_lat_in_radians ) *
cos( point2_long_in_radians - point1_long_in_radians) ) * EARTH_RADIUS;
return round(dist * 100.0) / 100.0;
}
int id[201];
double a_lat[201], a_long[201];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int n, m;
cin>>n;
for (int i = 0; i < n; i++) cin>>id[i]>>a_lat[i]>>a_long[i];
cin>>m;
while (m--)
{
double s_lat, s_long;
string method;
int minute;
cin>>s_lat>>s_long>>method>>minute;
double time_limit = (double)minute / 60.0;
double velo;
if (method == "foot") velo = 5.0;
if (method == "bike") velo = 15.0;
if (method == "metro") velo = 20.0;
vector< pair<double, int> > result;
for (int i = 0; i < n; i++)
{
double cur_dist = distance_between(a_lat[i], a_long[i], s_lat, s_long);
double time_taken = cur_dist / velo;
if (time_limit - time_taken >= 0) result.PB(MP(cur_dist, id[i]));
}
sort(result.begin(), result.end());
for (int i = 0; i < result.size(); i++) cout<<result[i].second<<" ";
cout<<endl;
}
return 0;
}
n = int(raw_input())
input_pattern = []
for i in range(n):
ss = raw_input()
input_pattern.append((ss, ss.lower()))
input_string = raw_input().lower()
result = []
# loop thru each pattern
for item in input_pattern:
#print item[0], item[1]
if item[1] in input_string:
result.append(item[0])
result.sort()
#print result
for item in result:
print item
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <deque>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <queue>
#include <stack>
#define PB push_back
#define MP make_pair
#define rep(i,a,b) for (int i = a; i <= b; i++)
#pragma comment(linker, "/STACK:16777216")
typedef long long int64;
typedef unsigned long long uint64;
using namespace std;
double s[10][10005];
pair< int, double> info[10][105];
int sz[10];
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
memset(sz, 0, sizeof(sz));
int n, budget;
cin>>n>>budget;
for (int i = 0; i < n; i++)
for (int j = 0; j < budget; j++) s[i][j] = 0.0;
for (int i = 0; i < n; i++)
{
cin>>sz[i];
for (int j = 0; j < sz[i]; j++)
{
int price; double score;
cin>>price>>score;
info[i][j] = MP(price, score);
}
}
// begin processing
set<int> set_of_price;
// get initial value
for (int i = 0; i < sz[0]; i++)
{
int price = info[0][i].first;
double score = info[0][i].second;
if (price <= budget)
{
s[0][price] = max(s[0][price], score);
set_of_price.insert(price);
}
}
for (int i = 1; i < n; i++)
{
// each CITY
set<int> new_set_of_price;
new_set_of_price.clear();
for (int j = 0; j < sz[i]; j++)
{
int price = info[i][j].first;
double score = info[i][j].second;
// for each item, PICK that item and see if its optimal
set<int>::iterator it;
for (it = set_of_price.begin(); it != set_of_price.end(); it++)
{
int base_price = *it;
int new_price = base_price + price;
if (new_price <= budget)
{
new_set_of_price.insert(new_price);
s[i][new_price] = max(s[i][new_price], s[i-1][base_price] + score);
}
}
}
set_of_price = new_set_of_price;
}
double max_score = -1.0;
set<int>::iterator it;
for (it = set_of_price.begin(); it != set_of_price.end(); it++)
{
max_score = max(max_score, s[n - 1][*it]);
}
if (max_score == -1.0) printf("-1\n");
else printf("%.2f\n", max_score);
return 0;
}
以上是关于c_cpp [HackerRank]预订Hackathon 2015的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 从已排序的链表中删除重复值节点 - Hackerrank
c_cpp Hackerrank的“乐高积木”问题的动态编程/递归解决方案。
c_cpp 总数没有。 A和B之间的2的补码表示中的1,包括在内。 Hackerrank的“2补”问题。
c_cpp https://www.hackerrank.com/contests/worldcodesprint/challenges/two-pluses/copy-from/1301129656