公平分队

#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的更多相关文章

  1. HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)

    描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN},其中第i个单词Wi有具有一个权值Vi. 现在小Hi要进行M次查询,每次查询包含一个前缀字符串Pi和一个后缀字符串Si.他希望 ...

  2. HihoCoder1655 : 第K小最简真分数([Offer收割]编程练习赛39)(唯一分解+容斥定理+二分)(不错的数学题)

    描述 给一个整数N,请你求出以N为分母的最简(既约)真分数中第K小的是多少? 输入 两个整数N个K. 对于30%的数据,1 <= N <= 1000000 对于100%的数据,1 < ...

  3. HihoCoder1654: XY游戏([Offer收割]编程练习赛39)(好久没写搜索)(已经超级简短了)

    描述 如下图所示,在4x4的棋盘上有X和Y两种棋子各若干枚:O表示空格. OXXY YOOX XOOY XOXX 小Hi每次可以选择任意一枚棋子,将它移动到上下左右相邻的空格中. 小Hi想知道最少移动 ...

  4. HihoCoder1653 : 公平分队([Offer收割]编程练习赛39)(贪心)

    描述 小Hi和小Ho在玩一个战争游戏.游戏中2N个战斗单位,其中第i个单位的战斗力是Ai. 现在小Hi和小Ho要各选N个单位组成队伍,当然他们都希望自己队伍的总战斗力越大越好. 为了使分队更加公平,经 ...

  5. hihocoder [Offer收割]编程练习赛4

    描述 最近天气炎热,小Ho天天宅在家里叫外卖.他常吃的一家餐馆一共有N道菜品,价格分别是A1, A2, ... AN元.并且如果消费总计满X元,还能享受优惠.小Ho是一个不薅羊毛不舒服斯基的人,他希望 ...

  6. hihocoder [Offer收割]编程练习赛61

    [Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...

  7. [Offer收割]编程练习赛46

    [Offer收割]编程练习赛46赛后题解 A.AEIOU 分析

  8. ACM学习历程—Hihocoder [Offer收割]编程练习赛1

    比赛链接:http://hihocoder.com/contest/hihointerview3/problem/1 大概有一个月没怎么打算法了.这一场的前一场BC,也打的不是很好.本来Div1的A和 ...

  9. HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)

    描述 H国编程联赛中有N只队伍,编号1~N. 他们计划在2018年一共进行M场一(队)对一(队)的比赛. 为了让参赛队员能得到充分的休息,联赛组委会决定:每支队伍连续两场比赛之间至少间隔一天.也就是如 ...

随机推荐

  1. [NOI2015]软件包管理器 树链剖分_线段树

    没有太大难度,刷水有益健康 Code: // luogu-judger-enable-o2 #include <bits/stdc++.h> #define setIO(s) freope ...

  2. 在阿里云的ubuntu服务器上安装xampp时出现unable to realloc unable to realloc 8380000 bytes错误

    在阿里云的ubuntu服务器上安装xampp时出现unable to realloc unable to realloc 8380000 bytes错误 解决:增加Swap空间(阿里云缺省没有分配任何 ...

  3. (C/C++学习)3.C++中cin的成员函数(cin.get();cin.getine()……)

    说明:流输入运算符,在一定程度上为C++程序的开发提供了很多便利,我们可以避免C语言那种繁琐的输入格式,比如在输入一个数值时,还需指定其格式,而cin以及cout则不需要.但是cin也有一些缺陷,比如 ...

  4. 重置root管理员密码(RedHat、CentOS、Fedora)

    重启Linux系统主机并出现引导画面时,按下键盘上的e键进入内核编辑界面. 在Linux16参数这行后面追加"rd.break"参数,然后按下Ctrl+x组合键来运行修改后的内核程 ...

  5. 在Win32 Application 环境下实现MFC窗口的创建

    // Win32下MFC.cpp : Defines the entry point for the application.// #include "stdafx.h" clas ...

  6. Spring Cloud-hystrix Dashboard(八)

    单机模式 1.创建一个dashboard项目 2.引入依赖 <!--histrix依赖--> <dependency> <groupId>org.springfra ...

  7. MySQL 索引分析

    MySQL复合唯一索引分析 关于复合唯一索引(unique key 或 unique index),网上搜索不少人说:"这种索引起到的关键作用是约束,查询时性能上没有得到提高或者查询时根本没 ...

  8. mongodb--分片架构【待填的坑】

    首先有一个问题没有搞懂:什么是自动分片?用脚本吗? 一: 多机方式中的另一种方式[分片 => sharding] 分片的对象的谁? 对一个[集合 => 表]进行拆分,把一个大数据拆分成多个 ...

  9. Spring中获取Session的方法汇总

    Spring: web.xml <listener> <listener-class>org.springframework.web.context.request.Reque ...

  10. 关于JOS 未对全部内存分页映射之前 物理地址映射问题的思考

    在kern/pmap.c 里面会又以下这段代码,要知道boot_alloc只会分配线性地址,真正建立虚拟页和物理页映射关系的在后面的page_alloc. ////////////////////// ...