ICPC训练联盟2021寒假冬令营(5)(部分题解):


A - Brainman
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.

Problem
Here’s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 2 8 0 3
swap (2 8) 8 2 0 3
swap (2 0) 8 0 2 3
swap (2 3) 8 0 3 2
swap (8 0) 0 8 3 2
swap (8 3) 0 3 8 2
swap (8 2) 0 3 2 8
swap (3 2) 0 2 3 8
swap (3 8) 0 2 8 3
swap (8 3) 0 2 3 8

So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:
Start with: 2 8 0 3
swap (8 0) 2 0 8 3
swap (2 0) 0 2 8 3
swap (8 3) 0 2 3 8

The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond’s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.
Input
The first line contains the number of scenarios.
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Start the output for every scenario with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.
Sample Input
4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0
Sample Output
Scenario #1:
3

Scenario #2:
0

Scenario #3:
5

Scenario #4:
0
大概意思就是要求逆序对数:可以直接求,或者模拟插入排序的过程也可以求:
第一种:直接遍历求a[i]后面的数中比a[i]小的数,累加起来:

#include<iostream>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1005
typedef long long int ll;
int main(){int t;cin>>t;for(int i=1;i<=t;i++){int n;cin>>n;int a[N];for(int i=0;i<n;i++)scanf("%d",&a[i]);int res=0;for(int j=0;j<n;j++)for(int k=j+1;k<n;k++)if(a[k]<a[j]) res++;cout<<"Scenario #"<<i<<":"<<endl;cout<<res<<endl<<endl;}return 0;
}

第二种:在插入排序的过程中发生交换动作记一次数

#include<iostream>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1005
typedef long long int ll;
int Insert_Sort(int a[],int n){a[0]=INT_MIN;int res=0;for(int i=2;i<=n;i++)for(int j=i;a[j]<a[j-1];j--){swap(a[j],a[j-1]);res++;}return res;
}
int main(){int t;cin>>t;for(int i=1;i<=t;i++){int n;cin>>n;int a[N];for(int j=1;j<=n;j++)           //留出第一个空位方便操作scanf("%d",&a[j]);int res=Insert_Sort(a,n);cout<<"Scenario #"<<i<<":"<<endl;cout<<res<<endl<<endl;}return 0;
}

还有一种(归并的过程中求逆序对数):数据量小的话,其实没必要用归并…

#include<iostream>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 500005
typedef long long int ll;
int res;
int temp[N];
int merge(int src[],int des[],int i,int m,int n){//将有序序列src[i...m],src[m+1,n]合并到des[i...n]int srcl=i,srcr=m+1,ldes=i;while(srcl<=m&&srcr<=n){if(src[srcl]<=src[srcr]){des[ldes++]=src[srcl++];}else{res+=m-srcl+1;des[ldes++]=src[srcr++];}}while(srcl<=m)      //判断并合并多余部分{des[ldes++]=src[srcl++];}while(srcr<=n){des[ldes++]=src[srcr++];}for(int j=i;j<=n;j++)   //拷贝回原数组src[j]=des[j];return 0;
}
int mergesort(int src[],int temp[],int s,int t){//将src[s...t]对半分解直至为长度为1的子序列,然后开始合并if(s==t) return 0;else{int m=(s+t)/2;             //将序列分两组 mergesort(src,temp,s,m);//src[s...m]分解mergesort(src,temp,m+1,t);//src[m+1...t]分解 merge(src,temp,s,m,t);//src[s..m]和src[m+1...t]合并到temp中,再将temp拷贝回src中 }return 0;
}
int merge_sort(int a[],int n)
{// int temp[N];            //数组开大了只能开全局变量memset(temp,0,sizeof(temp));mergesort(a,temp,0,n-1);return 0;
}int main(){int t;cin>>t;int j=1;while(j<=t){int n;cin>>n;int a[N];for(int i=0;i<n;i++)scanf("%d",&a[i]);res=0;merge_sort(a,n);cout<<"Scenario #"<<j<<":"<<endl;cout<<res<<endl<<endl;j++;}return 0;
}

E - Word Amalgamation
In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
Input
The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled ‘words’ that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.
Sample Input
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
Sample Output
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
题意:先输入字典,再给出一组单词,要求在字典中查找所有和该词一样的单词(不比较字母顺序),若未找到输出NOT A VALID WORD即可
代码:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1005
#define PI acos(-1.0)
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;
bool cmp(pair<string,string>a1,pair<string,string>a2){return a1.second<a2.second;
}
int main(){ios::sync_with_stdio(false);cin.tie(0); vector< pair<string,string> > dic;vector<string> wd;string s;cin>>s;while(s!="XXXXXX"){string s0=s;sort(s.begin(),s.end());dic.push_back( pair< string,string >(s,s0));cin>>s;}sort(dic.begin(),dic.end(),cmp);cin>>s;while(s!="XXXXXX"){sort(s.begin(),s.end());wd.push_back(s);cin>>s;}for(int i=0;i<wd.size();i++){int f=0;for(int j=0;j<dic.size();j++){if(wd[i]==dic[j].first){f=1;cout<<dic[j].second<<endl;}}if(!f)cout<<"NOT A VALID WORD"<<endl;cout<<"******"<<endl;}return 0;
}

F - Flooded!
To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients with the elevation of each 10-meter by 10-meter square of land in regions where homes may be purchased. Water from rain, melting snow, and burst water mains will collect first in those squares with the lowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we also assume that storm sewers enable water from high-elevation squares in valleys (completely enclosed by still higher elevation squares) to drain to lower elevation squares, and that water will not be absorbed by the land.
From weather data archives, we know the typical volume of water that collects in a region. As prospective homebuyers, we wish to know the elevation of the water after it has collected in low-lying squares, and also the percentage of the region’s area that is completely submerged (that is, the percentage of 10-meter squares whose elevation is strictly less than the water level). You are to write the program that provides these results.
Input
The input consists of a sequence of region descriptions. Each begins with a pair of integers, m and n, each less than 30, giving the dimensions of the rectangular region in 10-meter units. Immediately following are m lines of n integers giving the elevations of the squares in row-major order. Elevations are given in meters, with positive and negative numbers representing elevations above and below sea level, respectively. The final value in each region description is an integer that indicates the number of cubic meters of water that will collect in the region. A pair of zeroes follows the description of the last region.
Output
For each region, display the region number (1, 2, …), the water level (in meters above or below sea level) and the percentage of the region’s area under water, each on a separate line. The water level and percentage of the region’s area under water are to be displayed accurate to two fractional digits. Follow the output for each region with a blank line.
Sample Input
3 3
25 37 45
51 12 34
94 83 27
10000
0 0
Sample Output
Region 1
Water level is 46.67 meters.
66.67 percent of the region is under water.
题意:有海拔高度不同的楼房(题目以二维矩阵给出),现给定一定体积的水,问能淹没的高度以及淹没的楼占总楼数的百分比
最先淹没的肯定是海拔比较低的楼房,因此对输入的海拔排序,不断计算淹没高度h需要的水量,直至水消耗完
代码:

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1005
#define PI acos(-1.0)
#define reset(x) memset(x,0,sizeof(x))
typedef long long int ll;
int main(){ios::sync_with_stdio(false);cin.tie(0); int k=1;int m,n;while(cin>>m>>n&&m&&n){int a[N];for(int i=0;i<m*n;i++)cin>>a[i];sort(a,a+m*n);double sum;cin>>sum;double ht=a[0];int count=1;if(sum)for(int i=1;i<m*n;i++){if((a[i]-a[i-1])*100*i<sum){sum-=(a[i]-a[i-1])*100*i;count++;ht=a[i];}else{ht+=sum/(i*100);sum=0;break;}}if(sum) ht+=sum/(n*m*100);  //全淹没printf("Region %d\n",k++);printf("Water level is %.2f meters.\n",ht);printf("%.2f percent of the region is under water.\n",count*100.0/(m*n));}return 0;
}

ICPC训练联盟2021寒假冬令营(5)(部分题解):相关推荐

  1. ICPC训练联盟2021寒假冬令营(7)部分题解

    ICPC训练联盟2021寒假冬令营(7)部分题解: A - Doubles As part of an arithmetic competency program, your students wil ...

  2. ICPC训练联盟2021寒假冬令营(2)

    文章目录 ICPC训练联盟2021寒假冬令营(2) [A - Maya Calendar](https://vjudge.net/problem/POJ-1008) 题目 分析 代码 [B - Dip ...

  3. ICPC训练联盟2021寒假冬令营(2)(部分题解)

    ICPC训练联盟2021寒假冬令营(2)(部分题解) B - Diplomatic License In an effort to minimize the expenses for foreign ...

  4. ICPC训练联盟2021寒假冬令营(6)_2021.01.25_笔记

    文章目录 试题链接 学习笔记 - C++ STL 简介 STL容器实验 序列式容器 关联式容器 集合容器 A - The Blocks Problem (POJ 1208, UVA 101) 中文释义 ...

  5. ICPC训练联盟2021寒假冬令营(5)_2021.01.22_笔记

    文章目录 试题链接 学习笔记-高效排序算法( O(nlogn)时间复杂度 ) 算法介绍 归并排序 主要思路 算法图解 算法代码 快速排序 主要思路 算法代码 十大排序算法的动画演示链接 C++STL排 ...

  6. ICPC训练联盟2021寒假冬令营(7)_2021.01.26_笔记

    文章目录 试题链接 学习笔记 - C++STL.贪心算法 C++STL 迭代器 STL算法 关联式容器 贪心算法 介绍 使用贪心法能否得到最优解,是必须加以证明的. 体验贪心法内涵的实验范例 贪心法的 ...

  7. 【算法笔记】【几何初步、数学初步、矩阵计算】ICPC训练联盟2021寒假冬令营(3)笔记

    题目链接 A题题解:枚举法+简单直线方程知识. 本题采取枚举方法,在[-500, 500]的范围内枚举A和B,将樱桃坐标代入直线方程Ax+By,如果Ax+By大于0,则樱桃在直线方:小于0,则樱桃在直 ...

  8. ICPC训练联盟2021寒假冬令营(4)_2021.01.21_笔记

    文章目录 试题链接 学习笔记-排序算法( O(n^2^)时间复杂度 ) 选择排序程序段(C++) 冒泡排序程序段(C++) 插入排序程序段 A - Necklace (UVA 11001) 中文释义 ...

  9. ICPC训练联盟2021寒假冬令营(9)_2021.01.29_笔记

    试题链接 点我进入代码提交OJ 学习笔记 - 数论与组合分析入门 数论的编程实验 • 3.1素数运算的实验范例 • 3.2求解不定方程和同余方程的实验范例 • 3.3 特殊的同余式 • 3.4 积性函 ...

最新文章

  1. 【译】Asp.net MVC 利用自定义RouteHandler来防止图片盗链 (转)
  2. 你知道吗?脑机接口训练会对大脑物质结构和功能产生影响
  3. php如何实现省市,PHP简单实现正则匹配省市区的方法
  4. caffe模型weightsfeatureMap 可视化(c++)
  5. shell中的${},##和%%的使用
  6. 调试opencv程序显示应用程序无法正常启动,0xc000007b
  7. 如何对聚类结果进行分析_如何更合理地给聚类结果贴标签——由一个挖掘学生用户的项目说开去...
  8. 软件测试技术第一次试验之——JUnit的安装与使用
  9. Clipsync – 同步 Win 和 Android 剪贴板
  10. mysql 物理删除 索引_MySQL 索引重建
  11. jsonp和ajax的区别,dataType jsonp和JSON之间的区别
  12. 32位crc校验码程序_基于FPGA的CRC校验码生成器设计
  13. 第一次个人作业--词频统计总结
  14. 校园网一直是连接认证服务器无响应,校园网常见问题解决办法
  15. 刘强东的“长期主义”:做正确的事,敢于追求极致
  16. 赛马命运已成,微盟在劫难逃
  17. aw2013驱动分析
  18. RPA学习-数据表处理
  19. 咸鱼笔记:适合小白的Anaconda下载和安装步骤及使用教程
  20. Java基础篇 学习笔记

热门文章

  1. 60秒Dapp快讯 | 全球公有链技术评估:以太坊在应用性上排第一;蚂蚁金服区块链试水医疗电子票据
  2. sql loader导出数据和导入数据(sqlldr)
  3. 【源码阅读】GAT:GRAPH ATTENTION NETWORKS
  4. 莫纳什计算机硕士课程挂科率,莫纳什真的是挂科重灾区?
  5. 二叉树所有节点数、叶子节点数的计算
  6. PHP开发基于Mirai的QQ机器人(一)
  7. centos7 shell脚本开机自启动(亲测可用)
  8. MySQL生成自增的流水号
  9. 刚刚,ChatGPT官宣数学能力再升级,网友:终于精通十以内加减法了
  10. IOS开发 当滑动tabelview时,使键盘滑落