[Offer收割]编程练习赛39
公平分队
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII; int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx < dy ? - : ;
#undef datatype
} int a[]; int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(), cin.tie();
int n;
cin >> n;
for (int i = ; i < * n; i++) cin >> a[i];
sort(a, a + * n);
lint ans = ;
for (int i = ; i < n; i++) ans += a[i];
ans += a[ * n - ];
cout << ans << endl;
return ;
}
XY游戏
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII; int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx < dy ? - : ;
#undef datatype
} map<int, int> mp;
struct state {
int a[][];
int step;
};
queue<state> q;
int hash_state(state st) {
int rtn = ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++)
rtn = rtn * + st.a[i][j];
}
return rtn;
}
const int dx[] = {-, , , };
const int dy[] = {, , , -};
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(), cin.tie();
state initial, st, stt;
char ch;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
cin >> ch;
while (ch != 'O' && ch != 'X' && ch != 'Y') cin >> ch;
if (ch == 'O') initial.a[i][j] = ;
else if (ch == 'X') initial.a[i][j] = ;
else initial.a[i][j] = ;
}
}
initial.step = ;
q.push(initial);
mp[hash_state(initial)]++;
while (!q.empty()) {
st = q.front();
q.pop();
bool ok = false;
for (int i = ; i < ; i++) {
if (st.a[i][] == && st.a[i][] == && st.a[i][] == && st.a[i][] == ) ok = true;
if (st.a[i][] == && st.a[i][] == && st.a[i][] == && st.a[i][] == ) ok = true;
if (st.a[][i] == && st.a[][i] == && st.a[][i] == && st.a[][i] == ) ok = true;
if (st.a[][i] == && st.a[][i] == && st.a[][i] == && st.a[][i] == ) ok = true;
if (st.a[][] == && st.a[][] == && st.a[][] == && st.a[][] == ) ok = true;
if (st.a[][] == && st.a[][] == && st.a[][] == && st.a[][] == ) ok = true;
if (st.a[][] == && st.a[][] == && st.a[][] == && st.a[][] == ) ok = true;
if (st.a[][] == && st.a[][] == && st.a[][] == && st.a[][] == ) ok = true;
}
if (ok) {
cout << st.step << endl;
return ;
}
stt = st;
stt.step = st.step + ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if (st.a[i][j] == ) continue;
for (int k = ; k < ; k++) {
if ( <= i + dx[k] && i + dx[k] < && <= j + dy[k] && j + dy[k] < && st.a[i + dx[k]][j + dy[k]] == ) {
stt.a[i + dx[k]][j + dy[k]] = stt.a[i][j];
stt.a[i][j] = ;
int h = hash_state(stt);
if (mp.find(h) == mp.end()) {
mp[h]++;
q.push(stt);
}
stt.a[i][j] = stt.a[i + dx[k]][j + dy[k]];
stt.a[i + dx[k]][j + dy[k]] = ;
}
}
}
}
}
cout << - << endl;
return ;
}
第K小最简真分数
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<functional>
//#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<int> VI;
typedef pair<int, int> PII; int cmp(const void * x, const void * y) {
#define datatype int
datatype dx = *((datatype *)(x)), dy = *((datatype *)(y));
//x < y
return dx < dy ? - : ;
#undef datatype
}
lint p[];
int m = ;
lint calc(lint x) {
lint rtn = x;
for (int i = ; i < ( << m); i++) {
int cnt = , mlt = ;
for (int j = ; j < m; j++) {
if (i & ( << j)) {
cnt++;
mlt *= p[j];
}
}
if (cnt & ) rtn -= x / mlt;
else rtn += x / mlt;
}
return rtn;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
std::ios::sync_with_stdio(), cin.tie();
lint n, k;
cin >> n >> k;
lint l = , r = n;
for (int i = ; i * i <= n; i++) {
if (n % i == ) {
p[m++] = i;
while (n % i == ) n /= i;
}
}
if (n != ) p[m++] = n;
while (l < r) {
lint mid = (l + r) >> ;
lint fk = calc(mid);
if (fk < k) l = mid + ;
else r = mid;
}
cout << r << endl;
return ;
}
前缀后缀查询

#include <bits/stdc++.h> using namespace std;
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define SZ(x) x.size()
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define inf 1000000007
#define mod 1000000007
#define ll long long
#define N 100010 template<class T,class U> void Max(T &a,U b){if(a<b)a=b;}
template<class T,class U> void Min(T &a,U b){if(a>b)a=b;}
template<class T,class U> void add(T &a,U b){a+=b;if(a>=mod)a-=mod;} template<class T,class U> int min(T a,U b){return a>=b?b:a;} int a[],b[],sz;
char s[];
int ch[][];
void add(char s[],int a[]){
int n=strlen(s),p=;
rep(i,,n){
int j=s[i]-'a';
if(!ch[p][j])ch[p][j]=++sz;
p=ch[p][j];
a[i+]=p;
}
}
int find(string s){
int p=;
rep(i,,SZ(s)){
int j=s[i]-'a';
if(!ch[p][j])return -;
p=ch[p][j];
}
return p;
}
int main(){
int i,j,k,ca=,T,n,m,K;
scanf("%d%d",&n,&K);
unordered_map<ll,int>g;
int w;sz=;
rep(i,,n){
scanf("%s%d",s,&w);
m=strlen(s);
add(s,a);
reverse(s,s+m);
add(s,b);
rep(j,,m+){
rep(k,,m+){
ll x=a[j]*1000007LL+b[k];
if(!g.count(x))g[x]=w;
else if(w>g[x])g[x]=w;
}
}
}
string s,t;
while(K--){
cin>>s>>t;
reverse(all(t));
int ans=-;
int x=find(s),y=find(t);
if(x!=-&&y!=-){
ll w=x*1000007LL+y;
if(g.count(w))ans=g[w];
}
printf("%d\n",ans);
}
}
[Offer收割]编程练习赛39的更多相关文章
- HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)
描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN},其中第i个单词Wi有具有一个权值Vi. 现在小Hi要进行M次查询,每次查询包含一个前缀字符串Pi和一个后缀字符串Si.他希望 ...
- HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)
描述 给一个整数N,请你求出以N为分母的最简(既约)真分数中第K小的是多少? 输入 两个整数N个K. 对于30%的数据,1 <= N <= 1000000 对于100%的数据,1 < ...
- HihoCoder1654: XY游戏([Offer收割]编程练习赛39)(好久没写搜索)(已经超级简短了)
描述 如下图所示,在4x4的棋盘上有X和Y两种棋子各若干枚:O表示空格. OXXY YOOX XOOY XOXX 小Hi每次可以选择任意一枚棋子,将它移动到上下左右相邻的空格中. 小Hi想知道最少移动 ...
- HihoCoder1653 : 公平分队([Offer收割]编程练习赛39)(贪心)
描述 小Hi和小Ho在玩一个战争游戏.游戏中2N个战斗单位,其中第i个单位的战斗力是Ai. 现在小Hi和小Ho要各选N个单位组成队伍,当然他们都希望自己队伍的总战斗力越大越好. 为了使分队更加公平,经 ...
- hihocoder [Offer收割]编程练习赛4
描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...
- hihocoder [Offer收割]编程练习赛61
[Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...
- [Offer收割]编程练习赛46
[Offer收割]编程练习赛46赛后题解 A.AEIOU 分析
- ACM学习历程—Hihocoder [Offer收割]编程练习赛1
比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...
- HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)
描述 H国编程联赛中有N只队伍,编号1~N. 他们计划在2018年一共进行M场一(队)对一(队)的比赛. 为了让参赛队员能得到充分的休息,联赛组委会决定:每支队伍连续两场比赛之间至少间隔一天.也就是如 ...
随机推荐
- js 验证文件格式和大小
<script> $('#btnSearch').click(function(){ // alert("000");// fileElem = document.ge ...
- 自定义View实现拖动小圆球,并随机改变其颜色
//简单实现package com.example.demo1; import android.content.Context;import android.graphics.Canvas;impor ...
- Python实现ATM+购物商城
需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还,按欠 ...
- 如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......
如果说需要注册数据中心,这样才能使用demo部署数据中心license证需要申请,使用云之间-工作流程......
- [luogu2319 HNOI2006] 超级英雄 (匈牙利算法)
传送门 Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选 ...
- 单元测试,我在公司Web团队的分享
一.单元测试的意义 1.质量 2.效率 (短期和长远都值得)写单元测试代码,总的来说其实是更节省开发时间,更保证质量的.Controller.Service.Dao其实都可以进行测试. 通过启动 To ...
- 从0到1发布一个Vue Collapse组件
需求背景 最近在项目中遇到了一个类似Collapse的交互需求,因此到github上找了一圈关于Vue Collapse的相关轮子,但是多少都有些问题.有的是实现问题,例如vue2-collapse, ...
- jquery源码分析(二)——架构设计
要学习一个库首先的理清它整体架构: 1.jQuery源码大致架构如下:(基于 jQuery 1.11 版本,共计8829行源码)(21,94) 定义了一些变量和函数jQu ...
- (18)使用模板(thymeleaf-freemarker)【从零开始学Spring Boot】
整体步骤: (1) 在pom.xml中引入thymeleaf; (2) 如何关闭thymeleaf缓存 (3) 编写模板文件.html ...
- orcale 日期显示格式化
SQL> select * 2 from emp 3 where hiredate='1987-11-17'; where hiredate='1987-11-17' * 第 3 行出现错误: ...