A Palindromic Twist(字符串)

  问每个字母必须向左或向右变成另一个字母,问能不能构成回文

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i=a; i<b; i++);
//#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6 + ;
const int maxn = 5e5+; int len;
char str[]; int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif int T; cin >> T;
while(T--)
{
scanf("%d", &len);
scanf("%s", str+);
if (len == )
{
printf("YES");
continue;
} int mid;
int l, r, tmp;
bool ok = true;
mid = len/;
for ( int i = ; i <= mid; i++ )
{
l = i; r = len-i+;
tmp = abs(str[l]-str[r]);
if (tmp > || tmp == )
{
//cout << tmp << endl;
ok = false;
break;
}
}
if (ok)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

  

  B Numbers on the Chessboard(模拟)

  题意简单,写起来容易错,主要是将矩阵每两层作为一大层(+N),然后判断所在点是前面有几层

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i=a; i<b; i++);
//#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6 + ;
const int maxn = 5e5+; LL n, q; int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif cin >> n >> q;
int x, y;
while(q--)
{
LL ans = ;
LL tmp;
scanf("%d %d", &x, &y);
if ((x+y)%==) {
int h = x%;
tmp = h ? x/ : (x-)/;
if (h) {
ans = n*tmp + (y/)+;
}
else {
ans = n*tmp + (n/)+(y/);
if (n%) ans++;
}
}
else {
int h = x%;
tmp = h ? x/ : (x-)/;
if (h) {
ans = n*tmp + (y/);
}
else {
ans = n*tmp + (n/) + (y+)/;
}
if (n%)
ans += (n*n)/+;
else
ans += (n*n)/;
}
printf("%lld\n", ans);
}
return ;
}

  

  C Minimum Value Rectangle 

  很容易推出最后结果和(a/b)+(b/a)有关,根据均值不等式肯定两个值越近越小,所以把可行边排序后每次选择枚举距离最相近的两种边选最小

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i=a; i<b; i++)
//#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6+;
const int maxn = 1e6+; int n, cnt;
int a[maxn];
int num[maxn]; void init()
{
cin >> n;
cnt = ; int tmp;
foe(i, , n)
scanf("%d", &a[i]);
sort(a+, a++n); tmp = ;
foe(i, , n)
{
if (a[i] == a[i-]) {
if (!tmp) {
num[++cnt] = a[i];
tmp++;
}
else {
tmp = ;
}
}
else {
tmp = ;
}
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input_2.txt", "r", stdin);
#endif int T; cin >> T;
while(T--)
{
init(); double tmp, ans = inf;
int a, b;
//cout << cnt << endl;
fo(i, , cnt)
{
tmp = (double)*num[i]/num[i+] + (double)*num[i+]/num[i];
if ( tmp < ans )
{
ans = tmp;
a = i; b = i+;
}
}
printf("%d %d %d %d\n", num[a], num[a], num[b], num[b]);
}
return ;
}

  

  D Mouse Hunt

  有只耗子在n个房子内乱串,当此时刻耗子在i房间时,下一时刻耗子会移动到a[i]号房间,在i房间布置捕鼠器需要耗费c[i],开始耗子位置随机,问最少消耗多少能保证一定捕捉到老鼠

  根据题意我们需要判断连通量和环,因为耗子顺着一条路走下去最后必然在某一个点后走到原先的某点形成一个环,所以用并查集判环,在该环上寻找最小cost就行了

 #include <iostream>
#include <string.h>
#include <cstdio>
#include <vector>
#include <stack>
#include <math.h>
#include <string>
#include <algorithm>
#include <time.h> #define SIGMA_SIZE 26
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) (x&-x)
#define foe(i, a, b) for(int i=a; i<=b; i++)
#define fo(i, a, b) for(int i=a; i<b; i++)
//#pragma warning ( disable : 4996 ) using namespace std;
typedef long long LL;
inline LL LMax(LL a, LL b) { return a>b ? a : b; }
inline LL LMin(LL a, LL b) { return a>b ? b : a; }
inline LL lgcd(LL a, LL b) { return b == ? a : lgcd(b, a%b); }
inline LL llcm(LL a, LL b) { return a / lgcd(a, b)*b; } //a*b = gcd*lcm
inline int Max(int a, int b) { return a>b ? a : b; }
inline int Min(int a, int b) { return a>b ? b : a; }
inline int gcd(int a, int b) { return b == ? a : gcd(b, a%b); }
inline int lcm(int a, int b) { return a / gcd(a, b)*b; } //a*b = gcd*lcm
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
const int maxk = 1e6+;
const int maxn = 2e5+; int n;
int cost[maxn], to[maxn];
int fa[maxn];
bool vis[maxn];
vector<int> ans; int _find(int x)
{
if (x == fa[x])
return fa[x];
return fa[x] = _find(fa[x]);
} //将y作为父节点
void merge(int x, int y)
{
int xr = _find(x);
int yr = _find(y);
if (xr != yr)
fa[xr] = y;
} int dfs(int x, int y)
{
if (x == y) return cost[x];
return Min(dfs(x, to[y]), cost[y]);
} void init()
{
cin >> n; foe(i, , n) scanf("%d", &cost[i]);
foe(i, , n)
{
scanf("%d", &to[i]);
fa[i] = i;
vis[i] = false;
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif init();
//并查集判环
foe(i, , n) {
if (_find(to[i]) == _find(i)) {
vis[i] = true;
continue;
} merge(i, to[i]);
} int ans = ;
//foe(i, 1, n)
// printf("%d\n", vis[i]);
foe(i, , n)
if (vis[i])
ans += dfs(i, to[i]);
printf("%d\n", ans);
return ;
}

Educational Codeforces Round49的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. 笨办法学Python记录--习题15-17 开始读写文件啦

    习题15 - 17 打开并读流程: from sys import argv script,filename = argv txt = open(filename) print "Here' ...

  2. vue+webpack项目中px2rem的例子

    项目环境搭建此处省略,不再赘述,需要的请查阅相关资料. 本篇只介绍,如果在vue+webpack的项目中配置,把px自动转为rem 第一步安装   npm install   postcss-px2r ...

  3. Funcation:Object

    ylbtech-Funcation:Object 1.返回顶部 1. public object GetMyRelation() { var list = new List<object> ...

  4. 本地 win7 与虚拟机Centos7 ping互通和Centos7 上网设置

    VM 12 安装虚拟机我就不表示了 很简单网上找找 一 .虚拟机设置 1.修改使用了 VMWare 12 虚拟机,Oracle VM 用得有点晕 2.配置VM 的NET环境 3.在VMware虚拟机任 ...

  5. 使用R语言 SDK调取tushare数据

    安装Tushare 打开RStudio,在控制台输入命令: > install.packages('Tushare') Tushare的R包需要依赖httr.tidyverse.forecast ...

  6. Python匹马行天下之python之父

    龟叔和他的python 经过了漫长的旅程,终于要看到主角Python了.Python是现在非常非常流行的编程语言,在我们能看到的大部分编程语言排行榜中,Python都能在前三甲中拥有一席之地 ,并且发 ...

  7. Codeforces Round #526 D - The Fair Nut and the Best Path /// 树上两点间路径花费

    题目大意: 给定一棵树 树上每个点有对应的点权 树上每条边有对应的边权 经过一个点可得到点权 经过一条边必须花费边权 即从u到v 最终得分=u的点权-u到v的边权+v的点权 求树上一条路径使得得分最大 ...

  8. 织梦自增函数[field:global name=autoindex/]常见用法

    看来不少朋友需要不了解这个自增函数的用法,在这里我列举一些常见的写法以及作用.   [field:global name=autoindex/] !--普通打印递增的数字-- [field:globa ...

  9. c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)

    一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...

  10. JS对象 String 字符串对象定义字符串的方法就是直接赋值。比如: var mystr = "I love JavaScript!"

    String 字符串对象 在之前的学习中已经使用字符串对象了,定义字符串的方法就是直接赋值.比如: var mystr = "I love JavaScript!" 定义mystr ...