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. Mac OS 快速查询技巧

    今天不知道按到什么快捷键弹出了这个搜索框,试了试很强大 后来查了才知道要双击键盘上的key (⌘)就会弹出,这个搜索很强大,要先安装QQ,QQ自带的 版权声明:本文为博主原创文章,未经博主允许不得转载 ...

  2. LeetCode 445. Add Two Numbers II (两数相加 II)

    题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字 ...

  3. jvm-多线程

    多线程的目的 为什么要使用多线程?可以简单的分两个方面来说: 在多个cpu核心下,多线程的好处是显而易见的,不然多个cpu核心只跑一个线程其他的核心就都浪费了: 即便不考虑多核心,在单核下,多线程也是 ...

  4. 导出lab动态命名文件夹文件名

    set tmp=%date:~0,10%echo %tmp:/=-%mkdir %tmp:/=-%cd %tmp:/=-%exp meet/meet@orcl_11 file=%tmp:/=-%.dm ...

  5. 拾遗:Git 基础

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ 一.参数配置 $ git con ...

  6. DLL和OCX注册

    在注册DLL或者OCX的方法应该使用regsvr32.exe,使用得多了一定会觉得在cmd运行中写一长串东西很烦人吧!这里向大家介绍一种麻烦一次方便一生的方法.这个方法只要右击你想注册或者反注册的DL ...

  7. 使用PyCharm创建Django项目及基本配置

    https://segmentfault.com/a/1190000011576316 pycharm是个很不错的python开发工具,大大缩短了python项目的创建时间以及调试时间在使用pytho ...

  8. 配置Spring Security 错误:Property or field 'ROLE_USER' cannot be found

    在学习http://www.mkyong.com/spring-security/spring-security-hello-world-example/时,出现以下错误: Property or f ...

  9. C 自己实现strcpy,strcmp,strlen,strcat等函数

    // mystrlen() 测试字符长度方法 int mystrlen(char *str) { int cnt = 0; char *p= str; while(*p++ != '\0') { cn ...

  10. redis config

    网络上抄袭过来的 然后顺序自己做点笔记 http://chembo.iteye.com/blog/2054021 这里有各个key 详细的描述 # redis 配置文件示例 # 当你需要为某个配置项指 ...