TOPCODER


  • 首页

  • 标签

  • 分类

  • 归档

  • 搜索

Why 2022 mattered

发表于 2022-12-28 | 分类于 The Economist

Some years bring disorder, others a resolution. This one asked questions
Dec 20th 2022

IT WAS A year that put the world to the test. From the invasion of Ukraine to covid-19 in China, from inflation to climate change, from Sino-American tensions to pivotal elections, 2022 asked hard questions. The ordeal has not only sent the world in a new direction, but also shown it in a new light.

inflation
n. 膨胀;通货膨胀;充气
pivotal
adj. 关键性的, 核心的
ordeal
n. 折磨, 磨难, 煎熬, 严酷的考验

这是对世界进行考验的一年。从乌克兰入侵到中国新冠肺炎疫情,从通货膨胀到气候变化,从中美紧张关系到关键的选举,2022年提出了尖锐的问题。这场严峻的考验不仅使世界走向了新的方向,而且使世界呈现出新的面貌。

The biggest surprise—and the most welcome—has been the resilience of broadly liberal countries in the West. When Vladimir Putin ordered Russian troops into Ukraine on February 24th, he expected the government of a corrupt state to buckle. After a humiliating withdrawal from Afghanistan in 2021, the decadent, divided West would surely fail to match condemnation of Russia with real backing for Ukraine.

resilience
n. 适应力, 弹力, 快速恢复的能力, 还原能力
troop
n. 部队,军队;一群,一队
v. 群集,结队,成群前行
buckle
v. (使)搭扣扣住, (被)压垮, 双腿发软
n. (皮带等的)搭扣
humiliating
adj. 献丑的
v. “humiliate”的现在分词
withdrawal
n. 撤回, 取款, 收回, 撤走
decadent
adj. 堕落的, 颓废的, 贪图享乐的
n. 颓废派艺术家[文人]
condemnation
n. 谴责, 指责

最大的惊喜——也是最受欢迎的——是西方广泛自由主义国家的韧性。当弗拉基米尔·普京在2月24日下令俄罗斯军队进入乌克兰时,他以为这个腐败的国家会屈服。在2021年耻辱地从阿富汗撤军后,颓废、分裂的西方肯定无法将对俄罗斯的谴责与对乌克兰的真正支持相提并论。

In fact Volodymyr Zelensky and his people affirmed that self-determination and liberty are worth dying for. They became an inspiration. After an upsurge in popular support, Western governments threw their weight behind democracy’s new champion. Led by the Biden administration, the West is providing arms and aid on a scale even hawks had not imagined.

upsurge
n. 急剧上升, 飙升, 猛增
v. 高涨, 增长
scale
n. 规模;刻度;级别;比例尺;鳞;(scales) 秤,磅秤
v. 改变…的尺寸大小;攀登

事实上,弗拉基米尔·泽伦斯基和他的人民肯定,自决和自由值得为之牺牲。他们成为了一种灵感。在民众支持率高涨之后,西方政府纷纷支持这位民主的新冠军。在拜登政府的领导下,西方正在提供武器和援助,规模之大甚至连鹰派都无法想象。

At home voters also made themselves heard, siding against taboo-busting populists. In America, despite the awful approval numbers of Joe Biden, centrists used their ballots to preserve fundamental rights, including in some states the right to an abortion after the Supreme Court overturned Roe v Wade. In competitive races hard-core election-deniers endorsed by Donald Trump almost all lost.

populist
n. 平民主义者, 平民论者;人民党党员
adj. 平民主义的;民粹主义的
despite
prep. 尽管,虽然
n. 轻视,轻蔑(旧用)
awful
adj. 可怕的,骇人的;难过的;极坏的,糟糕的,极讨厌的;非常的,极大的;使人敬畏的,庄严的
adv. 极其,非常
centrist
n. (政治上的)中间派
overturn
v. 倾覆, 倾倒, 翻掉, 推翻
n. 毁灭, 推翻, 垮台
endorse
v. 支持, (尤指在支票背面)签名,背书, (在广告中)宣传, (在驾驶执照上)记录违章事项

在国内,选民们也发表了自己的意见,反对打破禁忌的民粹主义者。在美国,尽管乔·拜登(Joe Biden)的支持率很高,但中间派利用选票来维护基本权利,包括在最高法院推翻罗诉韦德案(Roe v Wade)后的一些州堕胎权。在竞争激烈的竞选中,唐纳德·特朗普(Donald Trump)支持的铁杆选举否认者几乎都输了。

In France Marine Le Pen camouflaged her far-right origins, but was still beaten by Emmanuel Macron, a centrist. After Giorgia Meloni became Italy’s first far-right post-war prime minister, she leaned to the centre. Even in stumbling Britain, both Labour and the governing Conservatives are calculating that victory in elections lies away from the populist extremes of right and left.

camouflage
n. 隐蔽, (动物的)保护色, 隐瞒
v. 伪装, 掩饰
lean
v. 倾斜, 倚靠, 前俯(或后仰), 靠在
adj. 瘦且健康的;〔肉〕瘦的,脂肪少的;高效精干的;回报少的;贫乏的
n. 瘦肉
stumble
v. 绊倒;跌跌撞撞地走,蹒跚而行; 演奏;无意中发现;犯错,失策
n. 绊倒, 失足, 失误, 失败

在法国,马琳·勒庞掩饰了自己的极右翼出身,但仍被中间派埃马纽埃尔·马克龙击败。梅洛尼(Giorgia Meloni)成为意大利战后首位极右翼总理后,她倾向于中间立场。即使在步履蹒跚的英国,工党和执政的保守党都在盘算,选举的胜利不在于极右和极左的民粹主义。

As messy democracies show unexpected resolve, so seemingly steady autocracies have had feet of clay. Mr Putin is the prime example, doubling and redoubling his catastrophic gamble. But he is not the only one. After three months of protests following the death in custody of Mahsa Amini, who had been arrested for failing to follow the rules in wearing her hijab, the security forces in Iran have taken to shooting female protesters in the face, breasts and genitals. Now that the mullahs have forfeited the faith of their people, they have no other lever but violence.

messy
adj. 凌乱的, 难以应付的
autocracy
n. 专制制度,独裁政体,独裁国家,专制国家
clay
n. 黏土, 陶土
catastrophic
adj. 大突变(灾难)的, 悲惨结局的, 灾难性的
gamble
v. 冒风险;碰运气;以…为赌注
n. 赌博;打赌;冒险”
custody
n. 羁押, 保管, 保护, 【法】抚养权
hijab
n. 盖头, 头巾制度(规定戴头巾的宗教制度)
genital
adj. 生殖的, 生殖器的
n. 外阴部
mullah
n. 毛拉(穆斯林宗教和教法的教师)
forfeit
v. (因犯错)丧失
n. 罚金, 没收物
adj. 被罚的, 被没收的

当混乱的民主国家展现出出人意料的决心时,看似稳定的专制国家也有了问题。普京就是最好的例子,他的灾难性赌博一倍又一倍。但他不是唯一一个这样做的人。马赫萨·阿米尼(Mahsa Amini)因未遵守佩戴头巾的规定而被捕,在拘留期间死亡,此后抗议活动持续了三个月,伊朗安全部队开始对女性抗议者的面部、乳房和生殖器开枪。现在毛拉们已经丧失了人民的信仰,除了暴力,他们没有别的手段。

Those who admire strong leaders for getting things done should be careful what they wish for. Xi Jinping has extended the dominance of the Chinese Communist Party, installing himself as its permanent chief and the most powerful leader since Mao Zedong. But his steps to cool the property market, rein back consumer tech and block covid did grave harm to the economy. Today, as the virus spreads, it is clear that his government wasted months when it should have been vaccinating the elderly, stockpiling drugs and creating intensive-care beds.

rein
n. 控制, 缰绳, (幼儿佩带以防走失的)保护带, 主宰
v. 控制, 箝制, 抑制(情欲等), 给(马)套上缰绳
grave
n. 坟墓, 墓地, 墓穴, 死
adj. 重要的, 严重的, 〔意大利语〕【音乐】沉重的, 极慢的
v. 刻, 对(船底)作清洗并涂上沥青等涂料, 铭记
adv. 庄重地;严肃地

那些钦佩强有力的领导者把事情做好的人应该小心他们的愿望。习近平扩大了中国共产党的统治地位,将自己任命为永久领导人和自毛泽东以来最有权势的领导人。但他为房地产市场降温、控制消费科技和阻止新冠肺炎的措施对经济造成了严重损害。今天,随着病毒的传播,很明显,他的政府浪费了数月时间,而本应为老年人接种疫苗、储备药物和建造重症监护病床。

Even China’s all-encompassing social control showed cracks. Although the Chinese security services swatted down widespread protests last month, these had been triggered partly by the sight of maskless crowds in Qatar enjoying the World Cup.

encompass
v. 包含, 包围, 围绕, 围住
crack
v. (使)破裂,断裂;重击,猛击
n. 裂缝;缝隙;(突然的)爆裂声;笑话,俏皮话
adj. 训练有素的;技艺高超的;优秀的;一流的
swat
v. 拍
n. 〈美口〉拍, 猛击

甚至中国无所不包的社会控制也出现了裂痕。尽管中国安全部门上个月镇压了广泛的抗议活动,但这些抗议活动的部分原因是看到卡塔尔不戴口罩的人群欣赏世界杯。

For all those who embrace classical liberal values, including this newspaper, Western resilience is heartening—and an important change after a long retreat. But the good news goes only so far. The tests of 2022 have also revealed the depths of the world’s divisions and have set big government on the march.

hearten
v. 激励, 鼓励

对于包括本报在内的所有拥护古典自由主义价值观的人来说,西方的复原力令人振奋——这是长期撤退后的重要变化。但好消息仅限于此。2022年的测试也揭示了世界分裂的深度,并让大政府踏上了征程。

To gauge the divisions, compare the almost universal support for America after the attacks of September 11th 2001 with the global south’s determination to stay neutral in the fight over Ukraine. In the most recent UN vote to reprimand Russia, 35 countries abstained. Many understandably resent how the West asserts that its worries are issues of global principle, whereas war in Yemen or the Horn of Africa, say, or climate-related droughts and floods, always seem to be regional.

gauge
v. 估计, 判定, 估算, (用仪器)测量
n. 厚度, 标准, 测量仪器(或仪表), 计量器
neutral
adj. 中立的,中性的
A judge must remain neutral.
裁判必须要保持中立。
reprimand
v. 斥责, 训斥, 申斥
n. 惩戒, 申斥
understandably
adv. 可以理解地, 正常地, 合乎情理地

要衡量这些分歧,可以将2001年9月11日恐怖袭击后对美国几乎普遍的支持与全球南方在乌克兰战争中保持中立的决心进行比较。在联合国最近一次谴责俄罗斯的投票中,35个国家弃权。可以理解的是,许多人对西方声称其担忧是全球原则问题感到不满,而也门或非洲之角的战争,或与气候有关的干旱和洪水,似乎总是地区性的。

In much of the world liberal values are embattled. Despite the defeat of Jair Bolsonaro in Brazil, democracy is under strain in Latin America. As he presides over ruinous inflation in Turkey, Recep Tayyip Erdogan is prosecuting potential opponents in the election in 2023. In Israel Binyamin Netanyahu is trying to avoid jail for corruption by forming a coalition with the Arab-hating, gay-bashing far right. Indonesia adopted an illiberal criminal code in December that threatens to ban sex outside marriage, stifle free speech and impose religious orthodoxy. India’s economy is brimming over with tech-inspired enterprise, but its politics are majoritarian, ugly and cruel.

embattle
v. 布阵, 设防于, 在…造城垛
ruinous
adj. 毁灭性的;被毁坏的;负担不起的;贵得离谱的
prosecute
v. 起诉, 控告, 检举, 担任控方律师
coalition
n. 联盟, 联合, 结合, (两党或多党)联合政府
bashing
n. 猛烈抨击, 严厉批评, (对某人或群体的)殴打
v. “bash”的现在分词
illiberal
adj. 不容言论(或行动)自由的, 不开明的
stifle
v. 扼杀, 抑制, 压制, 阻止, 使窒息
n. (马,狗的)后腿膝关节(病)
orthodoxy
n. 正统观念, 普遍接受的观点, 正统的信仰(或做法), 正教会
brim
n. (东西的)边缘, 边; 帽檐
v. 满溢; 溢出; 充满[某种事物、品质或情感]
majoritarian
adj. 多数主义的
n. 多数主义者

All around the world, the idea of limited government is taking a beating. Because of the post-invasion energy shock, European governments are pouring money into fixing prices. They are also powering the transition from fossil fuels, itself a welcome goal, using industrial policy rather than markets. America’s answer to the security threat from China is to deploy trade barriers and subsidies to decouple its own economy and boost home-grown industries. If that harms America’s allies, too bad.

在世界各地,有限政府的理念正在遭受打击。由于美国入侵后的能源冲击,欧洲各国政府正投入大量资金来固定价格。它们还在利用产业政策而非市场,推动从化石燃料的转型,这本身就是一个受欢迎的目标。面对来自中国的安全威胁,美国的应对办法是设置贸易壁垒和补贴,让自己的经济脱钩,促进本土产业的发展。如果这损害了美国的盟友,那就太糟糕了。

Economic nationalism is popular. The largesse during the pandemic changed expectations of the state. Creative destruction, which reallocates capital and labour, may be unpalatable to ageing populations that put less store by economic growth and to younger voters who embrace the politics of identity.

largesse
n. 慷慨解囊, 施舍, 赠款
destruction
n. 破坏,毁灭
unpalatable
adj. 令人不快的, 难以接受的, 难吃的, 不可口的

经济民族主义盛行。大流行期间的慷慨改变了国家的期望。重新分配资本和劳动力的创造性破坏可能不适合不太重视经济增长的老龄化人口和拥护身份政治的年轻选民。

But big-government capitalism has a poor record. Given decades-high inflation, caused partly by ill-judged fiscal and monetary policy, especially in America, it is odd that voters want to reward politicians and officials by giving them power over bits of the economy they are not suited to run. State-backed champions in energy and tech sometimes succeed, but the more that countries pile in, the more waste and rent-seeking there will be.

suited
adj. 合适, 适宜, 适当, 般配的
v. “suit”的过去分词和过去式
pile
n. 一堆; 大量; 高大建筑; 建筑群; 电池组
v. 堆积; 拥, 挤

但大政府资本主义的记录并不好。考虑到几十年的高通货膨胀,部分原因是财政和货币政策的错误判断,尤其是在美国,选民们想要奖励政治家和官员,让他们在不适合他们管理的经济领域获得权力,这是很奇怪的。在能源和科技领域,政府支持的领军企业有时会成功,但国家越多,浪费和寻租就会越多。

The chips were down
Judged by the liberal yardstick of limited government, a respect for individual dignity and a faith in human progress, 2022 has been mixed. However, there is hope. The West was arrogant after the collapse of Soviet communism. It paid the price in Iraq, Afghanistan and the global financial crisis of 2007-09. In 2022, having been rocked by populism at home and China’s extraordinary rise, the West was challenged and it found its footing. ■

chip
n. 集成电路片,芯片;炸薯条,炸薯片;碎屑,碎片;缺口
v. 打破;弄缺;(使)掉碎片
n. [名]奇普
yardstick
n. 准绳, 码尺, (好坏或成败的)衡量标准
dignity
n. 尊贵, 高贵; 庄严, 端庄; 尊严
arrogant
adj. 傲慢的, 自大的
populism
n. 平民政治, 民粹主义, 民意论
extraordinary
adj. 意想不到的,不平常的;非凡的;特派的;临时的
footing
n. 基础,地位; 立足,立足点;平衡,站稳
v. 支付(账单或费用)(“foot”的现在分词)

筹码下降了
从有限政府的自由主义标准、对个人尊严的尊重和对人类进步的信念来看,2022 年喜忧参半。然而,还是有希望的。苏联共产主义垮台后,西方变得傲慢自大。它在伊拉克、阿富汗和 2007-09 年的全球金融危机中付出了代价。 2022 年,在国内民粹主义和中国非凡崛起的冲击下,西方受到挑战并站稳脚跟。■

1029 words

《算法竞赛入门经典》 第五章:C++与STL入门

发表于 2022-01-22 | 分类于 《算法竞赛入门经典》

5-2 UVa101 The Blocks Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;

const int MAXN = 30;
int n;
vector<int> pile[MAXN];

// 找木块a所在的pile和height,以引用的形式返回调用者
void find_block(int a, int& p, int& h) {
for (p = 0; p < n; p++) {
for (h = 0; h < pile[p].size(); h++) {
if (pile[p][h] == a) {
return;
}
}
}
}

// 把第p堆高度为h的木块上方的所有木块移回原位
void clear_above(int p, int h) {
for (int i = h + 1; i < pile[p].size(); i++) {
int b = pile[p][i];
pile[b].push_back(b);
}
pile[p].resize(h + 1);
}

// 把第p堆高度h及其上方的木块整体移动到p2堆的顶部
void pile_onto(int p, int h, int p2) {
for (int i = h; i < pile[p].size(); i++) {
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);
}

void print() {
for (int i = 0; i < n; i++) {
printf("%d:", i);
for (int j = 0; j < pile[i].size(); j++) {
printf(" %d", pile[i][j]);
}
printf("\n");
}
}

int main() {
int a, b;
cin >> n;
string s1, s2;
for (int i = 0; i < n; i++) {
pile[i].push_back(i);
}
while (cin >> s1 >> a >> s2 >> b) {
int pa, pb, ha, hb;
find_block(a, pa, ha);
find_block(b, pb, hb);
if (pa == pb) {
continue;
}
if (s2 == "onto") {
clear_above(pb, hb);
}
if (s1 == "move") {
clear_above(pa, ha);
}
pile_onto(pa, ha, pb);
}
print();

return 0;
}

5-3 UVa10815 Andy’s First Dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std;

set<string> dict;
string s, buf;

int main() {
while (cin >> s) {
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i])) {
s[i] = tolower(s[i]);
} else {
s[i] = ' ';
}
}
stringstream ss(s);
while (ss >> buf) {
dict.insert(buf);
}
}
for (set<string>::iterator it = dict.begin(); it != dict.end(); it++) {
cout << *it << "\n";
}

return 0;
}

5-4 UVa156 Ananagrams

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

map<string, int> cnt;
vector<string> words;

string repr(string s) {
string res = s;
for (int i = 0; i < res.length(); i++) {
res[i] = tolower(res[i]);
}
sort(res.begin(), res.end());
return res;
}

int main() {
int n = 0;
string s;
while (cin >> s) {
if (s[0] == '#') {
break;
}
words.push_back(s);
string res = repr(s);
if (!cnt.count(res)) {
cnt[res] = 0;
}
cnt[res]++;
}
vector<string> res;
for (int i = 0; i < words.size(); i++) {
if (cnt[repr(words[i])] == 1) {
res.push_back(words[i]);
}
}
sort(res.begin(), res.end());
for (int i = 0; i < res.size(); i++) {
cout << res[i] << "\n";
}

return 0;
}

5-5 UVa12096 The SetStack Computer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())

typedef set<int> Set;
map<Set,int> IDcache; // 把集合映射成ID
vector<Set> Setcache; // 根据ID取集合

// 查找给定集合x的ID。如果找不到,分配一个新ID
int ID (Set x) {
if (IDcache.count(x)) return IDcache[x];
Setcache.push_back(x); // 添加新集合
return IDcache[x] = Setcache.size() - 1;
}

int main () {
int T;
cin >> T;
while(T--) {
stack<int> s; // 题目中的栈
int n;
cin >> n;
for(int i = 0; i < n; i++) {
string op;
cin >> op;
if (op[0] == 'P') s.push(ID(Set()));
else if (op[0] == 'D') s.push(s.top());
else {
Set x1 = Setcache[s.top()]; s.pop();
Set x2 = Setcache[s.top()]; s.pop();
Set x;
if (op[0] == 'U') set_union (ALL(x1), ALL(x2), INS(x));
if (op[0] == 'I') set_intersection (ALL(x1), ALL(x2), INS(x));
if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
s.push(ID(x));
}
cout << Setcache[s.top()].size() << endl;
}
cout << "***" << endl;
}
return 0;
}

2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT

5-6 UVa540 Team Queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdio>
#include <map>
#include <queue>
using namespace std;

const int MAXT = 1000 + 10;

int main () {
int t, kase = 1;
while (scanf("%d", &t) == 1 && t) {
printf("Scenario #%d\n", kase++);

map<int, int> team;
for (int i = 0; i < t; i++) {
int n, x;
scanf("%d", &n);
while (n--) { scanf("%d", &x); team[x] = i; }
}

queue<int> q, q2[MAXT];
for ( ; ; ) {
int x;
char cmd[10];
scanf("%s", cmd);
if (cmd[0] == 'S') { break; }
else if (cmd[0] == 'E') {
scanf("%d", &x);
int t = team[x];
if (q2[t].empty()) q.push(t);
q2[t].push(x);
}
else if (cmd[0] == 'D') {
int t = q.front();
printf("%d\n", q2[t].front());
q2[t].pop();
if (q2[t].empty()) q.pop();
}
}
printf("\n");
}

return 0;
}

5-7 UVa136 Ugly Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// UVa136 Ugly Numbers
// Rujia Liu
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
const int coeff[3] = {2, 3, 5};

int main() {
priority_queue<LL, vector<LL>, greater<LL> > pq;
set<LL> s;
pq.push(1);
s.insert(1);
for(int i = 1; ; i++) {
LL x = pq.top(); pq.pop();
if(i == 1500) {
cout << "The 1500'th ugly number is " << x << ".\n";
break;
}
for(int j = 0; j < 3; j++) {
LL x2 = x * coeff[j];
if(!s.count(x2)) { s.insert(x2); pq.push(x2); }
}
}
return 0;
}

1 2
123412341

5-8 UVa400 Unix ls

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int MAXCOL = 60;
const int MAXN = 100 + 5;
string filenames[MAXN];

void print(const string& s, int len, char extra) {
cout << s;
for (int i = 0; i < len - s.length(); i++) {
cout << extra;
}
}

int main() {
int n;
while (cin >> n) {
int M = 0;
for (int i = 0; i < n; i++) {
cin >> filenames[i];
M = max(M, (int)filenames[i].length());
}
// 计算列数cols和行数rows
int cols = (MAXCOL - M) / (M + 2) + 1;
// (M+2)(M+2)......M,几个(M+2)加一
int rows = (n - 1) / cols + 1;
// 如果clos为3,n为12,那么结果为5,但其实只要4行就可以
// (n - 1) / cols 避免行列填满的情况导致结果多1
// +1是为了不够clos的部分也得给他们留一行
print("", 60, '-');
cout << "\n";
sort(filenames, filenames + n);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int idx = c * rows + r;
// 只能一行一行来,因为每一行需要一个回车
// 从0行开始,0行0列,0行1列,0行2列,所以一开始r是0
if (idx < n) print(filenames[idx], c == cols - 1 ? M : M + 2, ' ');
}
cout << "\n";
}
}

return 0;
}

5-9 UVa1592 Database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// UVa1592 Database
// Rujia Liu
// 本程序只是为了演示STL各种用法,效率较低。实践中一般用C字符串和哈希表来实现。




using namespace std;

typedef pair<int,int> PII;

const int maxr = 10000 + 5;
const int maxc = 10 + 5;

int m, n, db[maxr][maxc], cnt;

map<string, int> id;
int ID(const string& s) {
if(!id.count(s)) {
id[s] = ++cnt;
}
return id[s];
}

void find() {
for(int c1 = 0; c1 < m; c1++)
for(int c2 = c1+1; c2 < m; c2++) {
map<PII, int> d;
for(int i = 0; i < n; i++) {
PII p = make_pair(db[i][c1], db[i][c2]);
if(d.count(p)) {
printf("NO\n");
printf("%d %d\n", d[p]+1, i+1);
printf("%d %d\n", c1+1, c2+1);
return;
}
d[p] = i;
}
}
printf("YES\n");
}


int main() {
string s;
while(getline(cin, s)) {
stringstream ss(s);
if(!(ss >> n >> m)) break;
cnt = 0;
id.clear();
for(int i = 0; i < n; i++) {
getline(cin, s);
int lastpos = -1;
for(int j = 0; j < m; j++) {
int p = s.find(',', lastpos+1);
if(p == string::npos) p = s.length();
db[i][j] = ID(s.substr(lastpos+1, p - lastpos - 1));
lastpos = p;
}
}
find();
}
return 0;
}

`c++

#include

#include

#include

#include

#include

#include
using namespace std;

typedef pair<int, int> PII;

const int MAXR = 10000 + 5;
const int MAXC = 10 + 5;

int m, n, db[MAXR][MAXC], cnt;
map<string, int> id;

int ID(const string& s) {
if (!id.count(s)) {
id[s] = ++cnt;
}
return id[s];
}

int main() {

return 0;

}

`10-11 UVa11181 ProbabilityGiven

《算法竞赛入门经典》 第四章:函数与程序结构

发表于 2022-01-22 | 分类于 《算法竞赛入门经典》

基础知识

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 计算两点之间的欧几里德的距离
double dist(double x1, double x2, double y1, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double dist_1(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
return hypot(dx, dy);
}

struct Point {
double x, y;
};

double dist_2(struct Point a, struct Point b) {
return hypot(a.x - b.x, a.y - b.y);
}

typedef struct {
double x, y;
}Point_;

double dist_3(Point_ a, Point_ b) {
return hypot(a.x - b.x, a.y - b.y);
}
1
2
3
4
5
6
7
8
9
10
11
12
// 4.1 组合数(有BUG,中间变量可能会溢出)
long long factorial(int n) {
long long m = 1;
for (int i = 1; i <= n; i++) {
m *= i;
}
return m;
}

long long C(int n, int m) {
return factorial(n) / (factorial(m) * factorial(n - m));
}
1
2
3
4
5
6
7
8
// 4.1 组合数
long long C(int n, int m) {
if (m < n - m) m = n - m;
long long ans = 1;
for (int i = m + 1; i <= n; i++) ans *= i;
for (int i = 1; i <= n - m; i++) ans /= i;
return ans;
}
1
2
3
4
5
6
7
// 4.2 素数判定 (有问题)
int is_prime(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}

1会被误判为素数,当n太大的时候,当n为接近int最大值的素数时候,i*i有可能会发生溢出。

1
2
3
4
5
6
7
8
9
// 4.2 素数判定
int is_prime_1(int n) {
if (n <= 1) return 0;
int m = floor(sqrt(n) + 0.5);
for (int i = 2; i <= m, i++) {
if (n % i == 0) return 0;
}
return 1;
}

避免了重复计算sqrt(n),同时+0.5避免了xxx.99999出现后取整的误差。

1
2
3
4
5
6
7
8
9
// 4.3 用函数交换变量(错误)
void swap(int a, int b) {
int t = a; a = b; b = t;
}

int a, b;
a = 3; b = 4;
swap(3, 4);
printf("a: %d b: %d\n", a, b);

同理,试了下,java也是不行的。

1
2
3
4
5
6
7
8
9
10
11
public static void swap(int a, int b) {
int t = a;
a = b;
b = t;
}

public static void main(String[] args) {
int a = 3, b = 4;
swap(a, b);
System.out.printf("a = %d, b = %d.", a, b);
}

1
2
3
4
5
6
7
8
9
10
// 4.3 用函数交换变量
void swap(int* a, int* b) {
int t = *a; *a = *b; *b = t;
}

int a =3, b = 4;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);

a = 4, b = 3

Q:递归和迭代到底是什么关系?

1
2
3
4
5
6
7
8
9
10
11
12
13
// 计算数组的元素和(错误,PS:C++中是可行的,亲测)
int sum(int a[]) {
int ans = 0;
for (int i = 0; i < sizeof(a); i++) {
ans += a[i];
}
return ans;
}

int a[] = {1,2,3};
printf("%d\n", sum(a));

6(C++测得)

新建了个c项目测试了下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int sum(int a[]) {
int ans = 0;
for (int i = 0; i < sizeof(a); i++) {
ans += a[i];
}
return ans;
}

int main() {
int a[] = {1,2,3};
printf("%d\n", sum(a));
return 0;
}

D:\Markdown\Coding\C\Exercises\main.c: In function 'sum':
D:\Markdown\Coding\C\Exercises\main.c:5:31: warning: 'sizeof' on array function parameter 'a' will return size of 'int *' [-Wsizeof-array-argument]
for (int i = 0; i < sizeof(a); i++) {
^
D:\Markdown\Coding\C\Exercises\main.c:3:13: note: declared here
int sum(int a[]) {

修改后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int sum(int* a, int n) {
int ans = 0;
for (int i = 0; i < n; i++) {
ans += a[i];
}
return ans;
}

int main() {
int a[] = {1,2,3};
printf("%d\n", sum(a + 1, 2));
return 0;
}

5

回到c++的环境中来:c可以的c++一定可以嘛。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 计算数组的元素和(正确)
int sum(int* a, int n) {
int ans = 0;
for (int i = 0; i < n; i++) {
ans += a[i];
}
return ans;
}

int a[] = {1,2,3};
printf("%d\n", sum(a + 1, 2));
return 0;

5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 计算左闭右开区间的元素和(两种写法)
// 方法一
int sum(int* begin, int* end) {
int n = end - begin;
int ans = 0;
for (int i = 0; i < n; i++) {
ans += begin[i];
}
return ans;
}

// 方法二
int sum(int* begin, int* end) {
int *p = begin;
int ans = 0;
for (int *p = begin; p != end; p++) {
ans += *p;
}
return ans;
}

暂时掠过:函数做参数。道理明白,题先放着。

1
2
3
4
// 用递归计算阶乘
int f(int n){
return n == 0 ? 1 : f(n - 1) * n;
};

记住,调用自己和调用其他函数没有什么本质不同。

4-1 UVa1339 Ancient Cipher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {
char s1[200], s2[200];
while(scanf("%s%s", s1, s2) == 2) {
int n = strlen(s1);
int cnt1[26] = {0}, cnt2[26] = {0};
for(int i = 0; i < n; i++) cnt1[s1[i] - 'A']++;
for(int i = 0; i < n; i++) cnt2[s2[i] - 'A']++;
sort(cnt1, cnt1 + 26);
sort(cnt2, cnt2 + 26);
int ok = 1;
for(int i = 0; i < 26; i++)
if(cnt1[i] != cnt2[i]) ok = 0;
if(ok) printf("YES\n"); else printf("NO\n");
}
return 0;
}


JWPUDJSTVP
VICTORIOUS

YES

4-2 UVa489 Hangman Judge

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h>
using namespace std;

#define maxn 105
int Left, Chance; // 还要猜left个位置,错chance次后就会输
char s[maxn], s1[maxn];
int win, lose;

void guess(char ch) {
int bad = 1;
for (int i = 0; i < strlen(s); i++) {
if (s[i] == ch) { Left--; s[i] = ' '; bad = 0; }
}
if (bad) Chance--;
if (!Chance) lose = 1;
if (!Left) win = 1;
}

int main() {
int rnd;
while (scanf("%d%s%s", &rnd, s, s1) == 3 && rnd != -1) {
printf("Round %d\n", rnd);
win = lose = 0;
Left = strlen(s);
Chance = 7;
for (int i = 0; i < strlen(s1); i++) {
guess(s1[i]);
if (win || lose) break;
}
if (win) printf("You win.\n");
else if (lose) printf("You lose.\n");
else printf("You chickened out.\n");
}

return 0;
}

4-3 UVa133 The Dole Queue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;

#define maxn 25
int n, m, k, cell[maxn];

int go(int p, int d, int t) {
while (t--) {
do {
p = (p + d + n - 1) % n + 1;
} while (cell[p] == 0);
}
return p;
}

int main() {
while (scanf("%d%d%d", &n, &k, &m) == 3 && n) {
for (int i = 1; i <= n; i++) cell[i] = i;
int left = n;
int p1 = n, p2 = 1;
while (left) {
p1 = go(p1, 1, k);
p2 = go(p2, -1, m);
printf("%3d", p1); left--;
if (p2 != p1) { printf("%3d", p2); left--; }
cell[p1] = cell[p2] = 0;
if (left) printf(",");
}
printf("\n");
}

return 0;
}

4-4 UVa213 Message Decoding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// UVa213 Message Decoding
// Rujia Liu
// 此版本保留书中展示的调试语句
#include<stdio.h>
#include<string.h>

int readchar() {
for(;;) {
int ch = getchar();
if(ch != '\n' && ch != '\r') return ch;
}
}

int readint(int c) {
int v = 0;
while(c--) v = v * 2 + readchar() - '0';
return v;
}

int code[8][1<<8];

int readcodes() {
memset(code, 0, sizeof(code));
code[1][0] = readchar();
for(int len = 2; len <= 7; len++) {
for(int i = 0; i < (1<<len)-1; i++) {
int ch = getchar();
if(ch == EOF) return 0;
if(ch == '\n' || ch == '\r') return 1;
code[len][i] = ch;
}
}
return 1;
}

// 用于调试
void printcodes() {
for(int len = 1; len <= 3; len++)
for(int i = 0; i < (1<<len)-1; i++) {
if(code[len][i] == 0) return;
printf("code[%d][%d] = %c\n", len, i, code[len][i]);
}
}

int main() {
while(readcodes()) {
//printcodes();
for(;;) {
int len = readint(3);
if(len == 0) break;
//printf("len=%d\n", len);
for(;;) {
int v = readint(len);
//printf("v=%d\n", v);
if(v == (1 << len)-1) break;
putchar(code[len][v]);
}
}
putchar('\n');
}
return 0;
}

4-5 UVa512 Spreadsheet Tracking

1
2


4-6 UVa12412 A Typical Homework

1
2


《算法竞赛入门经典》 第三章:数组和字符串

发表于 2022-01-21 | 分类于 《算法竞赛入门经典》

3.1 数组

读入一些整数,逆序输出到一行中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 程序3-1 逆序输出
#define maxn 105
int a[maxn];

int x, n = 0;
while (scanf("%d", &x) == 1) {
a[n++] = x;
}
for (int i = n - 1; i >= 1; i--) {
printf("%d ", a[i]);
}
printf("%d\n", a[0]);


//测试的时候可以改为这样,方便中断程序
while (scanf("%d", &x) == 1 && x != -1)

1 2 3 4 5 6 -1
6 5 4 3 2 1

有n盏灯,编号为1~n。 第1个人把所有灯打开,第2个人按下所有编号为2的倍数的开关(这些灯将被关掉),第3个人按下所有编号为3的倍数的开关(其中关掉的灯将被打开,开着的灯将被关闭),依此类推。 一共有k个人,问最后有哪些灯开着?输入n和k,输出开着的灯的编号。 k≤n≤1000。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 程序3-2 开灯问题
#define maxn 1010
int a[maxn];

int n, k, first = 1;
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &k);
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
if (j % i == 0) a[j] = !a[j];
}
}
for (int i = 1; i <= n; i++) {
if (a[i]) {
if (first) first = 0;
else printf(" ");
printf("%d", i);
}
}
printf("\n");

7 3
1 5 6 7

蛇形填数。 在n×n方阵里填入1,2,…,n×n,要求填成蛇形。 例如,n=4时方阵为:

1
2
3
4
10 11 12  1
9 16 13 2
8 15 14 3
7 6 5 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 程序3-3 蛇形填数
int a[20][20];
int n, x, y, tot = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a)); // 数组初始化
tot = a[x = 0][y = n - 1] = 1;
while (tot < n * n) {
while (x + 1< n && !a[x + 1][y]) a[++x][y] = ++tot;
while (y - 1 >= 0 && !a[x][y - 1]) a[x][--y] = ++tot;
while (x - 1 >= 0 && !a[x - 1][y]) a[--x][y] = ++tot;
while (y + 1 < n && !a[x][y + 1]) a[x][++y] = ++tot;
}
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++)
printf("%3d", a[x][y]);
printf("\n");
}

4
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4

3.2 字符数组

找出所有形如abc*de(三位数乘以两位数)的算式,使得在完整的竖式中,
所有数字都属于一个特定的数字集合。 输入数字集合(相邻数字之间没有空格),输出所有竖式。 每个竖式前应有编 号,之后应有一个空行。 最后输出解的总数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 程序3-4 竖式问题
int cnt = 0;
char s[20], buf[99];
scanf("%s", s);
for (int abc = 111; abc <= 999; abc++) {
for (int de = 11; de <= 99; de++) {
int x = abc * (de % 10), y = abc * (de / 10), z = abc * de;
sprintf(buf, "%d%d%d%d%d", abc, de, x, y, z);
int ok = 1;
for (int i = 0; i < strlen(buf); i++) {
if (strchr(s, buf[i]) == NULL) ok = 0;
}
if (ok) {
printf("<%d>\n", ++cnt);
printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----\n%5d\n\n", abc, de, x, y, z);
}
}
}
printf("The number of solution = %d\n", cnt);


2357
<1>
775
X 33
-----
2325
2325
-----
25575

The number of solution = 1

3.3 竞赛题目选讲

3-1 UVa272 Tex Quotes

在TeX中,左双引号是“”,右双引号是“’’”。 输入一篇包含双引号的文章,你的任务是
把它转换成TeX的格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
int c, q = 1;
while ((c = getchar()) != EOF) {
if (c == '"') {
printf("%s", q ? "``" : "''");
q = !q;
} else {
printf("%c", c);
}
}

return 0;
}

"To be or not to be," quoth the Bard, "that is the question".
``To be or not to be,'' quoth the Bard, ``that is the question''.

3-2 UVa10082 WERTYU

把手放在键盘上时,稍不注意就会往右错一位。这样,输入Q会变成输入W,输入J会变成输入K等。键盘如图3-2所示。输入一个错位后敲出的字符串(所有字母均大写),输出打字员本来想打出的句子。
输入保证合法,即一定是错位之后的字符串。例如输入中不会出现大写字母A。

1
2
3
4
5
6
7
8
9
10
11
12
13
char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

int main() {
int i, c;
while ((c = getchar()) != EOF) {
for (i = 1; s[i] && s[i] != c; i++);
if (s[i]) putchar(s[i - 1]);
else putchar(c);
}
}

O S, GOMR YPFSU/
I AM FINE TODAY.

for (i = 1; s[i] && s[i] != c; i++);

c是键盘上打不出的字符(c不在s字符数组里),直接输出c。

else putchar(c);

如果c = s[i],找出c在键盘中的位置,s的下标,然后输出前一个字符.

if (s[i]) putchar(s[i - 1]);

3-3 UVa401 Palindromes

输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后和原串相同,如abba和madam。 所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个字符的镜像如图3-3所示(空白项表示该字符镜像后不能得到一个合法字符)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const char* res = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const char* msg[] = {"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};
char r(char ch) {
if (isalpha(ch)) return res[ch - 'A'];
return res[ch - '0' + 25];
}

int main() {
char s[300];
while (scanf("%s", s) == 1) {
int len = strlen(s);
int p = 1, m = 1;
for (int i = 0; i < (len + 1 / 2); i++) {
if (s[i] != s[len - i - 1]) p = 0; // 不是回文串
if (r(s[i]) != s[len - i - 1]) m = 0; // 不是镜像串
}
printf("%s -- is %s.\n\n", s, msg[m * 2 + p]);
}
}

NOTAPALINDROME
NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI
ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS
2A3MEAS -- is a mirrored string.

ATOYOTA
ATOYOTA -- is a mirrored palindrome.

3-4 UVa340 Master-Mind Hints

实现一个经典”猜数字”游戏。 给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B)。
输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时输入结束。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define maxn 1010

int main() {
int n, a[maxn], b[maxn];
int kase = 0;
while (scanf("%d", &n) == 1 && n) {
printf("Game %d:\n", ++kase);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (; ;) {
int A = 0, B = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (a[i] == b[i]) A++;
}
if (b[0] == 0) break;
for (int d = 1; d <= 9; d++) {
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == d) c1++;
if (b[i] == d) c2++;
}
if (c1 < c2) B += c1;
else B += c2;
}
printf(" (%d,%d)\n", A, B - A);
}
}
return 0;
}

3-5 UVa1583 Digit Generator

如果x加上x的各个数字之和得到y,就说x是y的生成元。 给出n(1≤n≤100000),求最小生成元。 无解输出0。 例如,n=216,121,2005时的解分别为198,0,1979。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define maxn 100005
int ans[maxn];

int main() {
int T, n;
memset(ans, 0, sizeof(ans));
for (int m = 1; m < maxn; m++) {
int x = m, y = m;
while (x > 0) {
y = y + x % 10;
x = x / 10;
}
if (ans[y] == 0 || m < ans[y]) ans[y] = m;
}
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
}
}

3-6 UVa1584 Circular Sequence

长度为n的环状串有n种表示法,分别为从某个位置开始顺时针得到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#define maxn 105

int Less(const char* s, int p, int q) {
int n = strlen(s);
for (int i = 0; i < n; i++) {
if (s[(p + i) % n] != s[(q + i) % n]) {
return s[(p + i) % n] < s[(q + i) % n];
}
}
return 0;
}

int main() {
int T;
char s[maxn];
scanf("%d", &T);
while (T--) {
scanf("%s", s);
int ans = 0;
int n = strlen(s);
for (int i = 1; i < n; i++) {
if (Less(s, i, ans)) ans = i;
}
for (int j = 0; j < n; j++) {
putchar(s[(j + ans) % n]);
}
putchar('\n');
}
}

2022 Jan

发表于 2022-01-18

hello, world!

123

REQg

25 日志
3 分类
© 2023 REQg
由 Hexo 强力驱动
|
主题 — NexT.Mist v5.1.4
浙ICP备 - 19004121号