1.打酱油
#include <iostream>
using namespace std;
int main() {
int n, count=0;
cin >> n;
while(n > 0){
if(n >= 30 && n < 50){
count += 4;
n -= 30;
}else if(n >= 50){
count += 7;
n -= 50;
}else{
count += 1;
n -= 10;
}
}
cout << count << endl;
return 0;
}
40
5
80
11
2.公共钥匙盒(难)
#include <iostream>
#include <algorithm>
#include <queue>
#define MAXN 1005
using namespace std;
struct Teacher{
int key, sta, fin;
bool operator < (const Teacher &a) const{
if(fin == a.fin) return key > a.key;
return fin > a.fin;
}
}tea[MAXN];
int pos[MAXN];
int keypos[MAXN];
priority_queue<int, vector<int>, greater <int>> sparepos;
priority_queue<Teacher> backtea;
bool teacmp(const Teacher &a, const Teacher &b){
return a.sta < b.sta;
}
void takeKey(Teacher tea){
sparepos.push(keypos[tea.key]);
backtea.push(tea);
keypos[tea.key]=0;
}
void backKey(Teacher tea){
keypos[tea.key]=sparepos.top();
sparepos.pop();
backtea.pop();
}
int main(){
int N, K;
scanf("%d%d", &N, &K);
for(int i = 1;i <= N; ++i)
keypos[i] = i;
for(int i = 0;i < K; ++i){
scanf("%d%d%d", &tea[i].key, &tea[i].sta, &tea[i].fin);
tea[i].fin += tea[i].sta;
}
sort(tea, tea + K, teacmp);
for(int i = 0;i < K; ++i){
while(!backtea.empty() && tea[i].sta >= backtea.top().fin){
backKey(backtea.top());
}
takeKey(tea[i]);
}
while(!backtea.empty())
backKey(backtea.top());
for(int i = 1;i <= N; ++i)
pos[keypos[i]] = i;
for(int i = 1;i <= N; ++i)
printf("%d ",pos[i]);
return 0;
}
5 2
4 3 3
2 2 7
1 4 3 2 5
5 7
1 1 14
3 3 12
1 15 12
2 7 20
3 18 12
4 21 19
5 30 9
1 2 3 5 4
3.JSON查询(难)
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<map>
#define MAXN 16000
using namespace std;
string del_slash(const string &str){
string tmp = "";
if(str == "") return tmp;
for(string::size_type i = 1; i < str.length() - 1; ++i){
if(str[i] == '\\') tmp += str[++i];
else tmp += str[i];
}
return tmp;
}
int main(){
char json[MAXN] = {0}, ch;
int n, m, cnt = 0, quote = 0, slash = 0;
scanf("%d%d", &n, &m);
getchar();
for(int i = 0; i < n; ++i){
while((ch = getchar()) != '\n'){
if(ch == '\"'){
if(json[cnt-1] != '\\') {
if((quote&1) == 0)
json[cnt++] = ' ';
quote++;
}
else if((slash&1) == 1){
slash--;
}
else{
quote++;
}
json[cnt++] = '\"';
}
else if(ch == ':' && (quote&1) == 0){
json[cnt++] = ' ';
json[cnt++] = ':';
}
else if(ch == ',' && (quote&1) == 0){
json[cnt++] = ' ';
json[cnt++] = ',';
}
else if(ch == '}' && (quote&1) == 0){
json[cnt++] = ' ';
json[cnt++] = '}';
}
else if(ch == '\\'){
json[cnt++] = '\\';
slash++;
}
else if(ch == ' ' && (quote&1) == 0){
continue;
}
else json[cnt++] = ch;
}
}
json[cnt++] = 0;
istringstream ss(json);
string prestr = "", str;
string key = "", val = "";
string prefix = "";
vector<string> pfx;
map<string, string> kv;
while(ss >> str){
if(str == "{" || str == ":{"){
pfx.push_back(del_slash(prestr));
if(pfx.back() != "")
prefix += pfx.back();
kv[prefix] = "wingrez object";
if(prefix != "")
prefix += ".";
}
else if(str == "}" || str == "},"){
string t = pfx.back();
pfx.pop_back();
if(prefix != "")
prefix.erase(prefix.length() - t.length() - 1);
}
else if(str == ":") {
key = prefix + del_slash(prestr);
ss >> val;
kv[key] = del_slash(val);
}
prestr = str;
}
string query, ans;
for(int i = 0; i < m; ++i){
cin >> query;
if(kv.count(query) == 0) cout << "NOTEXIST" << endl;
else{
ans = kv[query];
if(ans == "wingrez object") cout << "OBJECT" << endl;
else cout << "STRING " << ans << endl;
}
}
return 0;
}
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, string> json;
int state;
string key;
void deal(string& s){
for(int i = 0; s[i]; i++) {
if(s[i] == '{') {
if(state == 0) key = "";
else json[key]="OBJECT";
state = 1;
}
else if(s[i] == '}') {
int j;
for(j = (int)key.size() - 1; j >= 0; j--)
if(key[j] == '.') break;
if(j >= 0) key = key.substr(0, j);
else key = "";
}
else if(s[i] == ':') state = 2;
else if(s[i] == ',') state = 1;
else if(s[i] == '"') {
string tmp;
for(i++; s[i]; i++) {
if(s[i] == '\\') tmp += s[++i];
else if(s[i] == '"') break;
else tmp += s[i];
}
if(state == 1) {
if(key == "") key = tmp;
else key += '.' + tmp;
state = 2;
}
else if(state == 2) {
json[key] = "STRING " + tmp;
int j;
for(j = (int)key.size() - 1; j >= 0; j--) {
if(key[j] == '.') break;
}
if(j >= 0) key = key.substr(0, j);
else key = "";
state = 1;
}
}
}
}
int main(){
int n, m;
string line;
cin >> n >> m;
getline(cin, line);
state = 0;
while(n--) {
getline(cin, line);
deal(line);
}
while(m--) {
string str;
cin >> str;
cout << (json[str] == "" ? "NOTEXIST" : json[str]) << endl;
}
return 0;
}
10 5
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "2ndStreet",
"city": "NewYork",
"state": "NY"
},
"esc\\aped": "\"hello\""
}
firstName
address
address.city
address.postal
esc\aped
STRING John
OBJECT
STRING NewYork
NOTEXIST
STRING "hello"
4.通信网络
#include <iostream>
#include <vector>
#include <queue>
#define maxn 1001
using namespace std;
vector<int> edge[maxn];
int vis[maxn]={0};
int link[maxn][maxn]={0};
void dfs(int v, int cur){
vis[v]=1;
link[v][cur]=link[cur][v]=1;
for(int j=0;j<edge[v].size();++j){
if(!vis[edge[v][j]])
dfs(edge[v][j], cur);
}
}
void bfs(int v,int cur){
queue<int> q;
q.push(v);
vis[v]=1;
while(!q.empty()){
int top=q.front();
q.pop();
link[top][cur]=link[cur][top]=1;
for(int j=0;j<edge[top].size();++j){
if(!vis[edge[top][j]]){
q.push(edge[top][j]);
vis[edge[top][j]]=1;
}
}
}
}
int main(){
int N,M,ans=0;
scanf("%d%d",&N,&M);
for(int i=0;i<M;++i){
int u,v;
scanf("%d%d",&u,&v);
edge[u].push_back(v);
}
for(int i=1;i<=N;++i){
memset(vis,0,sizeof(int)*maxn);
bfs(i,i);
}
for(int i=1;i<=N;++i){
int j;
for(j=1;j<=N;++j){
if(link[i][j]==0)
break;
}
if(j==N+1) ans++;
}
printf("%d",ans);
return 0;
}
4 4
1 2
1 3
2 4
3 4
2
5.除法